Skip to content

Commit a944234

Browse files
committed
Make sure effects run on reattach
Also handled situation where teardown was ran multiple times when reattaching. Fixes #58
1 parent 82d61c3 commit a944234

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/create-effect.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ function createEffect(setEffects: (state: State, cb: Callable) => void) {
4040
this._teardown = this.callback.call(this.state);
4141
}
4242

43-
teardown(): void {
43+
teardown(disconnected?: boolean): void {
4444
if (typeof this._teardown === "function") {
4545
this._teardown();
46+
// ensure effect is not torn down multiple times
47+
this._teardown = undefined;
48+
}
49+
50+
// reset to pristine state when element is disconnected
51+
if (disconnected) {
52+
this.lastValues = this.values = undefined;
4653
}
4754
}
4855

src/hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class Hook<P extends unknown[] = unknown[], R = unknown, H = unknown> {
1212
}
1313

1414
abstract update(...args: P): R;
15-
teardown?(): void;
15+
teardown?(disconnected?: boolean): void;
1616
}
1717

1818
interface CustomHook<

src/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class State<H = unknown> {
5555
let hooks = this[hookSymbol];
5656
hooks.forEach((hook) => {
5757
if (typeof hook.teardown === "function") {
58-
hook.teardown();
58+
hook.teardown(true);
5959
}
6060
});
6161
}

0 commit comments

Comments
 (0)