forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (71 loc) · 2.02 KB
/
index.ts
File metadata and controls
84 lines (71 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { ENVIRONMENT, EventType } from 'overmind'
import { afterUpdate, onDestroy, onMount } from 'svelte'
const IS_PRODUCTION = ENVIRONMENT === 'production'
let nextComponentId = 0
export function createMixin(overmind) {
const componentId = nextComponentId++
let nextComponentInstanceId = 0
let currentFlushId = 0
const subscribe = (listener) => {
const tree = overmind.proxyStateTreeInstance.getTrackStateTree()
const componentInstanceId = nextComponentInstanceId++
let isUpdating = false
const onUpdate = (mutations, paths, flushId) => {
tree.track(onUpdate)
currentFlushId = flushId
isUpdating = true
listener(tree.state)
}
tree.track(onUpdate)
listener(tree.state)
if (IS_PRODUCTION) {
afterUpdate(() => {
tree.stopTracking()
isUpdating = false
})
} else {
onMount(() => {
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId,
name: '',
paths: Array.from(tree.pathDependencies),
})
})
afterUpdate(() => {
tree.stopTracking()
if (isUpdating) {
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId,
name: '',
flushId: currentFlushId,
paths: Array.from(tree.pathDependencies),
})
}
isUpdating = false
})
}
return () => {
overmind.proxyStateTreeInstance.disposeTree(tree)
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: componentInstanceId,
name: '',
})
}
}
const reaction = (...args) => {
const dispose = overmind.reaction(...args)
onDestroy(() => {
dispose()
})
}
return {
state: { ...overmind.state, subscribe },
actions: overmind.actions,
effects: overmind.effects,
addMutationListener: overmind.addMutationListener,
reaction: reaction,
}
}