When building or extending algorithmic pipelines (streaming, reduction, conditional processing), ensure two correctness points:
1) Resolve dynamic conditions before acting
resolveEnabled) rather than treating it as a static flag.2) Define the reducer’s initial accumulator/seed
reduce semantics: either require/provide an explicit initial value, or implement the same “no initial value uses the first element as the seed” behavior.Example (pattern):
// 1) Resolve a dynamic predicate before selecting work
const shouldProcess = (query: any) =>
query.state.status === 'error' &&
query.getObserversCount() > 0 &&
query.observers.some((observer: any) =>
resolveEnabled(observer.options.enabled, query), // resolve callback-based enabled
)
// 2) Seed a reducer deterministically
function reduceChunks<TChunk, TAcc>(chunks: TChunk[], reducer: (acc: TAcc, c: TChunk) => TAcc, initial: TAcc) {
let acc = initial
for (const chunk of chunks) acc = reducer(acc, chunk)
return acc
}
Apply this standard to prevent “first-step” surprises (wrong/empty accumulator) and to avoid silently skipping or incorrectly processing work due to unresolved callback-based conditions.
Enter the URL of a public GitHub repository