@@ -11,7 +11,12 @@ import {
11
11
readonly ,
12
12
ReactiveEffectRunner
13
13
} from '../src/index'
14
- import { ITERATE_KEY , pauseScheduling , resetScheduling } from '../src/effect'
14
+ import {
15
+ ITERATE_KEY ,
16
+ getDepFromReactive ,
17
+ pauseScheduling ,
18
+ resetScheduling
19
+ } from '../src/effect'
15
20
16
21
describe ( 'reactivity/effect' , ( ) => {
17
22
it ( 'should run the passed function once (wrapped by a effect)' , ( ) => {
@@ -993,4 +998,69 @@ describe('reactivity/effect', () => {
993
998
resetScheduling ( )
994
999
expect ( counterSpy ) . toHaveBeenCalledTimes ( 1 )
995
1000
} )
1001
+
1002
+ describe ( 'empty dep cleanup' , ( ) => {
1003
+ it ( 'should remove the dep when the effect is stopped' , ( ) => {
1004
+ const obj = reactive ( { prop : 1 } )
1005
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1006
+ const runner = effect ( ( ) => obj . prop )
1007
+ const dep = getDepFromReactive ( toRaw ( obj ) , 'prop' )
1008
+ expect ( dep ) . toHaveLength ( 1 )
1009
+ obj . prop = 2
1010
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBe ( dep )
1011
+ expect ( dep ) . toHaveLength ( 1 )
1012
+ debugger
1013
+ stop ( runner )
1014
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1015
+ obj . prop = 3
1016
+ runner ( )
1017
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1018
+ } )
1019
+
1020
+ it ( 'should only remove the dep when the last effect is stopped' , ( ) => {
1021
+ const obj = reactive ( { prop : 1 } )
1022
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1023
+ const runner1 = effect ( ( ) => obj . prop )
1024
+ const dep = getDepFromReactive ( toRaw ( obj ) , 'prop' )
1025
+ expect ( dep ) . toHaveLength ( 1 )
1026
+ const runner2 = effect ( ( ) => obj . prop )
1027
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBe ( dep )
1028
+ expect ( dep ) . toHaveLength ( 2 )
1029
+ obj . prop = 2
1030
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBe ( dep )
1031
+ expect ( dep ) . toHaveLength ( 2 )
1032
+ stop ( runner1 )
1033
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBe ( dep )
1034
+ expect ( dep ) . toHaveLength ( 1 )
1035
+ obj . prop = 3
1036
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBe ( dep )
1037
+ expect ( dep ) . toHaveLength ( 1 )
1038
+ stop ( runner2 )
1039
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1040
+ obj . prop = 4
1041
+ runner1 ( )
1042
+ runner2 ( )
1043
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1044
+ } )
1045
+
1046
+ it ( 'should remove the dep when it is no longer used by the effect' , ( ) => {
1047
+ const obj = reactive < { a : number ; b : number ; c : 'a' | 'b' } > ( {
1048
+ a : 1 ,
1049
+ b : 2 ,
1050
+ c : 'a'
1051
+ } )
1052
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'prop' ) ) . toBeUndefined ( )
1053
+ effect ( ( ) => obj [ obj . c ] )
1054
+ const depC = getDepFromReactive ( toRaw ( obj ) , 'c' )
1055
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'a' ) ) . toHaveLength ( 1 )
1056
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'b' ) ) . toBeUndefined ( )
1057
+ expect ( depC ) . toHaveLength ( 1 )
1058
+ obj . c = 'b'
1059
+ obj . a = 4
1060
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'a' ) ) . toBeUndefined ( )
1061
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'b' ) ) . toHaveLength ( 1 )
1062
+ expect ( getDepFromReactive ( toRaw ( obj ) , 'c' ) ) . toBe ( depC )
1063
+ expect ( depC ) . toHaveLength ( 1 )
1064
+ } )
1065
+ } )
996
1066
} )
0 commit comments