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
200 lines (175 loc) · 5.15 KB
/
index.ts
File metadata and controls
200 lines (175 loc) · 5.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import proxify, { IS_PROXY, STATUS } from './proxify'
const isPlainObject = require('is-plain-object')
export type Options = {
devmode?: boolean
dynamicWrapper?: Function
}
export type Mutation = {
method: string
path: string
args: any[]
}
class ProxyStateTree {
state: object
options: Options
pathDependencies: object
globalDependencies: Set<Function>
objectChanges: Set<string>
mutations: Mutation[]
currentMutations: Mutation[]
paths: Set<string>[]
status: STATUS
proxy: any
currentFlushId: number
constructor(state: object, options: Options = {}) {
if (!isPlainObject(state)) {
throw new Error('You have to pass a plain object to the Proxy State Tree')
}
if (typeof options.devmode === 'undefined') {
options.devmode = true
}
this.state = state
this.options = options
this.pathDependencies = {}
this.globalDependencies = new Set()
this.mutations = []
this.currentMutations = []
this.paths = []
this.objectChanges = new Set()
this.status = STATUS.IDLE
this.proxy = proxify(this, state)
this.currentFlushId = 0
}
get() {
return this.proxy
}
addTrackingPath(path) {
if (this.status === STATUS.TRACKING_PATHS) {
this.paths[this.paths.length - 1].add(path)
}
}
flush() {
const pathCallbacksToCall = new Set()
const mutations = this.mutations.slice()
const flushId = this.currentFlushId++
for (let objectChange of this.objectChanges) {
if (this.pathDependencies[objectChange]) {
for (let callback of this.pathDependencies[objectChange]) {
pathCallbacksToCall.add(callback)
}
}
}
for (let mutation in this.mutations) {
const path = this.mutations[mutation].path
if (this.pathDependencies[path]) {
for (let callback of this.pathDependencies[path]) {
pathCallbacksToCall.add(callback)
}
}
}
for (let callback of pathCallbacksToCall) {
callback(flushId)
}
pathCallbacksToCall.clear()
this.mutations.length = 0
this.objectChanges.clear()
for (let globalDependency of this.globalDependencies) {
globalDependency(mutations, flushId)
}
return {
mutations,
flushId,
}
}
startMutationTracking() {
if (this.status !== STATUS.IDLE) {
throw new Error(
`You can not start tracking mutations unless idle. The status is: ${
this.status
}`
)
}
this.status = STATUS.TRACKING_MUTATIONS
}
clearMutationTracking() {
const currentMutations = this.currentMutations.slice()
this.status = STATUS.IDLE
this.currentMutations.length = 0
this.mutations = this.mutations.concat(currentMutations)
return currentMutations
}
startPathsTracking() {
if (this.status === STATUS.TRACKING_MUTATIONS) {
throw new Error(
`You can not start tracking paths when tracking mutations.`
)
}
this.status = STATUS.TRACKING_PATHS
return this.paths.push(new Set()) - 1
}
clearPathsTracking(index: number) {
if (index !== this.paths.length - 1) {
throw new Error(
'Nested path tracking requires you to stop the nested path tracker before the outer'
)
}
const pathSet = this.paths.pop()
if (!this.paths.length) {
this.status = STATUS.IDLE
}
return pathSet
}
addMutationListener(cb: (mutations: Mutation[], flushId: number) => void)
addMutationListener(initialPaths: Set<string>, cb: (flushId: number) => void)
addMutationListener() {
if (arguments.length === 1) {
const cb = arguments[0]
const globalDependencies = this.globalDependencies
globalDependencies.add(cb)
return {
dispose() {
globalDependencies.delete(cb)
},
}
} else {
const initialPaths = arguments[0]
const cb = arguments[1]
const pathDependencies = this.pathDependencies
let currentStringPaths = initialPaths
for (let currentStringPath of currentStringPaths) {
pathDependencies[currentStringPath] = pathDependencies[
currentStringPath
]
? pathDependencies[currentStringPath].add(cb)
: new Set([cb])
}
return {
update(newStringPaths: Set<string>) {
for (let currentStringPath of currentStringPaths) {
if (!newStringPaths.has(currentStringPath)) {
pathDependencies[currentStringPath].delete(cb)
}
}
for (let newStringPath of newStringPaths) {
if (!currentStringPaths.has(newStringPath)) {
pathDependencies[newStringPath] = pathDependencies[newStringPath]
? pathDependencies[newStringPath].add(cb)
: new Set([cb])
}
}
currentStringPaths = newStringPaths
},
dispose() {
for (let currentStringPath of currentStringPaths) {
pathDependencies[currentStringPath].delete(cb)
if (pathDependencies[currentStringPath].size === 0) {
delete pathDependencies[currentStringPath]
}
}
},
}
}
}
}
export { IS_PROXY }
export default ProxyStateTree