Must be an integer >= 1
Simple example:
function numbers(): ReadableChannel<number> {
const ch = new Channel<number>(0)
void (async () => {
for (let i = 0;; ++i) {
await ch.write(i)
}
})()
return ch
}
// This gives iterable:
// [0, 1, 2]
// [3, 4, 5]
// [6, 7, 8]
// ...
const groups = partitionTime(numbers(), 3, 1000)
Grouping with timeout:
function producer(): ReadableChannel<Something> {
// ...
}
async function saver(values: ReadableChannel<Something>) {
const batchedValues = partitionTime(values, 50, 10_000)
// Does not wait for longer than 10s if `producer()` produces
// less than 50 values
for await (const batch of batchedValues)) {
await insertInDb(batch)
}
}
Reads from
source
channel in groups of sizegroupSize
. However, if more thannextValueTimeoutMs
elapse since the last read fromsource
, yields an incomplete group (with length <groupSize
) earlyThe returned iterable is closed once
source
closes. If there is an incomplete group, it is yielded before closingNever yields empty arrays