iter-tools-es
Version:
The iterable toolbox
32 lines (26 loc) • 757 B
JavaScript
const {
asyncIterableCurry
} = require('../../internal/async-iterable.js');
const {
CircularBuffer,
ReadOnlyCircularBuffer
} = require('../../internal/circular-buffer.js');
async function* __asyncWindow(source, size) {
const buffer = new CircularBuffer(size);
const bufferReadProxy = new ReadOnlyCircularBuffer(buffer);
for await (const value of source) {
buffer.push(value);
if (buffer.isFull()) {
yield bufferReadProxy;
}
}
}
exports.__asyncWindow = __asyncWindow;
const asyncWindow = /*#__PURE__*/asyncIterableCurry(__asyncWindow, {
validateArgs(args) {
if (typeof args[1] !== 'number') {
throw new Error(`${'asyncWindow'} must be passed a numeric size`);
}
}
});
exports.asyncWindow = asyncWindow;