Skip to content
Open
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
13 changes: 10 additions & 3 deletions signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ export class Signal extends EventTarget {
}

effect(fn) {
fn();
this.addEventListener('change', fn);
return () => this.removeEventListener('change', fn);
const controller = new AbortController()
const cleanup = () => controller.abort()

fn(cleanup)
this.addEventListener(
'change',
() => fn(cleanup), { signal: controller.signal }
)

return cleanup
}

valueOf () { return this.#value; }
Expand Down