Skip to content

Commit 5794d19

Browse files
authored
Merge pull request #7 from solidjs/b
fix the cleanup bug
2 parents 40bddae + 2a12c67 commit 5794d19

5 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎packages/solid-signals/src/bubble-reactivity/effect.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Computation, type MemoOptions } from "./core";
1+
import { Computation, UNCHANGED, type MemoOptions } from "./core";
22
import { STATE_CLEAN, STATE_DISPOSED } from "./constants";
33
import type { Owner } from "./owner";
44
import { handleError } from "./owner";
55

66
let scheduledEffects = false;
77
let runningEffects = false;
8+
let renderEffects: RenderEffect[] = [];
89
let effects: Effect[] = [];
910

1011
/**
@@ -53,6 +54,17 @@ function runEffects() {
5354
runningEffects = true;
5455

5556
try {
57+
for (let i = 0; i < renderEffects.length; i++) {
58+
if (renderEffects[i]._state !== STATE_CLEAN) {
59+
renderEffects[i]._updateIfNecessary();
60+
}
61+
}
62+
for (let i = 0; i < renderEffects.length; i++) {
63+
if (renderEffects[i].modified) {
64+
renderEffects[i].effect(renderEffects[i]._value);
65+
renderEffects[i].modified = false;
66+
}
67+
}
5668
for (let i = 0; i < effects.length; i++) {
5769
if (effects[i]._state !== STATE_CLEAN) {
5870
runTop(effects[i]);
@@ -98,3 +110,42 @@ export class Effect<T = any> extends Computation<T> {
98110
handleError(this, error);
99111
}
100112
}
113+
114+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
115+
export class RenderEffect<T = any> extends Computation<T> {
116+
effect: (val: T) => void;
117+
modified: boolean = false;
118+
constructor(
119+
compute: () => T,
120+
effect: (val: T) => void,
121+
options?: MemoOptions<T>,
122+
) {
123+
super(undefined, compute, options);
124+
125+
this.effect = effect;
126+
renderEffects.push(this);
127+
// this._updateIfNecessary()
128+
}
129+
130+
override _notify(state: number): void {
131+
if (this._state >= state) return;
132+
133+
if (this._state === STATE_CLEAN) {
134+
renderEffects.push(this);
135+
if (!scheduledEffects) flushEffects();
136+
}
137+
138+
this._state = state;
139+
}
140+
141+
override write(value: T): T {
142+
this._value = value;
143+
this.modified = true;
144+
145+
return value;
146+
}
147+
148+
override _setError(error: unknown): void {
149+
handleError(this, error);
150+
}
151+
}

‎packages/solid-signals/src/bubble-reactivity/owner.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export class Owner {
7979
dispose(this: Owner, self = true): void {
8080
if (this._state === STATE_DISPOSED) return;
8181

82-
const head = self ? this._prevSibling : this;
8382
let current = this._nextSibling as Computation | null;
8483

8584
while (current && current._parent === this) {
@@ -88,13 +87,14 @@ export class Owner {
8887
current = current._nextSibling as Computation;
8988
}
9089

90+
const head = self ? this._prevSibling : this;
9191
if (self) this._disposeNode();
92-
if (current) current._prevSibling = !self ? this : this._prevSibling;
92+
else if (current) current._prevSibling = this._prevSibling;
9393
if (head) head._nextSibling = current;
9494
}
9595

9696
_disposeNode(): void {
97-
if (this._prevSibling) this._prevSibling._nextSibling = null;
97+
if (this._nextSibling) this._nextSibling._prevSibling = this._prevSibling;
9898
this._parent = null;
9999
this._prevSibling = null;
100100
this._context = null;

‎packages/solid-signals/src/reactivity.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { MemoOptions, SignalOptions } from "./bubble-reactivity/core";
22
import { Computation, compute, UNCHANGED } from "./bubble-reactivity/core";
3-
import { Effect } from "./bubble-reactivity/effect";
3+
import { Effect, RenderEffect } from "./bubble-reactivity/effect";
44
import { ERROR_BIT, LOADING_BIT } from "./bubble-reactivity/flags";
55
import { handleError, HANDLER, Owner } from "./bubble-reactivity/owner";
66

@@ -108,6 +108,18 @@ export function createEffect<T>(
108108
);
109109
}
110110

111+
export function createRenderEffect<T>(
112+
compute: () => T,
113+
effect: (val: T) => void,
114+
options?: { name?: string },
115+
): void {
116+
void new RenderEffect(
117+
compute,
118+
effect,
119+
__DEV__ ? { name: options?.name ?? "effect" } : undefined,
120+
);
121+
}
122+
111123
/**
112124
* Creates a computation root which is given a `dispose()` function to dispose of all inner
113125
* computations.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createSignal, createRenderEffect } from "../src";
2+
3+
it("should work with baisc renderEffects", () => {
4+
const [a, setA] = createSignal(0);
5+
createRenderEffect(
6+
() => a(),
7+
(a) => {
8+
console.log(a);
9+
},
10+
);
11+
});

‎packages/solid-signals/tests/onCleanup.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,44 @@ it("should clean up in reverse order", () => {
7777
expect(disposeA).toHaveBeenCalledWith(2);
7878
expect(disposeParent).toHaveBeenCalledWith(3);
7979
});
80+
81+
it("should dispose all roots", () => {
82+
const disposals: string[] = [];
83+
84+
const dispose = createRoot((dispose) => {
85+
createRoot(() => {
86+
onCleanup(() => disposals.push("SUBTREE 1"));
87+
createEffect(() => onCleanup(() => disposals.push("+A1")));
88+
createEffect(() => onCleanup(() => disposals.push("+B1")));
89+
createEffect(() => onCleanup(() => disposals.push("+C1")));
90+
});
91+
92+
createRoot(() => {
93+
onCleanup(() => disposals.push("SUBTREE 2"));
94+
createEffect(() => onCleanup(() => disposals.push("+A2")));
95+
createEffect(() => onCleanup(() => disposals.push("+B2")));
96+
createEffect(() => onCleanup(() => disposals.push("+C2")));
97+
});
98+
99+
onCleanup(() => disposals.push("ROOT"));
100+
101+
return dispose;
102+
});
103+
104+
flushSync();
105+
dispose();
106+
107+
expect(disposals).toMatchInlineSnapshot(`
108+
[
109+
"+C2",
110+
"+B2",
111+
"+A2",
112+
"SUBTREE 2",
113+
"+C1",
114+
"+B1",
115+
"+A1",
116+
"SUBTREE 1",
117+
"ROOT",
118+
]
119+
`);
120+
});

0 commit comments

Comments
 (0)