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

interface
types.ts:1050

extends StoreDecoration<R, S, EM>

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.
isReplayingbooleantrue while devtools is applying a snapshot or replaying events.
namestringStore name (used by DevTools to identify the instance).
signalAbortSignalAborted when the store is disposed. Created on first read; already aborted when read after disposal. Tie work that should live exactly as long as the store to it.

Methods

call

Sends a request and waits for the reply, correlating the two automatically.

signature
call<C, T>(channel, type, payload, opts): CallHandle<EventUnion<EM>, EventUnion<EM>>

Sends a request and waits for the reply, correlating the two automatically.

Type parameters

NameTypeDescription
Cextends string
Textends string

Parameters

NameTypeDescription
channelC
NameTypeDescription
typeT
NameTypeDescription
payloadEM[C][T]
NameTypeDescription
optsCallOptions<EM>

Returns

CallHandle<EventUnion<EM>, EventUnion<EM>>

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, options?): 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 }.
NameTypeDescription
options?ConnectOptions

Returns

Unsubscribe

dispose

Releases the store. Afterwards it is inert: emit() resolves { committed: false } without running anything, call() rejects with CallAbortedError (as does every pending call), and registration methods throw. StoreInstance.signal aborts last. Idempotent.

signature
dispose(): void

Releases the store. Afterwards it is inert: emit() resolves { committed: false } without running anything, call() rejects with CallAbortedError (as does every pending call), and registration methods throw. StoreInstance.signal aborts last. Idempotent.

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?MiddlewareInput<DeepReadonly<S>, EM>[]
preserveState?boolean
reducer?ReducerReplacement<R, S, EM>
scope?ReplaceScope

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, options?): 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.
NameTypeDescription
options?InstrumentOptions

Returns

Unsubscribe, Unsubscribe function.

instrumentEffects

Observes the effect phase: once every effect for an event has settled, reports how long each took, whether it failed, and what it is called.

signature
instrumentEffects(observer, options?): Unsubscribe

Observes the effect phase: once every effect for an event has settled, reports how long each took, whether it failed, and what it is called.

Parameters

NameTypeDescription
observerEffectsObserver<EM>Called once per event whose effects ran.
NameTypeDescription
options?InstrumentOptions

Returns

Unsubscribe, Unsubscribe function.

metrics

The store's current load: queue depth, effects in flight, deduplication. Cheap enough to read on every scrape of a metrics endpoint.

signature
metrics(): StoreMetrics

The store's current load: queue depth, effects in flight, deduplication. Cheap enough to read on every scrape of a metrics endpoint.

Returns

StoreMetrics

onDiagnostic

Observes the store's diagnostics: the failures it contained, its refusals and its development warnings, as Diagnostics.

signature
onDiagnostic(observer): Unsubscribe

Observes the store's diagnostics: the failures it contained, its refusals and its development warnings, as Diagnostics.

Parameters

NameTypeDescription
observerDiagnosticSinkCalled once per diagnostic.

Returns

Unsubscribe, Unsubscribe 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>, ctx: EffectContext) => void | Promise<void>Effect handler (payload, getState, emit, event).

Returns

Unsubscribe, Unsubscribe/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 - 'written': Events that actually changed state - 'all': Both committed and uncommitted events (handler receives phase parameter). Deliberately not written as well: an event that writes is also committed, so folding it in would notify every existing all subscriber twice for one event.

signature
onEvent<C, T>(channel, type, handler, phase?, options?): 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 - 'written': Events that actually changed state - 'all': Both committed and uncommitted events (handler receives phase parameter). Deliberately not written as well: an event that writes is also committed, so folding it in would notify every existing all subscriber twice for one event.

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').

options, object with:

NameTypeDescription
duringReplay?booleanAlso call this handler while devtools is replaying, which it does not by default.

Returns

Unsubscribe, Unsubscribe 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');

onRegistrationChange

Called when the store gains or loses a reducer, middleware or effect.

signature
onRegistrationChange(observer, options?): Unsubscribe

Called when the store gains or loses a reducer, middleware or effect.

Parameters

NameTypeDescription
observerRegistrationObserver<EM>Receives one batch per registration change.

options, object with:

NameTypeDescription
emitCurrent?boolean

Returns

Unsubscribe, Unsubscribe function.

registerEffect

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

signature
registerEffect<Spec>(spec): Unsubscribe & { store: DecoratableStore<R, S, Merge<EM, EMAddOf<Spec>>>; dispose: unknown }

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

Type parameters

NameTypeDescription
Specextends EffectSpec<any, any>

Parameters

NameTypeDescription
specSpec

Returns

Unsubscribe & { store: DecoratableStore<R, S, Merge<EM, EMAddOf<Spec>>>; dispose: unknown }

registerMiddleware

Dynamically add middleware, in either the function or the spec form.

signature
registerMiddleware<M>(mw): Unsubscribe & { store: DecoratableStore<R, S, Merge<EM, EMAddOf<M>>>; dispose: unknown }

Dynamically add middleware, in either the function or the spec form.

