Skip to content

Commit 543ba40

Browse files
feat(proxy-state-tree): add flush ID to track flushes
1 parent 1bcb087 commit 543ba40

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ describe('FUNCTIONS', () => {
392392

393393
describe('REACTIONS', () => {
394394
test('should be able to register a listener using paths', () => {
395+
expect.assertions(2)
395396
let reactionCount = 0
396397
const tree = new ProxyStateTree({
397398
foo: 'bar',
@@ -400,8 +401,9 @@ describe('REACTIONS', () => {
400401
const trackId = tree.startPathsTracking()
401402
state.foo // eslint-disable-line
402403
const paths = tree.clearPathsTracking(trackId)
403-
tree.addMutationListener(paths, () => {
404+
tree.addMutationListener(paths, (flushId) => {
404405
reactionCount++
406+
expect(flushId).toBe(0)
405407
})
406408
tree.startMutationTracking()
407409
state.foo = 'bar2'
@@ -410,19 +412,20 @@ describe('REACTIONS', () => {
410412
expect(reactionCount).toBe(1)
411413
})
412414
test('should be able to register a global listener', () => {
413-
expect.assertions(1)
415+
expect.assertions(2)
414416
const tree = new ProxyStateTree({
415417
foo: 'bar',
416418
})
417419
const state = tree.get()
418-
tree.addMutationListener((mutations) => {
420+
tree.addMutationListener((mutations, flushId) => {
419421
expect(mutations).toEqual([
420422
{
421423
method: 'set',
422424
path: 'foo',
423425
args: ['bar2'],
424426
},
425427
])
428+
expect(flushId).toBe(0)
426429
})
427430
tree.startMutationTracking()
428431
state.foo = 'bar2'
@@ -529,19 +532,22 @@ describe('REACTIONS', () => {
529532
tree.startMutationTracking()
530533
state.foo = 'bar3'
531534
tree.clearMutationTracking()
532-
const mutations = tree.flush()
533-
expect(mutations).toEqual([
534-
{
535-
path: 'foo',
536-
method: 'set',
537-
args: ['bar2'],
538-
},
539-
{
540-
path: 'foo',
541-
method: 'set',
542-
args: ['bar3'],
543-
},
544-
])
535+
const flushResult = tree.flush()
536+
expect(flushResult).toEqual({
537+
flushId: 0,
538+
mutations: [
539+
{
540+
path: 'foo',
541+
method: 'set',
542+
args: ['bar2'],
543+
},
544+
{
545+
path: 'foo',
546+
method: 'set',
547+
args: ['bar3'],
548+
},
549+
],
550+
})
545551
})
546552
})
547553

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ProxyStateTree {
2323
paths: Set<string>[]
2424
status: STATUS
2525
proxy: any
26+
currentFlushId: number
2627
constructor(state: object, options: Options = {}) {
2728
if (!isPlainObject(state)) {
2829
throw new Error('You have to pass a plain object to the Proxy State Tree')
@@ -42,6 +43,7 @@ class ProxyStateTree {
4243
this.objectChanges = new Set()
4344
this.status = STATUS.IDLE
4445
this.proxy = proxify(this, state)
46+
this.currentFlushId = 0
4547
}
4648
get() {
4749
return this.proxy
@@ -54,6 +56,7 @@ class ProxyStateTree {
5456
flush() {
5557
const pathCallbacksToCall = new Set()
5658
const mutations = this.mutations.slice()
59+
const flushId = this.currentFlushId++
5760

5861
for (let objectChange of this.objectChanges) {
5962
if (this.pathDependencies[objectChange]) {
@@ -74,18 +77,21 @@ class ProxyStateTree {
7477
}
7578

7679
for (let callback of pathCallbacksToCall) {
77-
callback()
80+
callback(flushId)
7881
}
7982

8083
pathCallbacksToCall.clear()
8184
this.mutations.length = 0
8285
this.objectChanges.clear()
8386

8487
for (let globalDependency of this.globalDependencies) {
85-
globalDependency(mutations)
88+
globalDependency(mutations, flushId)
8689
}
8790

88-
return mutations
91+
return {
92+
mutations,
93+
flushId,
94+
}
8995
}
9096
startMutationTracking() {
9197
if (this.status !== STATUS.IDLE) {
@@ -131,8 +137,8 @@ class ProxyStateTree {
131137

132138
return pathSet
133139
}
134-
addMutationListener(cb: (mutations: Mutation[]) => void)
135-
addMutationListener(initialPaths: Set<string>, cb: () => void)
140+
addMutationListener(cb: (mutations: Mutation[], flushId: number) => void)
141+
addMutationListener(initialPaths: Set<string>, cb: (flushId: number) => void)
136142
addMutationListener() {
137143
if (arguments.length === 1) {
138144
const cb = arguments[0]

0 commit comments

Comments
 (0)