/** like buffer, but collect item[] in interval (ms) */ export function chunkIntervals(interval?: number) { let chunks: T[] = []; let id: null | ReturnType = null; return new TransformStream({ start: (ctrl) => { if (interval) id = setInterval(() => ctrl.enqueue(chunks), interval); }, transform: async (chunk, ctrl) => { if (!interval) ctrl.enqueue([chunk]); chunks.push(chunk); }, flush: async (ctrl) => { if (chunks.length) ctrl.enqueue(chunks); id !== null && clearInterval(id); }, }); }