createForwardedAsyncState
projects/common/data/async-state/forwarded-async-state.ts
Description#
Wraps a changing source of async state in a stable CngxAsyncState façade.
A component that receives its state through an Input cannot publish that
object through CNGX_STATEFUL directly: the token is resolved once when a
bridge injects it, while the input can be rebound or absent. Capturing the
object would hand the bridge a stale state - or undefined before the
first binding.
This returns one object whose every member is a computed() that reads the
source on each access, so bridges observe transitions of whatever is bound
right now. With nothing bound it reports a quiet idle state rather than
throwing, which is what lets a bridge sit inside a timeline that has not
been given a state yet.
Contrast the select family, which provides CNGX_STATEFUL with
useFactory over a concrete commitState field - no forwarding needed
there, because the state is created by the component rather than passed in.
The split is not about the component, it is about where the state is born:
created locally, capture it; arriving through an Input, forward it.
readonly asyncState = createForwardedAsyncState(this.state);
// providers:
{ provide: CNGX_STATEFUL, useFactory: () => ({ state: inject(Host).asyncState }) }