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
201 lines (172 loc) · 5.34 KB
/
index.ts
File metadata and controls
201 lines (172 loc) · 5.34 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
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[]
}
let hasWarnedFlushWhileTracking = false
class ProxyStateTree {
pathDependencies: object = {}
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 proxy: any
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)
}
get() {
return this.proxy
}
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()
const flushId = this.currentFlushId++
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
}
for (let objectChange of this.objectChanges) {
if (this.pathDependencies[objectChange]) {
paths.add(objectChange)
}
}
for (let mutation in this.mutations) {
paths.add(this.mutations[mutation].path)
}
// Sort so that parent paths are called first
const sortedPaths = Array.from(paths).sort()
for (let path of sortedPaths) {
if (this.pathDependencies[path]) {
for (let callback of this.pathDependencies[path]) {
pathCallbacksToCall.add(callback)
}
}
}
for (let callback of pathCallbacksToCall) {
callback(flushId)
}
paths.clear()
pathCallbacksToCall.clear()
this.mutations.length = 0
this.objectChanges.clear()
for (let globalDependency of this.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: (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