@ipp/cli
Version:
An image build orchestrator for the modern web
45 lines (44 loc) • 1.57 kB
JavaScript
;
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buffer = void 0;
const denque_1 = __importDefault(require("denque"));
const events_1 = require("events");
const object_stream_1 = require("../object_stream");
function buffer(count) {
return (source) => (0, object_stream_1.createObjectStream)((async function* () {
let ended = false;
const events = new events_1.EventEmitter();
const queue = new denque_1.default();
// Ready the source asynchronously and fill the buffer
(async function () {
for await (const item of source) {
while (buffer.length === count) {
await (0, events_1.once)(events, "item");
}
queue.push(item);
events.emit("item");
}
ended = true;
events.emit("item");
})();
// Yield items from the buffer
while (!queue.isEmpty() || !ended) {
if (queue.isEmpty()) {
await (0, events_1.once)(events, "item");
}
while (!queue.isEmpty()) {
yield queue.shift();
}
}
})());
}
exports.buffer = buffer;