forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutationTree.ts
More file actions
49 lines (47 loc) · 1.23 KB
/
MutationTree.ts
File metadata and controls
49 lines (47 loc) · 1.23 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
import {
IProxyStateTree,
IMutationTree,
IMutationCallback,
IMutation,
IProxifier,
} from './types'
import { Proxifier } from './Proxyfier'
export class MutationTree<T extends object> implements IMutationTree<T> {
private mutationCallbacks: IMutationCallback[] = []
master: IProxyStateTree<T>
state: T
proxifier: IProxifier<T>
isTracking: boolean = false
constructor(master: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
this.isTracking = true
this.master = master
this.proxifier = proxifier || new Proxifier(this)
this.state = this.proxifier.proxify(master.sourceState, '')
}
addMutation(mutation: IMutation, objectChangePath: string) {
this.master.addMutation(mutation, objectChangePath)
for (let callback of this.mutationCallbacks) {
callback(
mutation,
new Set(
objectChangePath ? [mutation.path, objectChangePath] : [mutation.path]
),
this.master.currentFlushId
)
}
}
onMutation(callback: IMutationCallback) {
this.mutationCallbacks.push(callback)
}
canMutate() {
return this.isTracking
}
canTrack() {
return false
}
dispose() {
this.isTracking = false
this.mutationCallbacks.length = 0
return this
}
}