UNPKG

streaming

Version:

Transforms and other streaming helpers

53 lines (52 loc) 2.16 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Batcher = void 0; var stream_1 = require("stream"); /** Batcher transforms a stream of <T> into a stream of Array<T>, where each array has at most `size` elements (the last chunk may have fewer, but more than 0). */ var Batcher = /** @class */ (function (_super) { __extends(Batcher, _super); function Batcher(batchSize) { var _this = _super.call(this, { objectMode: true }) || this; _this.batchSize = batchSize; _this.batchBuffer = []; return _this; } /** checkFlush is called by both _transform and _flush, with different `end` values. */ Batcher.prototype.checkFlush = function (end, callback) { if (this.batchBuffer.length >= this.batchSize || (this.batchBuffer.length > 0 && end)) { // splice(index, number_to_remove, number_to_insert) returns the removed items var batch = this.batchBuffer.splice(0, this.batchSize); this.push(batch); } callback(); }; Batcher.prototype._transform = function (chunk, encoding, callback) { this.batchBuffer.push(chunk); this.checkFlush(false, callback); }; Batcher.prototype._flush = function (callback) { this.checkFlush(true, callback); }; return Batcher; }(stream_1.Transform)); exports.Batcher = Batcher;