UNPKG

kafka-console

Version:
120 lines (119 loc) 4.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CancelToken = void 0; exports.createItem = createItem; const evnty_1 = __importDefault(require("evnty")); function createItem() { const result = {}; result.promise = new Promise((resolve, reject) => { result.resolve = resolve; result.reject = reject; }); return result; } class CancelToken { constructor(timeout) { this.timeout = timeout; } } exports.CancelToken = CancelToken; class Pool { constructor(values = [], { skip = 0, count = Infinity, timeout } = {}) { this.pool = [createItem()]; this.doneEvent = (0, evnty_1.default)(); this.index = 0; this.skip = skip; this.count = count + skip; if (typeof timeout === 'number') { this.timerId = setTimeout(() => this.done(true), timeout); } values.forEach((value) => this.push(value)); } push(value) { if (this.index >= this.skip && this.index < this.count) { this.pool[this.pool.length - 1].resolve(value); this.pool.push(createItem()); } this.index++; if (this.index >= this.count) { this.done(); } return this; } done(timeout = false) { this.pool[this.pool.length - 1].resolve(new CancelToken(timeout)); return this; } onDone(callback) { this.doneEvent.on(callback); return this; } toArray() { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; const result = []; try { for (var _d = true, _e = __asyncValues(this), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; let item = _c; result.push(item); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return result; }); } [Symbol.asyncIterator]() { if (this.count === 0) { Promise.resolve().then(() => this.done()); } let i = 0; return { i, next: () => __awaiter(this, void 0, void 0, function* () { const value = yield this.pool[0].promise; this.pool.shift(); if (value instanceof CancelToken) { clearTimeout(this.timerId); this.doneEvent(value.timeout); return { i: ++i, done: true, }; } return { i: ++i, value, done: false, }; }), }; } } exports.default = Pool;