UNPKG

padlocal-client-ts

Version:
60 lines 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SerialExecutor = void 0; const PromiseUtils_1 = require("./PromiseUtils"); class SerialJob { constructor(func, promiseCallback, type) { this.func = func; this.promiseCallback = promiseCallback; this.type = type; } } class SerialExecutor { constructor() { this._jobs = []; this._executing = false; } execute(jobFunc, type) { return new Promise((resolve, reject) => { this._jobs.push(new SerialJob(jobFunc, new PromiseUtils_1.PromiseCallback(resolve, reject), type)); this._executeNextJob(); }); } /** * @param type: if type is undefined, clear all jobs in queue */ clear(type) { this._jobs = this._jobs.filter((job) => { if (!type) { return false; } else { return job.type !== type; } }); } _executeNextJob() { if (this._executing) { return; } const job = this._jobs.shift(); if (!job) { return; } this._executing = true; job .func() .then((res) => { job.promiseCallback.resolve(res); }) .catch((e) => { job.promiseCallback.reject(e); }) .finally(() => { this._executing = false; this._executeNextJob(); }); } } exports.SerialExecutor = SerialExecutor; //# sourceMappingURL=SerialExecutor.js.map