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
247 lines (213 loc) · 6.62 KB
/
index.ts
File metadata and controls
247 lines (213 loc) · 6.62 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { IS_PROXY, VALUE, PATH, STATUS, proxify } from './proxify'
const isPlainObject = require('is-plain-object')
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
export type Options = {
devmode?: boolean
dynamicWrapper?: Function
}
export type Mutation = {
method: string
path: string
args: any[]
}
export type MutationCallback = (
mutation: Mutation,
paths: Set<string>,
flushId: number
) => void
let hasWarnedFlushWhileTracking = false
export class ProxyStateTree {
pathDependencies: object = {}
private parentTree: ProxyStateTree
private globalDependencies = new Set<Function>()
private objectChanges = new Set<string>()
private mutations: Mutation[] = []
private currentMutations: Mutation[] = []
private paths: Set<string>[] = []
private status: Set<STATUS> = new Set()
private currentFlushId: number = 0
private completedTrackingCallbacks = []
private mutationCallbacks: MutationCallback[] = []
private proxy: any
private PROXY = Symbol('TREE_INSTANCE')
constructor(private state: object, private options: Options = {}) {
if (!isPlainObject(state)) {
throw new Error(
'You did not pass a plain object as state to Proxy State Tree'
)
}
if (typeof options.devmode === 'undefined') {
options.devmode = true
}
this.proxy = proxify(this, state)
}
private addMutation(mutation: Mutation) {
this.currentMutations.push(mutation)
const paths = new Set([...this.objectChanges, mutation.path])
if (this.parentTree) {
for (let cb of this.parentTree.mutationCallbacks) {
cb(mutation, paths, this.parentTree.currentFlushId + 1)
}
for (let cb of this.mutationCallbacks) {
cb(mutation, paths, this.parentTree.currentFlushId + 1)
}
} else {
for (let cb of this.mutationCallbacks) {
cb(mutation, paths, this.currentFlushId + 1)
}
}
}
get() {
return this.proxy
}
getScopedTree() {
const instance = new ProxyStateTree(this.state, this.options)
instance.parentTree = this
return instance
}
scope(value) {
return value && value[IS_PROXY]
? proxify(this, value[VALUE], value[PATH])
: value
}
addTrackingPath(path) {
if (this.status.has(STATUS.TRACKING_PATHS)) {
this.paths[this.paths.length - 1].add(path)
}
}
flush() {
const paths = new Set()
const pathCallbacksToCall = new Set()
const mutations = this.mutations.slice()
if (
this.status.has(STATUS.TRACKING_PATHS) &&
!hasWarnedFlushWhileTracking
) {
console.warn(
'PROXY-STATE-TREE has detected that it is flushing out mutations while tracking state changes. Please report this warning with information about any unexpected behaviour'
)
hasWarnedFlushWhileTracking = true
}
const tree = this.parentTree || this
const flushId = tree.currentFlushId++
for (let objectChange of tree.objectChanges) {
if (tree.pathDependencies[objectChange]) {
paths.add(objectChange)
}
}
for (let mutation of mutations) {
paths.add(mutation.path)
}
// Sort so that parent paths are called first
const sortedPaths = Array.from(paths).sort()
for (let path of sortedPaths) {
if (tree.pathDependencies[path]) {
for (let callback of tree.pathDependencies[path]) {
pathCallbacksToCall.add(callback)
}
}
}
for (let callback of pathCallbacksToCall) {
callback(flushId)
}
paths.clear()
pathCallbacksToCall.clear()
this.mutations.length = 0
tree.objectChanges.clear()
for (let globalDependency of tree.globalDependencies) {
globalDependency(mutations, flushId)
}
return {
mutations,
flushId,
}
}
startMutationTracking() {
this.status.add(STATUS.TRACKING_MUTATIONS)
}
clearMutationTracking() {
const currentMutations = this.currentMutations.slice()
this.status.delete(STATUS.TRACKING_MUTATIONS)
this.currentMutations.length = 0
this.mutations = this.mutations.concat(currentMutations)
return currentMutations
}
startPathsTracking() {
this.status.add(STATUS.TRACKING_PATHS)
return this.paths.push(new Set()) - 1
}
clearPathsTracking(index: number, cb?: () => void) {
const pathSet = this.paths.splice(index, 1, null)[0]
while (this.paths[this.paths.length - 1] === null) {
this.paths.pop()
}
if (cb) {
this.completedTrackingCallbacks.push(cb)
}
if (!this.paths.length) {
this.status.delete(STATUS.TRACKING_PATHS)
while (this.completedTrackingCallbacks.length) {
this.completedTrackingCallbacks.shift()()
}
}
return pathSet
}
addMutationListener(cb: MutationCallback) {
const index = this.mutationCallbacks.push(cb) - 1
return () => {
this.mutationCallbacks.splice(index, 1)
}
}
addFlushListener(cb: (mutations: Mutation[], flushId: number) => void)
addFlushListener(initialPaths: Set<string>, cb: (flushId: number) => void)
addFlushListener() {
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, VALUE, PATH }