UNPKG

datainout

Version:

Import and reports data

237 lines (233 loc) 6.74 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/reporters/SheetMeta.ts var SheetMeta = class { constructor(opts) { this.byJobIndex = true; this.sheetMetas = {}; this.mapSheetByJobIndex = {}; this.currentSheetName = 0; this.rowCount = 0; this.sheetNames = []; this.isCompleted = false; this.jobCount = 0; var _a; for (let i = 0; i < opts.length; i++) if (opts[i].jobs || opts[i].maxRow) this.addSheetMeta(opts[i].name, (_a = opts[i].jobs) != null ? _a : opts[i].maxRow); } // private addSheetMeta(name: string, jobIndexes: number[]): this; // private addSheetMeta(name: string, rowCount: number): this; addSheetMeta(name, data) { if (typeof data === "number") { this.byJobIndex = false; this.sheetMetas[name] = { isCompleted: false, rowCount: 0, maxRow: data }; } else if (Array.isArray(data)) { this.byJobIndex = true; this.sheetMetas[name] = { isCompleted: false, rowCount: 0, jobCount: data.length, maxRow: 0 }; this.jobCount += data.length; data.forEach((e) => { this.mapSheetByJobIndex[e] = name; }); } this.sheetNames.push(name); return this; } completeJob(index) { const name = this.mapSheetByJobIndex[index]; if (this.sheetMetas[name].jobCount) { this.sheetMetas[name].jobCount -= 1; this.sheetMetas[name].isCompleted = this.sheetMetas[name].jobCount <= 0; this.jobCount--; this.isCompleted = this.jobCount <= 0; } } get IsCompleted() { return this.isCompleted; } get RowCount() { return this.rowCount; } updateRowCount(isCompleted, rowCount) { var _a; const name = this.sheetNames[this.currentSheetName]; const sheetMeta = this.sheetMetas[name]; if (isCompleted) sheetMeta.isCompleted = isCompleted; else if (!this.byJobIndex) { this.rowCount += rowCount; if (((_a = sheetMeta.maxRow) != null ? _a : 0) < sheetMeta.rowCount) { this.currentSheetName++; sheetMeta.isCompleted = true; } sheetMeta.rowCount += rowCount; } } getSheetName(index) { if (this.byJobIndex && index) return this.mapSheetByJobIndex[index]; return this.sheetNames[this.currentSheetName]; } getSheetStatus(name) { return this.sheetMetas[name].isCompleted; } }; // src/common/core/QueueData.ts var QueueData = class { constructor(size = 50) { this.cells = []; this.resolveFunc = () => { }; this.size = size; this.waitingFunc = this.createWaiter(); } createWaiter() { return new Promise((resolve) => { this.resolveFunc = resolve; }); } waiting() { return __async(this, null, function* () { if (this.cells.length < this.size) return; yield this.waitingFunc; }); } shift() { const data = this.cells.shift(); if (this.cells.length === this.size - 1) { this.resolveFunc(); } return data; } add(data) { this.cells.push(data); if (this.cells.length === this.size) { this.waitingFunc = this.createWaiter(); } } }; // src/reporters/PartialDataTransfer.ts var PartialDataTransfer = class { constructor(opts) { this.delayMs = 5; this.isStream = false; this.partialDataHandler = {}; this.jobCount = 1; this.queueData = new QueueData(100); this.isStopConsumeData = false; var _a, _b, _c; this.delayMs = (_a = opts == null ? void 0 : opts.delayMs) != null ? _a : 1; this.isStream = (_b = opts == null ? void 0 : opts.isStream) != null ? _b : false; this.jobCount = (_c = opts == null ? void 0 : opts.jobCount) != null ? _c : 1; } init(partialDataHandler, originalSheetName) { return __async(this, null, function* () { const sheetMetaOptions = this.configSheetMeta(originalSheetName); this.partialDataHandler = partialDataHandler; this.partialDataHandler.done = () => __async(this, null, function* () { return yield this.completed(); }); if (sheetMetaOptions) this.partialDataHandler.SheetMeta = new SheetMeta(sheetMetaOptions); yield this.awake(); }); } start() { return __async(this, null, function* () { if (this.isStream) yield this.startStream(); else yield Promise.all([this.startJobs(), this.consumeData()]); }); } /** Run with stream */ startStream() { return __async(this, null, function* () { const readable = this.createStream(); if (readable === null) throw new Error("You must implement 'createStream' method when using isStream = true"); const wriable = this.partialDataHandler.stream(); readable.pipe(wriable); }); } consumeData() { return __async(this, null, function* () { while (!this.isStopConsumeData) { const data = this.queueData.shift(); if (data) { const isCompleted = yield this.partialDataHandler.do(data); if (isCompleted) break; } else { yield new Promise((r) => setTimeout(r, 2)); } } }); } /** Run with one or multiples job */ startJobs() { return __async(this, null, function* () { const promises = []; const that = this; for (let i = 0; i < this.jobCount; i++) { promises.push(that.createJob(i)); } yield Promise.all(promises); }); } createJob(i) { return __async(this, null, function* () { let isLoop = true; while (isLoop) { const { hasNext, items } = yield this.fetchBatch(i); yield this.queueData.waiting(); this.queueData.add({ items, jobIndex: i }); isLoop = hasNext; yield new Promise((resolve) => setTimeout(resolve, this.delayMs)); } }); } configSheetMeta(originalSheetName) { return void 0; } awake() { return __async(this, null, function* () { }); } completed() { return __async(this, null, function* () { }); } /** Batching data */ fetchBatch(jobIndex) { return __async(this, null, function* () { return { items: null, hasNext: false }; }); } /** Run with streaming data */ createStream() { return null; } }; export { PartialDataTransfer };