createAggregateAsyncState
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):
- any source
error->error - else any source
loading/pending->loading - else any source
refreshing->refreshing - else all sources
success->success - else ->
idle(empty list, or any remainingidle)
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).