UNPKG

datainout

Version:

Import and reports data

61 lines (59 loc) 1.56 kB
var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/common/core/RingPromise.ts import { Writable } from "stream"; var RingPromise = class { constructor(ringSize, task) { this.ring = []; this.index = 0; this.ring = new Array(ringSize).fill(Promise.resolve()); this.task = task; } run(...arg) { return __async(this, null, function* () { this.index = (this.index + 1) % this.ring.length; this.ring[this.index] = this.task(...arg); if (this.index === this.ring.length - 1) yield Promise.all(this.ring); }); } stream() { const that = this; return new Writable({ objectMode: true, write(arg, _encoding, callback) { return __async(this, null, function* () { try { yield that.run(...arg); } catch (err) { return callback(err); } callback(); }); }, final(callback) { Promise.all(that.ring).then(() => callback()).catch((err) => callback(err)); } }); } }; export { RingPromise };