File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -362,4 +362,21 @@ describe('reactivity/effect/scope', () => {
362
362
expect ( scope . effects . length ) . toBe ( 0 )
363
363
expect ( scope . cleanups . length ) . toBe ( 0 )
364
364
} )
365
+
366
+ test ( 'signal' , ( ) => {
367
+ const scope = effectScope ( )
368
+ // should not create an `AbortController` until `scope.signal` is accessed
369
+ expect ( ( scope as any ) . _controller ) . toBeUndefined ( )
370
+
371
+ const { signal } = scope
372
+ expect ( ( scope as any ) . _controller ) . toBeDefined ( )
373
+ expect ( signal ) . toBeDefined ( )
374
+
375
+ const spy = vi . fn ( )
376
+ signal . addEventListener ( 'abort' , spy )
377
+
378
+ scope . stop ( )
379
+ // should trigger `abort` on the `signal` when `scope.stop()` is called.
380
+ expect ( spy ) . toBeCalled ( )
381
+ } )
365
382
} )
Original file line number Diff line number Diff line change @@ -39,6 +39,16 @@ export class EffectScope {
39
39
* @internal
40
40
*/
41
41
private index : number | undefined
42
+ /**
43
+ * @internal
44
+ */
45
+ private _controller : AbortController | undefined
46
+
47
+ get signal ( ) : AbortSignal {
48
+ if ( ! this . _controller ) this . _controller = new AbortController ( )
49
+
50
+ return this . _controller . signal
51
+ }
42
52
43
53
constructor ( public detached = false ) {
44
54
this . parent = activeEffectScope
@@ -129,6 +139,9 @@ export class EffectScope {
129
139
stop ( fromParent ?: boolean ) : void {
130
140
if ( this . _active ) {
131
141
this . _active = false
142
+
143
+ if ( this . _controller ) this . _controller . abort ( )
144
+
132
145
let i , l
133
146
for ( i = 0 , l = this . effects . length ; i < l ; i ++ ) {
134
147
this . effects [ i ] . stop ( )
You can’t perform that action at this time.
0 commit comments