fw-03 step 02 — computed, cleanup, and the diamond

Goal

The two hardest correctness problems in reactivity: lazy caching, and stale dependencies.

Tasks

  1. computed(fn)lazy (not evaluated until read), cached while dependencies are unchanged, and both a dependent and a dependency.
  2. Cleanup. Clear an effect's dependencies before each re-run. Then remove the cleanup and reproduce stage 5's dependency leak: an effect that stopped reading a still firing on a.
  3. Nested effects. Replace your stack with a single global and reproduce stage 6 — the inner effect stealing the outer's dependencies.
  4. The diamond / glitch test. Build a → b → d, a → c → d. Count how many times d evaluates, and check whether it ever computes from inconsistent inputs. Most hand-written systems have this bug and never notice.
  5. watch(source, cb) reporting old and new values.

Done when

  • Stages 4–6 green
  • Dependency leak reproduced deliberately, then fixed
  • d evaluates exactly once per change, never from a transient value