UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

83 lines (82 loc) 2.69 kB
import { queue } from 'async'; import * as os from 'node:os'; const __filename = globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).filename; export const SINGLE_THREADED_TRANSFORM = false; const CONCURRENCY = SINGLE_THREADED_TRANSFORM ? 4 : os.cpus().length; export class QueueTransformer { constructor(logger) { Object.defineProperty(this, "q", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "progressCallback", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "progress", { enumerable: true, configurable: true, writable: true, value: { completed: 0, total: 0, warnings: 0, failed: 0 } }); this.logger = logger.child({ filename: __filename }); this.q = queue(async (queueTask) => this.processQueueTask(queueTask), CONCURRENCY); this.q.error((err, queueTask) => { this.logger.error(err.stack ? err.stack : err.message); if (queueTask.retries > 0) { queueTask.retries--; this.q.push(queueTask); } else { this.progress.failed++; this.notify(); } }); } async processQueueTask(task) { const subTasks = await task.run(); this.progress.warnings += task.warnings; this.progress.completed++; this.notify(); for (const subTask of subTasks) { this.q.push(subTask); this.progress.completed++; } this.notify(); await new Promise(resolve => setTimeout(resolve, 100)); } async finished() { if (this.q.length() + this.q.running() === 0) { return; } return this.q.drain(); } addTask(taskFetchDir) { this.q.push(taskFetchDir); this.progress.total++; this.notify(); } onProgressNotify(progressCallback) { this.progressCallback = progressCallback; } notify() { if (this.progressCallback) { this.progressCallback({ completed: this.progress.completed, total: this.progress.total, warnings: this.progress.warnings, failed: this.progress.failed }); } } }