forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackMutationTree.ts
More file actions
69 lines (64 loc) · 1.69 KB
/
TrackMutationTree.ts
File metadata and controls
69 lines (64 loc) · 1.69 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
import { IProxyStateTree, VALUE } from './'
import { Proxifier } from './Proxyfier'
export interface IMutation {
method: string
path: string
args: any[]
}
export interface IMutationCallback {
(mutation: IMutation, paths: Set<string>, flushId: number): void
}
export interface ITrackMutationTree<T extends object> {
addMutation(mutation: IMutation, objectChangePath?: string): void
onMutation(callback: IMutationCallback): void
canTrack(): boolean
canMutate(): boolean
dispose(): ITrackMutationTree<T>
track(): ITrackMutationTree<T>
master: IProxyStateTree<T>
proxifier: Proxifier
state: T
}
export class TrackMutationTree<T extends object>
implements ITrackMutationTree<T> {
private mutationCallbacks: IMutationCallback[] = []
master: IProxyStateTree<T>
state: T
proxifier: Proxifier
isTracking: boolean = false
constructor(master: IProxyStateTree<T>) {
this.master = master
this.proxifier = new Proxifier(this)
this.state = this.proxifier.proxify(master.state[VALUE], '')
}
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
)
}
}
track() {
this.isTracking = true
return this
}
onMutation(callback: IMutationCallback) {
this.mutationCallbacks.push(callback)
}
canMutate() {
return this.isTracking
}
canTrack() {
return false
}
dispose() {
this.isTracking = false
this.mutationCallbacks.length = 0
return this
}
}