File tree 2 files changed +41
-2
lines changed
2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,10 @@ export class EffectScope {
8
8
* @internal
9
9
*/
10
10
private _active = true
11
+ /**
12
+ * @internal track `on` calls, allow `on` call multiple times
13
+ */
14
+ private _on = 0
11
15
/**
12
16
* @internal
13
17
*/
@@ -99,20 +103,27 @@ export class EffectScope {
99
103
}
100
104
}
101
105
106
+ prevScope : EffectScope | undefined
102
107
/**
103
108
* This should only be called on non-detached scopes
104
109
* @internal
105
110
*/
106
111
on ( ) : void {
107
- activeEffectScope = this
112
+ if ( ++ this . _on === 1 ) {
113
+ this . prevScope = activeEffectScope
114
+ activeEffectScope = this
115
+ }
108
116
}
109
117
110
118
/**
111
119
* This should only be called on non-detached scopes
112
120
* @internal
113
121
*/
114
122
off ( ) : void {
115
- activeEffectScope = this . parent
123
+ if ( this . _on > 0 && -- this . _on === 0 ) {
124
+ activeEffectScope = this . prevScope
125
+ this . prevScope = undefined
126
+ }
116
127
}
117
128
118
129
stop ( fromParent ?: boolean ) : void {
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ import {
31
31
TrackOpTypes ,
32
32
TriggerOpTypes ,
33
33
effectScope ,
34
+ onScopeDispose ,
34
35
shallowReactive ,
35
36
shallowRef ,
36
37
toRef ,
@@ -1982,4 +1983,31 @@ describe('api: watch', () => {
1982
1983
expect ( spy1 ) . toHaveBeenCalled ( )
1983
1984
expect ( spy2 ) . toHaveBeenCalled ( )
1984
1985
} )
1986
+
1987
+ // #12631
1988
+ test ( 'this.$watch w/ onScopeDispose' , ( ) => {
1989
+ const onCleanup = vi . fn ( )
1990
+ const toggle = ref ( true )
1991
+
1992
+ const Comp = defineComponent ( {
1993
+ render ( ) { } ,
1994
+ created ( this : any ) {
1995
+ this . $watch (
1996
+ ( ) => 1 ,
1997
+ function ( ) { } ,
1998
+ )
1999
+ onScopeDispose ( onCleanup )
2000
+ } ,
2001
+ } )
2002
+
2003
+ const App = defineComponent ( {
2004
+ render ( ) {
2005
+ return toggle . value ? h ( Comp ) : null
2006
+ } ,
2007
+ } )
2008
+
2009
+ const root = nodeOps . createElement ( 'div' )
2010
+ createApp ( App ) . mount ( root )
2011
+ expect ( onCleanup ) . toBeCalledTimes ( 0 )
2012
+ } )
1985
2013
} )
You can’t perform that action at this time.
0 commit comments