createReactiveStoreFromDataPublisher

function createReactiveStoreFromDataPublisher<TData>(
    config,
): ReactiveStore<TData>;

Returns a ReactiveStore given a data publisher.

The store will update its state with each message published to dataChannelName and notify all subscribers. When a message is published to errorChannelName, subscribers are notified so they can react to the error condition, but the last-known state is preserved. Triggering the abort signal disconnects the store from the data publisher.

Things to note:

  • getState() returns undefined until the first notification arrives.
  • On error, getState() continues to return the last known value and getError() returns the error. Only the first error is captured.
  • The function returned by subscribe is idempotent — calling it multiple times is safe.

Type Parameters

Type Parameter
TData

Parameters

ParameterTypeDescription
configConfig-

Returns

ReactiveStore<TData>

Example

const store = createReactiveStoreFromDataPublisher({
    abortSignal: AbortSignal.timeout(10_000),
    dataChannelName: 'notification',
    dataPublisher,
    errorChannelName: 'error',
});
const unsubscribe = store.subscribe(() => {
    console.log('State updated:', store.getState());
});

On this page