File tree 2 files changed +25
-3
lines changed
2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -296,6 +296,19 @@ describe('reactivity/effect/scope', () => {
296
296
} )
297
297
} )
298
298
299
+ it ( 'calling on() and off() multiple times inside an active scope should not break currentScope' , ( ) => {
300
+ const parentScope = effectScope ( )
301
+ parentScope . run ( ( ) => {
302
+ const childScope = effectScope ( true )
303
+ childScope . on ( )
304
+ childScope . on ( )
305
+ childScope . off ( )
306
+ childScope . off ( )
307
+ childScope . off ( )
308
+ expect ( getCurrentScope ( ) ) . toBe ( parentScope )
309
+ } )
310
+ } )
311
+
299
312
it ( 'should pause/resume EffectScope' , async ( ) => {
300
313
const counter = reactive ( { num : 0 } )
301
314
const fnSpy = vi . fn ( ( ) => counter . num )
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
*/
@@ -105,16 +109,21 @@ export class EffectScope {
105
109
* @internal
106
110
*/
107
111
on ( ) : void {
108
- this . prevScope = activeEffectScope
109
- activeEffectScope = this
112
+ if ( ++ this . _on === 1 ) {
113
+ this . prevScope = activeEffectScope
114
+ activeEffectScope = this
115
+ }
110
116
}
111
117
112
118
/**
113
119
* This should only be called on non-detached scopes
114
120
* @internal
115
121
*/
116
122
off ( ) : void {
117
- activeEffectScope = this . prevScope
123
+ if ( this . _on > 0 && -- this . _on === 0 ) {
124
+ activeEffectScope = this . prevScope
125
+ this . prevScope = undefined
126
+ }
118
127
}
119
128
120
129
stop ( fromParent ?: boolean ) : void {
You can’t perform that action at this time.
0 commit comments