Skip to content
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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ function run(e: EffectNode): void {
e.flags = ReactiveFlags.Watching | ReactiveFlags.RecursedCheck;
const prevSub = setActiveSub(e);
try {
(e as EffectNode).fn();
e.fn();
} finally {
activeSub = prevSub;
e.flags &= ~ReactiveFlags.RecursedCheck;
purgeDeps(e);
}
} else {
} else if (e.flags) {
e.flags = ReactiveFlags.Watching;
}
}
Expand Down
31 changes: 10 additions & 21 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ export function createReactiveSystem({
}

function unlink(link: Link, sub = link.sub): Link | undefined {
const dep = link.dep;
const prevDep = link.prevDep;
const nextDep = link.nextDep;
const nextSub = link.nextSub;
const prevSub = link.prevSub;
const { dep, prevDep, nextDep, nextSub, prevSub } = link;
if (nextDep !== undefined) {
nextDep.prevDep = prevDep;
} else {
Expand Down Expand Up @@ -186,17 +182,15 @@ export function createReactiveSystem({
if (sub.flags & ReactiveFlags.Dirty) {
dirty = true;
} else if ((flags & (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) === (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) {
const subs = dep.subs;
if (update(dep)) {
const subs = dep.subs!;
if (subs.nextSub !== undefined) {
if (subs !== undefined && subs.nextSub !== undefined) {
shallowPropagate(subs);
}
dirty = true;
}
} else if ((flags & (ReactiveFlags.Mutable | ReactiveFlags.Pending)) === (ReactiveFlags.Mutable | ReactiveFlags.Pending)) {
if (link.nextSub !== undefined || link.prevSub !== undefined) {
stack = { value: link, prev: stack };
}
stack = { value: link, prev: stack };
link = dep.deps!;
sub = dep;
++checkDepth;
Expand All @@ -212,18 +206,13 @@ export function createReactiveSystem({
}

while (checkDepth--) {
const firstSub = sub.subs!;
const hasMultipleSubs = firstSub.nextSub !== undefined;
if (hasMultipleSubs) {
link = stack!.value;
stack = stack!.prev;
} else {
link = firstSub;
}
link = stack!.value;
stack = stack!.prev;
if (dirty) {
const subs = sub.subs;
if (update(sub)) {
if (hasMultipleSubs) {
shallowPropagate(firstSub);
if (subs !== undefined && subs.nextSub !== undefined) {
shallowPropagate(subs);
}
sub = link.sub;
continue;
Expand All @@ -240,7 +229,7 @@ export function createReactiveSystem({
}
}

return dirty;
return dirty && !!sub.flags;
} while (true);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/issue_109.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test } from 'vitest';
import { computed, effect, signal } from '../src';

test('#109', () => {
const s = signal(false);
let dispose!: () => void;
const a = computed(() => {
if (s()) dispose();
return 0;
});
const b = computed(() => a());
dispose = effect(() => { b(); });
s(true);
});
81 changes: 81 additions & 0 deletions tests/issue_109_edge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { expect, test } from 'vitest';
import { computed, effect, getActiveSub, signal } from '../src';
import { ReactiveFlags, type ReactiveNode } from '../src/system';

// (1) Side-effect after the dispose-trigger inside the effect body
// should still run, just like in any other JS function — disposing
// swaps the .fn property but the closure already on the stack must
// finish executing.
test('self-dispose inside effect: code after dispose() still runs', () => {
const s = signal(0);
let dispose!: () => void;
const stages: string[] = [];

dispose = effect(() => {
stages.push('start');
s();
if (s() === 1) {
dispose();
stages.push('after-dispose');
}
stages.push('end');
});

expect(stages).toEqual(['start', 'end']);
s(1);
expect(stages).toEqual([
'start', 'end', // initial run
'start', 'after-dispose', 'end', // re-run; the fn body must finish
]);
});

// (2) When dispose is triggered from *another* node's update (the
// #109 path), the swapped fn replaces the user fn before run() calls
// it — so the side-effect inside the user fn is never invoked.
// This documents that this run is *skipped*, not just suppressed.
test('disposed-by-other-node effect: scheduled run is skipped entirely', () => {
const s = signal(0);
let dispose!: () => void;
let bodyRuns = 0;

const a = computed(() => {
if (s() === 1) dispose();
return s();
});

dispose = effect(() => { a(); bodyRuns++; });
effect(() => { a(); });

expect(bodyRuns).toBe(1);
s(1);
// The flush queued e1 because of the propagation; if e1 had been
// disposed *between flushes*, this run wouldn't have been scheduled.
// The fix-by-fn-swap turns the scheduled run into a no-op.
expect(bodyRuns).toBe(1);
});

// (3) After being disposed mid-flush, the effect's graph state should
// be clean: no deps, not Watching, no subs — otherwise future
// propagations would still walk through it.
test('disposed effect: graph state is fully cleaned up', () => {
const s = signal(0);
let dispose!: () => void;
let e1Node: ReactiveNode | undefined;

const a = computed(() => {
if (s() === 1) dispose();
return s();
});

dispose = effect(() => {
e1Node ??= getActiveSub();
a();
});
effect(() => { a(); });

s(1);

expect(e1Node).toBeDefined();
expect(e1Node!.deps).toBeUndefined();
expect(e1Node!.flags & ReactiveFlags.Watching).toBe(0);
});
28 changes: 28 additions & 0 deletions tests/issue_109_leak.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, test } from 'vitest';
import { computed, effect, signal } from '../src';

test('disposed effect should not be re-notified on later updates', () => {
const s = signal(0);
let dispose1!: () => void;
let e1runs = 0;

const a = computed(() => {
if (s() === 1) dispose1();
return s();
});

dispose1 = effect(() => { a(); e1runs++; });
effect(() => { a(); });

expect(e1runs).toBe(1);
s(1);
expect(e1runs).toBe(1);

// If e1 was actually disposed, further signal changes shouldn't
// involve it at all. With the fn-swap fix, e1 stays subscribed to `a`
// and gets notified each time, even though fn is now a no-op.
s(2);
s(3);
s(4);
expect(e1runs).toBe(1);
});
23 changes: 23 additions & 0 deletions tests/issue_109_scope.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test } from 'vitest';
import { computed, effect, effectScope, signal } from '../src';

// Same shape as issue_109.spec.ts, but the dispose target is an
// effectScope instead of an effect. effectScopeOper has no defer
// branch in the current fix, so unwatched() cascades immediately and
// the original crash should still reproduce.
test('#109 scope-variant: dispose effectScope during computed update', () => {
const s = signal(false);
let disposeScope!: () => void;

const a = computed(() => {
if (s()) disposeScope();
return 0;
});
const b = computed(() => a());

disposeScope = effectScope(() => {
effect(() => { b(); });
});

s(true);
});
42 changes: 42 additions & 0 deletions tests/issue_109_shallow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from 'vitest';
import { computed, effect, signal } from '../src';

test('#109 shallowPropagate walks stale link after sibling disposal', () => {
const s = signal(0);
let dispose1!: () => void;
let e2Value = -1;

const a = computed(() => {
if (s() === 1) dispose1();
return s();
});

dispose1 = effect(() => { a(); });
effect(() => { e2Value = a(); });

expect(e2Value).toBe(0);
s(1);
expect(e2Value).toBe(1);
});

test('#109 shallowPropagate walks stale link with 3 subscribers', () => {
const s = signal(0);
let dispose1!: () => void;
let e2Value = -1;
let e3Value = -1;

const a = computed(() => {
if (s() === 1) dispose1();
return s();
});

dispose1 = effect(() => { a(); });
effect(() => { e2Value = a(); });
effect(() => { e3Value = a(); });

expect(e2Value).toBe(0);
expect(e3Value).toBe(0);
s(1);
expect(e2Value).toBe(1);
expect(e3Value).toBe(1);
});
20 changes: 20 additions & 0 deletions tests/issue_109_stale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from 'vitest';
import { computed, effect, signal } from '../src';

test('#109 stale-link: disposed effect still runs', () => {
const s = signal(0);
let dispose1!: () => void;
let e1runs = 0;

const a = computed(() => {
if (s() === 1) dispose1();
return s();
});

dispose1 = effect(() => { a(); e1runs++; });
effect(() => { a(); }); // second subscriber keeps `a` alive

expect(e1runs).toBe(1);
s(1);
expect(e1runs).toBe(1); // disposed during update(a); should not re-run
});
12 changes: 12 additions & 0 deletions tests/issue_109_variant.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from 'vitest';
import { computed, effect, signal } from '../src';

test('#109 variant - dep.subs undefined at line 190', () => {
const s = signal(0);
let dispose!: () => void;
const a = computed(() => (s(), 0)); // value never changes
const a2 = computed(() => (s() && dispose(), s())); // disposes mid-update, value changes
const b = computed(() => (a(), a2(), 0)); // reads a before a2
dispose = effect(() => { b(); });
s(1);
});
Loading