Projects / Reactive library
JavaScript · about 10 hours · 4 phases
Build a signals library in JavaScript
Picture a shopping cart on a web page. The price changes, so the total must change, so the label on the button must change. Written by hand, that is a chain of "and then update this" calls, and sooner or later one is forgotten and the screen shows a stale number. Or everything is redrawn on every change, which is slow. Signals fix this: you describe what depends on what, and the library works out the rest. In about 10 hours you will build a small signals library, test it, and package it.
You work in four phases of about 2.5 hours each, and every phase ends with something you can run. By the end you will have:
signal,computedandeffect, which find out for themselves what they depend on,- updates that run each effect once and always see consistent values, even when several things change,
- batching, cleanup and a way to skip work when a computed value comes out the same,
- a tested package that you install from another folder, plus a benchmark of how fast it is.
You only need basic JavaScript: functions, classes and modules. Anything more is explained when it first shows up.
The four phases
Notice what was read
You end with an effect that re-runs exactly when something it read changes, stops watching what it no longer reads, and a visible glitch that Phase 2 will fix.
- 1.1 A box that remembers who looked30 min
- 1.2 An effect that follows its reads40 min
- 1.3 Watch only what you read40 min
- 1.4 Stop watching, and meet the glitch40 min
Derived values and calm updates
You end with computed values that are lazy and cached, and effects that run once per update with consistent values, even when two derived values share one source.
- 2.1 Computed values50 min
- 2.2 Effects that wait their turn40 min
- 2.3 The diamond30 min
- 2.4 Conditions inside computeds, and a leak30 min
Do less, control more
You end with batch, effects that skip work when a computed value did not really change, untracked, and effect cleanup. One experiment goes from 1,001 effect runs to 11.
- 3.1 Batch writes35 min
- 3.2 Skip work when nothing really changed55 min
- 3.3 Untracked reads and cleanup30 min
- 3.4 Count the savings30 min
Make it a package
You end with a package with tests and a benchmark that you install from another folder and use.
- 4.1 A guard against loops30 min
- 4.2 Tests40 min
- 4.3 A benchmark that finds a bug40 min
- 4.4 Package it40 min