@@ -37,6 +37,52 @@ describe('TRACKING', () => {
3737 tree . clearPathsTracking ( trackId )
3838 expect ( hasCalledCb ) . toBe ( true )
3939 } )
40+ test ( 'should allow tracking by flush' , ( ) => {
41+ let reactionCount = 0
42+ const tree = new ProxyStateTree ( {
43+ foo : 'bar' ,
44+ } )
45+ const state = tree . get ( )
46+
47+ const trackId = tree . startPathsTracking ( )
48+ state . foo
49+ const paths = tree . clearPathsTracking ( trackId )
50+
51+ tree . addFlushListener ( paths , ( flushId ) => {
52+ reactionCount ++
53+ expect ( flushId ) . toBe ( 0 )
54+ } )
55+ tree . startMutationTracking ( )
56+ state . foo = 'bar2'
57+ tree . clearMutationTracking ( )
58+ tree . flush ( )
59+ expect ( reactionCount ) . toBe ( 1 )
60+ } )
61+ test ( 'should allow tracking by mutation' , ( ) => {
62+ let reactionCount = 0
63+ const tree = new ProxyStateTree ( {
64+ foo : 'bar' ,
65+ } )
66+ const state = tree . get ( )
67+
68+ const trackId = tree . startPathsTracking ( )
69+ state . foo
70+ tree . clearPathsTracking ( trackId )
71+
72+ tree . addMutationListener ( ( mutation , flushId ) => {
73+ reactionCount ++
74+ expect ( mutation ) . toEqual ( {
75+ method : 'set' ,
76+ path : 'foo' ,
77+ args : [ 'bar2' ] ,
78+ } )
79+ expect ( flushId ) . toBe ( 1 )
80+ } )
81+ tree . startMutationTracking ( )
82+ state . foo = 'bar2'
83+ tree . clearMutationTracking ( )
84+ expect ( reactionCount ) . toBe ( 1 )
85+ } )
4086} )
4187
4288describe ( 'OBJECTS' , ( ) => {
@@ -217,7 +263,7 @@ describe('ARRAYS', () => {
217263 }
218264
219265 if ( ! listener ) {
220- tree . addMutationListener ( paths , trackPaths )
266+ tree . addFlushListener ( paths , trackPaths )
221267 }
222268
223269 iterations ++
@@ -452,7 +498,7 @@ describe('FUNCTIONS', () => {
452498 } )
453499 const paths = tree . clearPathsTracking ( trackId )
454500
455- tree . addMutationListener ( paths , ( flushId ) => {
501+ tree . addFlushListener ( paths , ( flushId ) => {
456502 reactionCount ++
457503 expect ( flushId ) . toBe ( 0 )
458504 } )
@@ -475,7 +521,7 @@ describe('REACTIONS', () => {
475521 const trackId = tree . startPathsTracking ( )
476522 state . foo // eslint-disable-line
477523 const paths = tree . clearPathsTracking ( trackId )
478- tree . addMutationListener ( paths , ( flushId ) => {
524+ tree . addFlushListener ( paths , ( flushId ) => {
479525 reactionCount ++
480526 expect ( flushId ) . toBe ( 0 )
481527 } )
@@ -491,7 +537,7 @@ describe('REACTIONS', () => {
491537 foo : 'bar' ,
492538 } )
493539 const state = tree . get ( )
494- tree . addMutationListener ( ( mutations , flushId ) => {
540+ tree . addFlushListener ( ( mutations , flushId ) => {
495541 expect ( mutations ) . toEqual ( [
496542 {
497543 method : 'set' ,
@@ -518,7 +564,7 @@ describe('REACTIONS', () => {
518564 renderCount ++
519565 return tree . clearPathsTracking ( trackId )
520566 }
521- const listener = tree . addMutationListener ( render ( ) , ( ) => {
567+ const listener = tree . addFlushListener ( render ( ) , ( ) => {
522568 listener . update ( render ( ) )
523569 } )
524570 tree . startMutationTracking ( )
@@ -538,7 +584,7 @@ describe('REACTIONS', () => {
538584 state . foo // eslint-disable-line
539585 state . bar // eslint-disable-line
540586 const paths = tree . clearPathsTracking ( trackId )
541- tree . addMutationListener ( paths , ( ) => {
587+ tree . addFlushListener ( paths , ( ) => {
542588 reactionCount ++
543589 } )
544590 tree . startMutationTracking ( )
@@ -562,7 +608,7 @@ describe('REACTIONS', () => {
562608 }
563609 return tree . clearPathsTracking ( trackId )
564610 }
565- const listener = tree . addMutationListener ( render ( ) , ( ) => {
611+ const listener = tree . addFlushListener ( render ( ) , ( ) => {
566612 listener . update ( render ( ) )
567613 } )
568614 tree . startMutationTracking ( )
@@ -586,7 +632,7 @@ describe('REACTIONS', () => {
586632 }
587633 return tree . clearPathsTracking ( trackId )
588634 }
589- const listener = tree . addMutationListener ( render ( ) , ( ) => {
635+ const listener = tree . addFlushListener ( render ( ) , ( ) => {
590636 listener . dispose ( )
591637 } )
592638 tree . startMutationTracking ( )
@@ -679,7 +725,7 @@ describe('ITERATIONS', () => {
679725 if ( mutationListener ) {
680726 mutationListener . update ( paths )
681727 } else {
682- mutationListener = tree . addMutationListener ( paths , update )
728+ mutationListener = tree . addFlushListener ( paths , update )
683729 }
684730
685731 if ( runCount === 1 ) {
@@ -728,7 +774,7 @@ describe('ITERATIONS', () => {
728774 expect ( paths ) . toEqual (
729775 new Set ( [ 'items' , 'items.0' , 'items.0.title' , 'items.1' , 'items.1.title' ] )
730776 )
731- tree . addMutationListener ( paths , ( ) => {
777+ tree . addFlushListener ( paths , ( ) => {
732778 reactionCount ++
733779 } )
734780 tree . startMutationTracking ( )
@@ -758,7 +804,7 @@ describe('ITERATIONS', () => {
758804 expect ( paths ) . toEqual (
759805 new Set ( [ 'items' , 'items.0' , 'items.0.title' , 'items.1' , 'items.1.title' ] )
760806 )
761- tree . addMutationListener ( paths , ( ) => {
807+ tree . addFlushListener ( paths , ( ) => {
762808 reactionCount ++
763809 } )
764810 tree . startMutationTracking ( )
@@ -777,7 +823,7 @@ describe('ITERATIONS', () => {
777823 state . items . map ( ( item ) => item )
778824 const paths = tree . clearPathsTracking ( trackId )
779825 expect ( paths ) . toEqual ( new Set ( [ 'items' , 'items.0' , 'items.1' ] ) )
780- tree . addMutationListener ( paths , ( ) => {
826+ tree . addFlushListener ( paths , ( ) => {
781827 reactionCount ++
782828 } )
783829 tree . startMutationTracking ( )
@@ -825,7 +871,7 @@ describe('PRODUCTION', () => {
825871 }
826872 )
827873
828- tree . addMutationListener ( new Set ( [ 'items.0.title' ] ) , ( ) => { } )
874+ tree . addFlushListener ( new Set ( [ 'items.0.title' ] ) , ( ) => { } )
829875
830876 tree . startMutationTracking ( )
831877 tree . get ( ) . items [ 0 ] . title = 'foo1'
0 commit comments