UNPKG

@barchart/common-js

Version:
165 lines (159 loc) 4.91 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Serializer_exports = {}; __export(Serializer_exports, { default: () => Serializer }); module.exports = __toCommonJS(Serializer_exports); var assert = __toESM(require("./../lang/assert.js")); var promise = __toESM(require("./../lang/promise.js")); var import_Disposable = __toESM(require("./../lang/Disposable.js")); var import_Queue = __toESM(require("./../collections/Queue.js")); class Serializer extends import_Disposable.default { #workQueue; #enqueued; #processed; #running; constructor() { super(); this.#workQueue = new import_Queue.default(); this.#enqueued = 0; this.#processed = 0; this.#running = false; } /** * Gets the sequence of the item that was last processed. * * @public * @returns {number} */ getCurrent() { return this.#processed; } /** * The total number of items that have been added to the queue. * * @public * @returns {number} */ getTotal() { return this.#enqueued; } /** * The number of items that are currently pending. * * @public * @returns {number} */ getPending() { return this.#enqueued - this.#processed; } /** * Indicates if a work item is currently being processed. * * @public * @returns {boolean} */ getRunning() { return this.#running; } /** * Adds a new action to the processing queue. If the action * is asynchronous, the action should return a promise. * * @public * @param {Function} actionToEnqueue * @returns {Promise} - A promise which resolves once the action has been processed. */ enqueue(actionToEnqueue) { return promise.build((resolveCallback, rejectCallback) => { assert.argumentIsRequired(actionToEnqueue, "actionToEnqueue", Function); if (this.getIsDisposed()) { throw new Error("Unable to add action to the Serializer, it has been disposed."); } this.#enqueued = this.#enqueued + 1; this._getWorkQueue().enqueue(async () => { try { if (this.getIsDisposed()) { throw new Error("Unable to process Serializer action, the serializer has been disposed."); } this.#processed = this.#processed + 1; const result = await actionToEnqueue(); resolveCallback(result); } catch (error) { rejectCallback(error); } }); this.#checkStart(); }); } /** * Allows an inheriting class to override the internal {@link Queue} implementation. * * @protected * @returns {Queue|*} */ _getWorkQueue() { return this.#workQueue; } #checkStart() { const workQueue = this._getWorkQueue(); if (workQueue.empty() || this.#running) { return; } this.#running = true; const actionToExecute = workQueue.dequeue(); const run = async () => { await actionToExecute(); this.#running = false; this.#checkStart(); }; run(); } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[Serializer]"; } } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }