StoreInstance<R extends string = string, S extends Record<R, any> = Record<string, any>, EM extends EventMapBase = EventMapBase>

interface
types.ts:368

Public Store surface.

The concrete Store implements this as StoreInstance<R, DeepReadonly<S>, EM>.

Type parameters

NameTypeDescription
Rextends stringReducer name union.
Sextends Record<R, any>State record (already readonly at the call site).
EMextends EventMapBaseEvent map.

Properties

NameTypeDescription
emitEmit<EM>Emit a typed event (channel, type, payload). Returns a promise that resolves when the event has been processed.
namestringStore name (used by DevTools to identify the instance).

Methods

connect

Fine-grained subscription: listen to a specific reducer.property path. Accepts a dotted path string (e.g., "data.123.title"). Fires when that path (or its ancestors) actually changes.

signature
connect(spec, handler): Unsubscribe

Fine-grained subscription: listen to a specific reducer.property path. Accepts a dotted path string (e.g., "data.123.title"). Fires when that path (or its ancestors) actually changes.

Parameters

spec — object with:

NameTypeDescription
propertystring
reducerR
NameTypeDescription
handler(change: Change) => voidHandler receiving a Change with { oldValue, newValue, path }.

Returns

Unsubscribe

dispose

Cleanup resources (timers, etc.) when disposing the store. Call this if you're dynamically creating/destroying stores.

signature
dispose(): void

Cleanup resources (timers, etc.) when disposing the store. Call this if you're dynamically creating/destroying stores.

Returns

void

getState

Read the full state (already readonly).

signature
getState(): DeepReadonly<S>

Read the full state (already readonly).

Returns

DeepReadonly<S>

hotReplace

Convenience API to replace any subset of store parts (HMR patterns).

signature
hotReplace(partial): void

Convenience API to replace any subset of store parts (HMR patterns).

Parameters

partial — object with:

NameTypeDescription
effects?EffectSpec<DeepReadonly<S>, EM>[]
middleware?MiddlewareFunction<DeepReadonly<S>, EM>[]
preserveState?boolean
reducer?Record<R, ReducerSpec<S[R], EM>>

Returns

void

instrument

Registers an instrumentation observer, called once per emitted event (committed or vetoed) after the synchronous reduce phase, with the exact changed paths, their old/new values, and reduce timing. This is the typed seam DevTools agents consume — no as any bridging required.

signature
instrument(observer): Unsubscribe

Registers an instrumentation observer, called once per emitted event (committed or vetoed) after the synchronous reduce phase, with the exact changed paths, their old/new values, and reduce timing. This is the typed seam DevTools agents consume — no as any bridging required.

Parameters

NameTypeDescription
observerInstrumentationObserver<EM>Receives an InstrumentedEvent per emit.

Returns

UnsubscribeUnsubscribe function.

onEffect

Convenience helper to register an **effect** filtered by a single (channel, type) pair.

signature
onEffect<C, T>(channel, type, handler): Unsubscribe

Convenience helper to register an **effect** filtered by a single (channel, type) pair.

Type parameters

NameTypeDescription
Cextends stringChannel key within EM.
Textends stringEvent type key within channel C.

Parameters

NameTypeDescription
channelCChannel to filter.
NameTypeDescription
typeTEvent type to filter.
NameTypeDescription
handler(payload: EM[C][T], getState: () => DeepReadonly<S>, emit: Emit<EM>, event: Event<EM, C, T>) => void | Promise<void>Effect handler (payload, getState, emit, event).

Returns

UnsubscribeUnsubscribe/teardown function.

onEvent

Subscribe to events by channel and type. Event subscriptions are intended for the View layer (e.g., React components) to react to events without affecting the event flow. They are fire-and-forget and cannot cancel event propagation. **Phases:** - 'committed' (default): Events that passed middleware and reached reducers - 'uncommitted': Events rejected by middleware - 'all': Both committed and uncommitted events (handler receives phase parameter)

signature
onEvent<C, T>(channel, type, handler, phase?): Unsubscribe

Subscribe to events by channel and type. Event subscriptions are intended for the View layer (e.g., React components) to react to events without affecting the event flow. They are fire-and-forget and cannot cancel event propagation. **Phases:** - 'committed' (default): Events that passed middleware and reached reducers - 'uncommitted': Events rejected by middleware - 'all': Both committed and uncommitted events (handler receives phase parameter)

Type parameters

NameTypeDescription
Cextends stringChannel key within EM.
Textends stringEvent type key within channel C.

Parameters

NameTypeDescription
channelCChannel to subscribe to.
NameTypeDescription
typeTEvent type to subscribe to.
NameTypeDescription
handlerNarrowedEventHandler<DeepReadonly<S>, EM, C, T>Handler function (event, getState, emit, phase).
NameTypeDescription
phase?EventPhaseEvent phase to subscribe to (default: 'committed').

Returns

UnsubscribeUnsubscribe function.

Example

example
const off = store.onEvent('ui', 'save', (event, getState, emit, phase) => {
  console.log('Save committed:', event.payload);
});

Example

example
store.onEvent('ui', 'delete', (event, getState, emit, phase) => {
  console.log('Delete was rejected by middleware');
}, 'uncommitted');

Example

example
store.onEvent('ui', 'action', (event, getState, emit, phase) => {
  console.log('Action:', phase); // 'committed' or 'uncommitted'
}, 'all');

registerEffect

Register a post-reducer effect (sees final state). Returns an unsubscribe.

signature
registerEffect(spec): Unsubscribe

Register a post-reducer effect (sees final state). Returns an unsubscribe.

Parameters

NameTypeDescription
specEffectSpec<DeepReadonly<S>, EM>

Returns

Unsubscribe

registerMiddleware

Dynamically add middleware.

signature
registerMiddleware(mw): Unsubscribe

Dynamically add middleware.

Parameters

NameTypeDescription
mwMiddlewareFunction<DeepReadonly<S>, EM>

Returns

Unsubscribe

registerReducer

Dynamically add/remove a namespaced reducer slice at runtime.

signature
registerReducer(name, spec): Unsubscribe

Dynamically add/remove a namespaced reducer slice at runtime.

Parameters

NameTypeDescription
namestring
NameTypeDescription
specReducerSpec<any, EM>

Returns

Unsubscribe

replaceEffects

Replaces all registered effects (HMR-friendly).

signature
replaceEffects(next): void

Replaces all registered effects (HMR-friendly).

Parameters

NameTypeDescription
nextEffectSpec<DeepReadonly<S>, EM>[]New effects array (as EffectSpecs).

Returns

void

replaceMiddleware

Replaces the entire middleware pipeline (HMR-friendly).

signature
replaceMiddleware(next): void

Replaces the entire middleware pipeline (HMR-friendly).

Parameters

NameTypeDescription
nextMiddlewareFunction<DeepReadonly<S>, EM>[]New middleware array.

Returns

void

replaceReducers

Replaces the entire reducer set (HMR-friendly).

signature
replaceReducers(next, opts?): void

Replaces the entire reducer set (HMR-friendly).

Parameters

NameTypeDescription
nextRecord<R, ReducerSpec<S[R], EM>>Map of slice specs keyed by slice name.

opts — object with:

NameTypeDescription
preserveState?boolean

Returns

void

subscribe

Coarse subscription: runs after any state change (once per committed event).

signature
subscribe(listener): Unsubscribe

Coarse subscription: runs after any state change (once per committed event).

Parameters

NameTypeDescription
listener() => void

Returns

Unsubscribe