Skip to content

Latest commit

 

History

History
85 lines (73 loc) · 4.88 KB

STORE.md

File metadata and controls

85 lines (73 loc) · 4.88 KB

State stores

webfront::store::Store sends one browser snapshots of a C++ value; @webfront/store receives them in the page, for any UI library. The package is optional, installed by wf from an npm-compatible registry, has no dependency and is not an embedded core resource. Its adapters and support policy are in its README.

wf create store (a developer environment built from these sources; not yet in a published one) writes an interface without a UI library, with the C++ side in the comment at the top of ui/main.ts. Until the package is published to a registry, the starter carries a copy of its files instead of depending on it:

wf create store
wf init
wf dev ui/main.ts

Store

Install the browser receiver before asking C++ to send:

import {createStore} from '@webfront/store';
const statistics = createStore('statistics', [0, 'Waiting']);
statistics.subscribe(() => render(statistics.get()));        // after each change
statistics.readable.subscribe(value => render(value));       // at once, then after each change

In a server-context callback, construct and retain a store for that browser:

#include <webfront/components/store/Store.hpp>
using Snapshot = std::tuple<std::uint32_t, std::string>;
webfront::store::Store<UI, Snapshot> statistics{ui, "statistics"};
statistics.set({1, "Running"});
statistics.set({2, "Running"}); // replaces the pending snapshot

Copies share pending state. Moving a store transfers its wrapper: the source's valid() returns false, and set, flush, close and onError throw std::runtime_error on that moved-from wrapper. The first set schedules an application-owned task, which waits 1 ms on the server context. Only the last complete snapshot is sent. A burst of 1,000 set calls in one callback produces one bridge message and one React commit in the browser regression. This is a server cadence, not a rendering deadline: writes in later cycles can cause additional commits. Different stores have independent cycles; fields requiring an atomic update belong in one tuple. flush() queues the latest snapshot early, without awaiting the browser.

T must be a supported bridge value: scalar, string, tuple, numeric typed array or packed bitset. Tuples arrive as arrays. Ordinary JavaScript objects do not cross the bridge. Snapshot identity is stable until a message arrives; treat snapshots, including nested arrays and typed arrays, as immutable. Subscribe functions stay stable and return an unsubscribe function. An identical snapshot by Object.is does not notify. Store names contain ASCII letters, digits, _ or -. The browser namespace webfrontReactStores is reserved for these receivers (it keeps the name it had when React was the only consumer). A throwing subscriber is logged to the browser console and does not interrupt the remaining subscribers; the adapter contains diagnostic logging failures too.

The C++ store shares its connection's document lifetime and thread rules, even when no island is used. It does not require dom.connect() to send data. Create and mutate it on the server context. Disconnect, document rejection and application shutdown invalidate it; the timer is owned and drained by the application. valid() returns false off that thread. Retained stores are safe to inspect after the application has been destroyed; they do not extend the lifetime of UI. close() discards pending values, releases the installed error callback and invalidates all copies. Synchronous errors from set/flush throw. An explicit flush() encoding/send failure retains the pending snapshot and leaves the store open for retry (including the scheduled cycle). Only a successful send clears that pending snapshot. An automatic encoding/send failure closes the store and calls onError(std::exception_ptr) if installed. The automatic failure consumes and clears that callback before invoking it, so a callback capturing a store copy does not keep a closed store alive. Self-owning callbacks still require an explicit close() on the normal path; prefer weak captures when that lifecycle cannot be guaranteed. Exceptions thrown by onError are caught and logged; they cannot escape the automatic-send coroutine. Browser function failures are not acknowledged by this untyped, fire-and-forget C++ call.

Create only one C++ store per browser/name and share its copies; two independently constructed stores do not coalesce together. Browser createStore rejects duplicate names. dispose() removes the receiver and subscribers; coordinate it with C++ close() after stopping writers. Store disposal is explicit, independent of a component's StrictMode cleanup. Unmount all consumers before disposal: subscribing to a disposed store throws, including a later useSyncExternalStore resubscription while rendering a mounted consumer. Optional {target: window} installs a receiver in another window; the default is globalThis.