File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed
packages/node_modules/proxy-state-tree/src Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff 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+
2742describe ( 'OBJECTS' , ( ) => {
2843 describe ( 'ACCESS' , ( ) => {
2944 test ( 'should create proxy when accessed' , ( ) => {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments