Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ describe('reactivity/effect/scope', () => {
expect(getEffectsCount(scope)).toBe(0)
expect(scope.cleanupsLength).toBe(0)
})

test('signal', () => {
const scope = effectScope()
// should not create an `AbortController` until `scope.signal` is accessed
expect((scope as any)._controller).toBeUndefined()

const { signal } = scope
expect((scope as any)._controller).toBeDefined()
expect(signal).toBeDefined()

const spy = vi.fn()
signal.addEventListener('abort', spy)

scope.stop()
// should trigger `abort` on the `signal` when `scope.stop()` is called.
expect(spy).toBeCalled()
})
})

function getEffectsCount(scope: EffectScope): number {
Expand Down
13 changes: 13 additions & 0 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export class EffectScope implements ReactiveNode {
* @internal
*/
cleanupsLength = 0
/**
* @internal
*/
private _controller: AbortController | undefined

get signal(): AbortSignal {
if (!this._controller) this._controller = new AbortController()

return this._controller.signal
}

constructor(detached = false) {
if (!detached && activeEffectScope) {
Expand Down Expand Up @@ -88,6 +98,9 @@ export class EffectScope implements ReactiveNode {
unlink(sub)
}
cleanup(this)
if (this._controller) {
this._controller.abort()
}
}
}

Expand Down
Loading