Projects / Reactive library / Wrap up
Wrap up
Keep going
Pick one when you want more:
- Ownership. Make effects created inside an effect dispose with their parent.
- Fix the leak. Let a computed with no observers unsubscribe from its sources, or hold observers with
WeakRef. - Async scheduling. Run effects in a microtask instead of synchronously, and compare the behaviour.
- Deep objects. Wrap an object in a
Proxyso that each property is its own signal. - Bind to the DOM. Make a tiny function that keeps a text node in sync with a signal.
- Remove the recursion. Mark and refresh with a loop so a chain of 100,000 does not overflow the stack.
What this version leaves out, and what a real library does
We kept the ideas and left out some weight.
- Real libraries avoid the memory leak from Step 2.4, with ownership trees or weak references.
- Ours runs effects synchronously at the end of a write. Many libraries run them in a microtask or hand scheduling to a framework.
- Errors: we let them escape. Real libraries decide whether one failing effect may stop the others.
- Deep dependency chains use loops, not recursion.
- A TC39 proposal for signals in JavaScript is at an early stage. Real APIs differ from ours in names and in detail, but the ideas are the same.
Check yourself
Answer each question in your head first, then open it to check.
Why does an effect unsubscribe from all its sources before each run?
The reads can depend on a condition. Clearing the subscriptions and collecting them again means the effect watches what it read this time. Without it, an effect would keep re-running for signals it no longer uses.In Step 1.4 the effect printed a name that never existed. What two changes made that go away, and why was the first not enough?
Queueing effects until all stale marks were placed, then running them, fixed the diamond case where two computed values share a source. It was not enough for two separate writes, which are still two updates.batchgroups them so that effects run once, after the last one.Why is a computed lazy, and what did laziness cost us in Step 2.4?
A computed that nobody reads does no work, and one that is read twice calculates once. The cost is that it stays subscribed to its sources, so unread computeds that are thrown away are never freed.In Step 3.4 the effect ran 11 times for 1,000 writes. How do version numbers make that possible?
Each node has a version that moves only when its value really changes. An effect remembers the versions it saw and runs only if one differs. Thebucketvalue changed 10 times in 1,000 writes, so the effect ran for the start plus 10 changes.Teach it back: explain why the loop guard counted runs per effect, and what found the problem.
A thousand different effects running once each is a normal update, not a loop. A loop is the same effect running again and again. The first guard counted all runs together, and the benchmark with 1,000 effects on one signal tripped it, though the tests had no case that large.
Sources
- TC39 proposal: Signals, the early proposal to add signals to the language.
- Super Charging Fine-Grained Reactive Performance, an article on the algorithms behind reactive libraries.
- Node.js: test runner, the
node:testmodule used in Step 4.2. - Node.js: Packages, what the
exportsfield means.