Wado

WEP: Reactive Signals

Context

UI frameworks have converged on fine-grained reactivity. A signal holds a value, a computation that reads it becomes its dependent, and a write reruns only the dependents. The page is built once, with no virtual DOM and no diffing of a tree.

This WEP first proposed signals as a language feature: a reactive keyword on bindings, &reactive references, and a compiler pass that found each computation's dependencies statically. example/web-signals then built signals as a library in the language as it is: signal, memo, observe, scope and flush, bindings to the DOM, a keyed list, and a to-do list on top of them. It needed no language feature. So the question this WEP answers changed from how the compiler tracks dependencies to whether it should.

Prior Art

Solid, Preact Signals, Vue and Svelte 5 find dependencies as the code runs. A computation records itself as the one running, and a signal that is read subscribes it. Dependencies follow control flow, and a signal is a value like any other: it crosses function and module boundaries, and lives in a list or a field.

Svelte 3 and 4 found dependencies at compile time. What their users complained about is the useful half:

Svelte 5 moved to signals tracked at run time. Solid's compiler never tracked dependencies. It compiles templates, which is where its speed comes from.

Decision

Signals Are a Library

Signals are a library that finds dependencies as the code runs. The language gains no reactive keyword, no &reactive reference, and no static dependency analysis. reactive is an identifier like any other.

Static analysis sees a graph only where it is closed within one function. The to-do list already leaves that: each item carries a signal of its own, created as items arrive, kept in a list, and read by a row built in another function. That part needs a graph at run time whatever the compiler does. A compile-time graph would then be a second mechanism beside it, for the one case that is cheap already. It would also subscribe a computation to every signal its text mentions, on whichever branch.

The language stays smaller, and what the library needs from it is general: closures, effect handlers, and resources.

Model

Reading reports to the running computation through an effect interface, whose operations have default bodies. A computation installs a handler while it runs. Outside one the default body runs, so a read is untracked, and the function that reads needs no handler above it.

Updates

A write only marks. A memo that is marked forgets its value and marks its own dependents, and an effect that is marked joins a queue. flush then reruns each queued effect once, until none is marked, and a memo recomputes when one of them reads it.

So a write performs no effect, and several writes are one batch. An effect never sees half of a batch, and never sees one memo updated beside another that is stale, since a memo recomputes on the read that needs it.

A listener bound with on flushes when it returns. Every entry into a program from the browser is a listener, so with on the page is consistent again before the browser gets control back. Any other code that writes, a listener added with add_event_listener directly included, calls flush itself.

Templates

Turning markup into the calls that build a page once and bind its holes is where a compiler helps a signals library most. That belongs to the markup dialect (WEP: Markup Dialect), a generator, and not to the language.

Known gaps