import type { Awaitable } from "./Awaitable"; /** return undefined to skip emit */ export const reduces: { ( state: S, fn: (state: S, x: T, i: number) => Awaitable ): TransformStream; ( fn: (state: T | null, x: T, i: number) => Awaitable ): TransformStream; } = (...args: any[]) => { const fn = typeof args[1] === "function" ? args[1] : typeof args[0] === "function" ? args[0] : null; let state = typeof args[1] === "function" ? args[0] : null; let i = 0; return new TransformStream({ transform: async (chunk, ctrl) => { const next = await fn(state, chunk, i++); if (undefined !== next) return ctrl.enqueue((state = next)); }, }); };