Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disposing currently running effects #170

Merged
merged 3 commits into from
Sep 18, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/twelve-suits-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals-core": minor
---

Allow disposing a currently running effect
44 changes: 26 additions & 18 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ function getValue<T>(signal: Signal<T>): T {
_flags: 0,
_version: 0,
_source: signal,
_prevSource: undefined,
_nextSource: evalContext._sources,
_target: evalContext,
_prevTarget: undefined,
_nextTarget: undefined,
_rollbackNode: node,
};
evalContext._sources = node;
Expand Down Expand Up @@ -492,17 +495,31 @@ export function computed<T>(compute: () => T): Computed<T> {
}
export type { Computed as ReadonlySignal };

function disposeEffect(effect: Effect) {
for (
let node = effect._sources;
node !== undefined;
node = node._nextSource
) {
node._source._unsubscribe(node);
}
disposeNestedEffects(effect);
effect._sources = undefined;
effect._flags |= DISPOSED;
}

function endEffect(this: Effect, prevContext?: Computed | Effect) {
if (evalContext !== this) {
throw new Error("Out-of-order effect");
}

cleanupSources(this);

evalContext = prevContext;
endBatch();

this._flags &= ~RUNNING;
if (this._flags & DISPOSED) {
disposeEffect(this);
}
}

class Effect {
Expand All @@ -515,6 +532,11 @@ class Effect {

constructor(compute: () => void) {
this._compute = compute;

if (evalContext !== undefined) {
this._nextNestedEffect = evalContext._effects;
evalContext._effects = this;
}
}

_callback() {
Expand All @@ -536,10 +558,6 @@ class Effect {

/*@__INLINE__**/ startBatch();
const prevContext = evalContext;
if (prevContext !== undefined) {
this._nextNestedEffect = prevContext._effects;
prevContext._effects = this;
}
evalContext = this;

prepareSources(this);
Expand All @@ -555,19 +573,9 @@ class Effect {
}

_dispose() {
if (this._flags & RUNNING) {
throw new Error("Effect still running");
if (!(this._flags & RUNNING)) {
disposeEffect(this);
}
for (
let node = this._sources;
node !== undefined;
node = node._nextSource
) {
node._source._unsubscribe(node);
}
disposeNestedEffects(this);
this._sources = undefined;
this._flags |= DISPOSED;
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/core/test/signal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,20 @@ describe("effect()", () => {
expect(() => dispose()).to.throw;
});

it("should throw when disposing a running effect", () => {
it("should allow disposing a running effect", () => {
const a = signal(0);
const spy = sinon.spy();
const dispose = effect(() => {
if (a.value === 1) {
dispose();
spy();
}
});
expect(() => {
a.value = 1;
}).to.throw("Effect still running");
expect(spy).not.to.be.called;
a.value = 1;
expect(spy).to.be.calledOnce;
a.value = 2;
expect(spy).to.be.calledOnce;
});

it("should not run if it's first been triggered and then disposed in a batch", () => {
Expand Down