Skip to main content
cngx-src documentation

createAggregateAsyncState

Functioncore/utils/async-statev0.1.0

projects/core/utils/aggregate-async-state.ts

Description#

Aggregate N independent CngxAsyncStates into one derived CngxAsyncState.

Pure, keyless, and injection-context-free - it reuses buildAsyncStateView so every derived flag (isLoading, isSettled, hasData, ...) stays single-source-consistent with every other producer. The result is a CngxAsyncState, so it flows through cngx-async-container, the transition bridges, and every other consumer unchanged.

Combined status follows a fixed priority rule (first match wins):

  1. any source error -> error
  2. else any source loading/pending -> loading
  3. else any source refreshing -> refreshing
  4. else all sources success -> success
  5. else -> idle (empty list, or any remaining idle)

data is the per-source data() values in input order; each element is T | undefined because a source carries no data until it reaches success. error is the first error in input order (raw, for the single-error bridge path). Emptiness is the aggregate rule - empty only when it has at least one source and every source is itself empty - which the data shape cannot infer (an N-element array is never length 0), so it is supplied explicitly.

Two view fields are constant by construction: rule 2 collapses pending into loading, so the combined status is never 'pending' and isPending is always false; and progress is always undefined because per-source progress values have no comparable scale to aggregate over (a determinate upload percentage and an indeterminate fetch cannot be averaged honestly).

Signature#

createAggregateAsyncState(sources: Signal)

Parameters#

@paramsourcesSignal

Returns#

CngxAsyncState<(T | undefined)[]>