createLatencyProbe
projects/core/utils/latency-probe.ts
Description#
Measures how long the previous busy window lasted, from a boolean-busy source.
The duration is a wall-clock sample taken at the busy edge - not derivable
from signals alone - so the probe writes lastDuration imperatively from an
effect. This is a measurement side effect: a distinct write-in-effect
pattern, not the same mechanism as createVisibilityGate (which defers its
visible write via setTimeout) nor the CngxAsyncContainer exception. It is
loop-safe by construction: the effect tracks only busy(), reads the clock in
untracked(), and never reads lastDuration back.
Aggregate semantics: the probe measures the busy-envelope. On a
false->true edge it stamps startedAt; on the matching true->false edge it
records lastDuration = clock() - startedAt. Across concurrent operations
behind one boolean source (e.g. CngxAsyncRegistry.isAnythingLoading) that is
the whole in-flight window, not any single operation.
Observation invariant: the clock is stamped at the busy transition at
effect-flush granularity, independent of whether or when lastDuration is
read. A busy window that opens and closes within a single synchronous tick,
before change detection flushes the effect, is not measured - real registry
transitions cross async boundaries (macrotasks), so the effect flushes between
them.
Must be called in an injection context: the internal effect auto-cleans on
destroy, mirroring createVisibilityGate.
Signature#
createLatencyProbe(busy, now?)Parameters#
A boolean-busy source (e.g. state.isBusy or
() => registry?.isAnythingLoading() ?? false).
Injectable clock, default performance.now() (monotonic; correct
for durations, unaffected by wall-clock jumps). Inject a plain counter in
specs for deterministic edge timestamps.
Returns#
CngxLatencyProbe { lastDuration, isBusy }.