ReactiveStore

type ReactiveStore<T> = object;

A reactive store that holds the latest value published to a data channel and allows external systems to subscribe to changes. Compatible with useSyncExternalStore, Svelte stores, Solid's from(), and other reactive primitives that expect a { subscribe, getState } contract.

Example

// React — throw error from snapshot function to surface via Error Boundary
const state = useSyncExternalStore(store.subscribe, () => {
    if (store.getError()) throw store.getError();
    return store.getState();
});
 
// Vue — check error reactively in a composable
const data = shallowRef(store.getState());
const error = shallowRef(store.getError());
store.subscribe(() => {
    data.value = store.getState();
    error.value = store.getError();
});

See

createReactiveStoreFromDataPublisher

Type Parameters

Type Parameter
T

Methods

getError()

getError(): unknown;

Returns the error published to the error channel, or undefined if no error has occurred. Once set, the error is preserved — subsequent errors do not overwrite it.

Returns

unknown


getState()

getState(): T | undefined;

Returns the most recent value published to the data channel, or undefined if no notification has arrived yet. On error, continues to return the last known value.

Returns

T | undefined


subscribe()

subscribe(callback): () => void;

Registers a callback to be called whenever the state changes or an error is received. Returns an unsubscribe function. Safe to call multiple times.

Parameters

ParameterType
callback() => void

Returns

(): void;
Returns

void

On this page