silmaril

Stores

Reactivity is limited to $$. A store passes values across that boundary: to event emitters, observables, global state or another framework.

silmaril/store provides a small Store class. $store binds any store to a variable.

stores-counter.js

No logs.

The variable current holds the value of the store.

One-way and two-way

Declaration Direction
const x = $store(s); The store writes the variable.
let x = $store(s); The store writes the variable and back.

let registers a write-back. It runs once at registration, and again each time the variable changes, also when the change came from the store. It calls store.set(value) each time. A store that does not compare values sees the value it sent.

Store compares with Object.is and notifies only when the value changes.

The store interface

$store takes any object with this shape.

The Store class adds destroy(), which stops it from notifying.

Rules