Skip to content

feat: support effect cleanup functions - #113

Merged
johnsoncodehk merged 1 commit into
masterfrom
feat/effect-cleanup
May 12, 2026
Merged

johnsoncodehk merged 1 commit into
masterfrom
feat/effect-cleanup

Conversation

@johnsoncodehk

@johnsoncodehk johnsoncodehk commented May 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Effect callbacks can now return a cleanup function that the runtime calls before each re-run and on disposal
  • Cleanup runs outside the reactive tracking context (reads in cleanup don't create dependencies)
  • If cleanup calls dispose, the pending re-run is skipped

Details

Three touch points in src/index.ts:

  1. effect() — captures fn() return value as cleanup
  2. run() — calls cleanup (with activeSub = undefined) before re-executing, bails out if cleanup triggered dispose
  3. effectOper() (dispose) — calls cleanup before tearing down the node
const dispose = effect(() => {
  const id = setInterval(() => console.log(a()), 1000);
  return () => clearInterval(id);
});
// a changes → clearInterval(old), setInterval(new)
// dispose()  → clearInterval(current)

Test plan

  • All 58 existing alien-signals tests pass
  • Cross-framework test suite: cleanup tests #38, #39, #40, #89, #109, #110, #111, #120, #140 now pass

Effect callbacks can now return a cleanup function. The cleanup runs:
- Before each re-run (outside reactive tracking context)
- On disposal

If cleanup calls dispose, the effect is not re-run.
@johnsoncodehk
johnsoncodehk merged commit cf75b33 into master May 12, 2026
2 checks passed
@johnsoncodehk
johnsoncodehk deleted the feat/effect-cleanup branch May 12, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant