Skip to content

Commit 1e20df1

Browse files
feat(proxy-state-tree): allow callback to run logic after all tracking is done
1 parent 1bd2b1c commit 1e20df1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ describe('CREATION', () => {
2424
})
2525
})
2626

27+
describe('TRACKING', () => {
28+
test('should allow callback to run when tracking is finished', () => {
29+
let hasCalledCb = false
30+
const tree = new ProxyStateTree({})
31+
const trackId = tree.startPathsTracking()
32+
const trackId2 = tree.startPathsTracking()
33+
tree.clearPathsTracking(trackId2, () => {
34+
hasCalledCb = true
35+
})
36+
expect(hasCalledCb).toBe(false)
37+
tree.clearPathsTracking(trackId)
38+
expect(hasCalledCb).toBe(true)
39+
})
40+
})
41+
2742
describe('OBJECTS', () => {
2843
describe('ACCESS', () => {
2944
test('should create proxy when accessed', () => {

packages/node_modules/proxy-state-tree/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ProxyStateTree {
2121
private paths: Set<string>[] = []
2222
private status: STATUS = STATUS.IDLE
2323
private currentFlushId: number = 0
24+
private completedTrackingCallbacks = []
2425
private proxy: any
2526
constructor(private state: object, private options: Options = {}) {
2627
if (!isPlainObject(state)) {
@@ -118,15 +119,22 @@ class ProxyStateTree {
118119

119120
return this.paths.push(new Set()) - 1
120121
}
121-
clearPathsTracking(index: number) {
122+
clearPathsTracking(index: number, cb?: () => void) {
122123
const pathSet = this.paths.splice(index, 1, null)[0]
123124

124125
while (this.paths[this.paths.length - 1] === null) {
125126
this.paths.pop()
126127
}
127128

129+
if (cb) {
130+
this.completedTrackingCallbacks.push(cb)
131+
}
132+
128133
if (!this.paths.length) {
129134
this.status = STATUS.IDLE
135+
while (this.completedTrackingCallbacks.length) {
136+
this.completedTrackingCallbacks.shift()()
137+
}
130138
}
131139

132140
return pathSet

0 commit comments

Comments
 (0)