File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -75,7 +75,11 @@ export function recordEffectScope(
75
75
scope = scope || activeEffectScope
76
76
if ( scope && scope . active ) {
77
77
scope . effects . push ( effect )
78
+ return
78
79
}
80
+ // destory on parent component unmounted
81
+ const vm = getCurrentInstance ( ) ?. proxy
82
+ vm && vm . $on ( 'hook:destroyed' , ( ) => effect . stop ( ) )
79
83
}
80
84
81
85
export function effectScope ( detached ?: boolean ) {
Original file line number Diff line number Diff line change 8
8
computed ,
9
9
ref ,
10
10
ComputedRef ,
11
+ createApp ,
11
12
} from '../../../src'
12
13
import { mockWarn } from '../../helpers'
13
14
@@ -251,4 +252,34 @@ describe('reactivity/effect/scope', () => {
251
252
expect ( watchSpy ) . toHaveBeenCalledTimes ( 1 )
252
253
expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 2 )
253
254
} )
255
+
256
+ it ( 'should stop along with parent component' , async ( ) => {
257
+ let dummy , doubled
258
+ const counter = reactive ( { num : 0 } )
259
+
260
+ const root = document . createElement ( 'div' )
261
+ const vm = createApp ( {
262
+ setup ( ) {
263
+ const scope = new EffectScope ( )
264
+ scope . run ( ( ) => {
265
+ watchEffect ( ( ) => ( dummy = counter . num ) )
266
+ watchEffect ( ( ) => ( doubled = counter . num * 2 ) )
267
+ } )
268
+ } ,
269
+ } )
270
+ vm . mount ( root )
271
+
272
+ expect ( dummy ) . toBe ( 0 )
273
+ counter . num = 7
274
+ await nextTick ( )
275
+ expect ( dummy ) . toBe ( 7 )
276
+ expect ( doubled ) . toBe ( 14 )
277
+
278
+ vm . unmount ( )
279
+
280
+ counter . num = 6
281
+ await nextTick ( )
282
+ expect ( dummy ) . toBe ( 7 )
283
+ expect ( doubled ) . toBe ( 14 )
284
+ } )
254
285
} )
You can’t perform that action at this time.
0 commit comments