Type parameters

NameTypeDescription
Mextends MiddlewareInput<any, any>

Parameters

NameTypeDescription
mwM

Returns

Unsubscribe & { store: DecoratableStore<R, S, Merge<EM, EMAddOf<M>>>; dispose: unknown }

registerReducer

Dynamically add/remove a namespaced reducer slice at runtime.

signature
registerReducer<N, Spec>(name, spec, options?): Unsubscribe & { store: WidenedSlice<R, S, EM, N, Spec>; dispose: unknown }

Dynamically add/remove a namespaced reducer slice at runtime.

Type parameters

NameTypeDescription
Nextends string
Specextends ReducerSpec<any, any>

Parameters

NameTypeDescription
nameN
NameTypeDescription
specSpec

options, object with:

NameTypeDescription
owner?string

Returns

Unsubscribe & { store: WidenedSlice<R, S, EM, N, Spec>; dispose: unknown }

registerSlice

Mounts a slice and hands back both the widened store and a disposer. The disposer is **library-private**: after it runs, the widened type still promises a slice that is gone. Application code should take StoreDecoration.withSlice instead, which returns no disposer at all.

signature
registerSlice<N, Spec>(name, spec, options?): Unsubscribe & { store: WidenedSlice<R, S, EM, N, Spec>; dispose: unknown }

Mounts a slice and hands back both the widened store and a disposer. The disposer is **library-private**: after it runs, the widened type still promises a slice that is gone. Application code should take StoreDecoration.withSlice instead, which returns no disposer at all.

Type parameters

NameTypeDescription
Nextends string
Specextends ReducerSpec<any, any>

Parameters

NameTypeDescription
nameN
NameTypeDescription
specSpec

options, object with:

NameTypeDescription
owner?string

Returns

Unsubscribe & { store: WidenedSlice<R, S, EM, N, Spec>; dispose: unknown }

replaceEffects

Replaces all registered effects (HMR-friendly).

signature
replaceEffects(next, opts?): void

Replaces all registered effects (HMR-friendly).

Parameters

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

opts, object with:

NameTypeDescription
scope?ReplaceScope

Returns

void

replaceMiddleware

Replaces the entire middleware pipeline (HMR-friendly).

signature
replaceMiddleware(next, opts?): void

Replaces the entire middleware pipeline (HMR-friendly).

Parameters

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

opts, object with:

NameTypeDescription
scope?ReplaceScope

Returns

void

replaceReducers

Replaces the entire reducer set (HMR-friendly).

signature
replaceReducers(next, opts?): void

Replaces the entire reducer set (HMR-friendly).

Parameters

NameTypeDescription
nextReducerReplacement<R, S, EM>Slice specs keyed by slice name, each typed with its own slice's state. Every key is optional: an omitted slice this call owns is removed, and an omitted slice mounted at runtime is kept. See ReducerReplacement.

opts, object with:

NameTypeDescription
preserveState?boolean
scope?ReplaceScope

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

whenIdle

Resolves when the store is idle: no event waiting to be reduced and no effect running.

signature
whenIdle(): Promise<void>

Resolves when the store is idle: no event waiting to be reduced and no effect running.

Returns

Promise<void>

withEffect

Registers an effect and returns the store widened by whatever event map it declares.

signature
withEffect<Spec>(spec): DecoratableStore<R, S, Merge<EM, EMAddOf<Spec>>>

Registers an effect and returns the store widened by whatever event map it declares.

Type parameters

NameTypeDescription
Specextends EffectSpec<any, any>

Parameters

NameTypeDescription
specSpec

Returns

DecoratableStore<R, S, Merge<EM, EMAddOf<Spec>>>

withMiddleware

Registers middleware and returns the store widened by whatever event map it declares. Only the **spec form** can widen: MiddlewareFunction's event parameter is EventUnion<EM>, a mapped type TypeScript cannot infer EM back out of. A bare function therefore contributes {}.

signature
withMiddleware<M>(mw): DecoratableStore<R, S, Merge<EM, EMAddOf<M>>>

Registers middleware and returns the store widened by whatever event map it declares. Only the **spec form** can widen: MiddlewareFunction's event parameter is EventUnion<EM>, a mapped type TypeScript cannot infer EM back out of. A bare function therefore contributes {}.

Type parameters

NameTypeDescription
Mextends MiddlewareInput<any, any>

Parameters

NameTypeDescription
mwM

Returns

DecoratableStore<R, S, Merge<EM, EMAddOf<M>>>

withSlice

Mounts a slice and returns the widened store, for chaining.

signature
withSlice<N, Spec>(name, spec, options?): WidenedSlice<R, S, EM, N, Spec>

Mounts a slice and returns the widened store, for chaining.

Type parameters

NameTypeDescription
Nextends string
Specextends ReducerSpec<any, any>

Parameters

NameTypeDescription
nameN
NameTypeDescription
specSpec

options, object with:

NameTypeDescription
owner?string

Returns

WidenedSlice<R, S, EM, N, Spec>