ReactiveStreamStore

type ReactiveStreamStore<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, getUnifiedState } contract.

Example

// React — the unified state snapshot has stable identity per update, making it suitable as
// the second argument to `useSyncExternalStore`.
const state = useSyncExternalStore(store.subscribe, store.getUnifiedState);
if (state.status === 'error') return <ErrorMessage error={state.error} onRetry={store.retry} />;
if (state.status === 'loading') return <Spinner />;
return <View data={state.data} />;

See

createReactiveStoreFromDataPublisherFactory

Type Parameters

Type Parameter
T

Methods

getError()

getError(): unknown;

Returns the error published to the error channel, or undefined if no error has occurred.

Returns

unknown

Deprecated

Use `getUnifiedState()` instead. This getter returns only the error field and cannot narrow the relationship between the current value, error, and status.


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

Deprecated

Use `getUnifiedState()` instead. This getter returns only the value field and does not surface lifecycle status (e.g. retrying).


getUnifiedState()

getUnifiedState(): ReactiveState<T>;

Returns the current lifecycle snapshot: { data, error, status }. The returned object has stable identity between state changes, making it safe to pass directly as the getSnapshot argument to React's useSyncExternalStore.

Returns

ReactiveState<T>

See

ReactiveState


retry()

retry(): void;

Re-opens the stream after an error. No-op when the store is not in the error state.

Returns

void


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