vUX / Store / v4.1.0

One store.
Every view in step.

The badge, the product cards, the cart and the totals below are four separate components. None of them calls another. They all follow one Store, and a single set() moves every one that cares.

const cart = new Store({ items: [], currency: "NGN" })  ·  cart.bind(instance, select)  ·  cart.set({ ... })
kiln.store
    store.get()no set() yet
    
                
    subscribe(fn): every change
    
                

    Watch the pill: set() returns the fields that actually changed, computed ones included. Pick the currency that is already selected and it returns nothing, so nobody is told.

    01Who hears what

    A subscriber names the fields it follows. A change reaches it only when one of those fields moved, and changed lists only those. Tick the boxes to change what each one watches (it re-subscribes), then fire the buttons.

    a separate store: { a, b, c } + computed sumready

    The fourth subscriber watches sum, a computed field. It hears a and b changes through it, never c.

    02batch(): many sets, one announcement

    Ten separate set() calls tell subscribers ten times. Inside batch() the store changes at once (a get() inside sees every step), but subscribers are told once, with every field that moved.

    ten separate set() calls
    0notifications
    the same ten, inside batch()
    0notifications

    03A subscriber that sets the store

    Subscriber A converts celsius into fahrenheit with its own set(). That change is queued, not announced mid-round, so B still hears the first change with the state A was given. A second round then announces fahrenheit. The last button wires a subscriber that never settles, and the store stops it after 100 rounds.

    roundscelsius 20 · fahrenheit 68
    
                

    04Bindings and teardown

    bind() runs its select once at once, then on each change to the fields it watches. Destroy the widget and its binding drops on the next change instead of throwing. The log counts every select run, so you can see it stop.

    the widgetunbound
    select() runs

    set({ other }) changes a field the binding does not watch, so select does not run at all. That is what the fields argument buys.

    05What it refuses to do

    Every button below really misuses the API. The text is the Error exactly as thrown, and none of these calls changes the store.

    pick a case