UNPKG

@thi.ng/transducers-async

Version:

Async versions of various highly composable transducers, reducers and iterators

21 lines (20 loc) 560 B
import { isReduced } from "@thi.ng/transducers/reduced"; import { ensureAsyncTransducer } from "./ensure.js"; import { push } from "./push.js"; const step = (tx, unwrap = true) => { const [_, complete, reduce] = ensureAsyncTransducer(tx)(push()); let done = false; return async (x) => { if (!done) { let acc = await reduce([], x); done = isReduced(acc); if (done) { acc = await complete(acc.deref()); } return acc.length === 1 && unwrap ? acc[0] : acc.length > 0 ? acc : void 0; } }; }; export { step };