Threading contract
Every Ducky store follows the rules below. The same text is in the XML docs (remarks) of IDispatcher, so your IDE shows it on that interface.
Dispatch,DispatchAsync,Restore,Select,State,InitializeAsyncandWhenIdleAsyncmay be called from any thread at any time, including from inside a reducer, a middleware hook, a subscriber or an effect, with two exceptions forWhenIdleAsync: from a non-LongRunningeffect run of the same store it throwsInvalidOperationException(the run counts itself as running, so the wait could never end; another store's run is an unrelated caller, §6.5), and awaited from a middleware'sInitializeAsyncit waits untilInitTimeout(idle requires initialization). None of them ever blocks waiting for another thread, except that concurrent first users wait for the one-time construction of effects and middleware (whose constructors must not block).- Actions are reduced one at a time, never concurrently, in the order in which they were enqueued, with one exception: user actions dispatched before initialization are replayed after
StoreInitialized, behind library actions enqueued later (rule 5). Actions from one thread keep their relative order. - If no drain is active, the calling thread becomes the drainer and reduces the whole queue inline on its own thread and synchronization context, including actions enqueued by other threads, before its call returns. Otherwise the call enqueues and returns immediately. A synchronous
Dispatchgives read-your-writes only when the caller became the drainer. Useawait DispatchAsync(a)for read-after-write. It completes afterais reduced, not after its effects. UseWhenIdleAsync()(orTestStore.Settled()) to also wait for effects. DispatchAsyncnever faults or cancels, and never completes inline on the drainer. At run time no member throws for a reducer, middleware or effect failure. The only exceptions are programmer errors, thrown synchronously, before any task exists:ArgumentNullException(a null argument); the store's cachedDuckyConfigurationException(effects or middleware could not be constructed, DUCKY353);ArgumentOutOfRangeExceptionfromRestorewith a non-restore origin;InvalidOperationExceptionfrom a constructor that uses the store during materialization; and misuse,DuckyConfigurationExceptionDUCKY351 (aSliceStoreused before the store attached it),InvalidOperationExceptionDUCKY352 (Selectafter a component's first render) andInvalidOperationExceptionfromWhenIdleAsync(orTestStore.Settled) called inside a non-LongRunningeffect run (WhenIdle_FromNonLongRunningEffectRun_ThrowsNotHangs). Never block on it (.Result,.Wait(),GetAwaiter().GetResult()): if the caller is the drainer, that is a self-deadlock. DUCKY010 flags it.- Until the store is initialized, user actions are buffered and replayed in order after
StoreInitialized. Initialization starts automatically on the first dispatch, select,Stateread or idle wait. - Reducers, middleware hooks, subscriber callbacks,
SelectonChangecallbacks and the synchronous part of effects run on the draining thread. On Blazor Server that may be a thread-pool thread. Ducky.Blazor's components marshal to the renderer themselves. Custom callbacks must marshal UI work throughInvokeAsync. - Reducers,
SliceStoremutators and selector projectors must be pure. Projectors may run concurrently on two threads. - A dispatch or restore made while its parent action is being processed (synchronously) is a child with depth + 1, whatever its origin. Chains deeper than
MaxDispatchDepth(64) are dropped withReducerFailed(DispatchLoopException). A dispatch made after an effect has really yielded, or while the store is idle, starts a new chain. Statereads take no lock and always return a complete, immutable snapshot.- After disposal begins, dispatches are ignored and complete with
Disposed, and effect tokens are cancelled.
Known liveness ceilings
Two limits are known and accepted in 2.0. Each names the change that would lift it.
- the drainer drains other producers' actions with no bound, so on Blazor Server a renderer-thread caller that became the drainer can be held for as long as a hot background producer (a fast poller, a reactive stream) keeps the queue non-empty. "Never blocks" holds; latency does not. Upgrade path: a per-drain action budget that hands the rest to a thread-pool drainer.
- the depth guard bounds linear synchronous loops only. A synchronous fan-out (each action dispatches two children) grows the FIFO queue as 2^depth and exhausts memory long before depth 64. Upgrade path: a per-correlation cap on synchronous-chain actions that drops with
DispatchLoopException.