UNPKG

@buttercup/channel-queue

Version:

A queue management library with channels

144 lines (143 loc) 4.53 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Task = void 0; const tools_1 = require("./tools"); const types_1 = require("./types"); /** * Internal Task class, for handling executions */ class Task { /** * Constructor for a Task * @param {Function|Promise} item The item to enqueue * @param {TaskPriority=} type The priority to set * @param {String=} stack The stack name */ constructor(item, type = types_1.TaskPriority.Normal, stack) { this._error = null; this._rejectFn = null; this._resolveFn = null; this._stack = null; if (item instanceof Promise !== true && typeof item !== "function") { throw new Error("Invalid task item: Expected a Promise or Function"); } this._target = typeof item === "function" ? item : () => item; this._stack = stack !== null && stack !== void 0 ? stack : null; this._type = type; this._timeLimit = -1; this._resolveFn = null; this._rejectFn = null; this._queuedPromise = new Promise((resolve, reject) => { this._resolveFn = resolve; this._rejectFn = reject; }); const now = new Date(); this._created = now.getTime(); } /** * Creation timestamp * @type {Number} * @readonly */ get created() { return this._created; } /** * Execution error, if one occurred * @type {Error | null} */ get error() { return this._error; } /** * Promise which resolves when work has completed * @type {Promise} */ get queuedPromise() { return this._queuedPromise; } /** * The stack name * @type {String} */ get stack() { return this._stack; } /** * The target function * @type {Function} */ get target() { return this._target; } /** * Current time limit * @type {Number} */ get timeLimit() { return this._timeLimit; } /** * The task priority type * @type {TaskPriority} */ get type() { return this._type; } set timeLimit(newLimit) { this._timeLimit = newLimit; } /** * Execute the task * @returns {Promise} */ execute(throws = true) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const fn = this.target; let output; try { output = fn(); } catch (err) { this._error = err; if (throws) { (_a = this._rejectFn) === null || _a === void 0 ? void 0 : _a.call(this, err); } else { (_b = this._resolveFn) === null || _b === void 0 ? void 0 : _b.call(this); } return Promise.resolve(); } let chainOutput = output instanceof Promise ? output : Promise.resolve(output); if (this.timeLimit >= 0) { chainOutput = (0, tools_1.timeLimit)(chainOutput, this.timeLimit); } yield chainOutput .then(result => { var _a; (_a = this._resolveFn) === null || _a === void 0 ? void 0 : _a.call(this, result); }) .catch(err => { var _a, _b; this._error = err; if (throws) { (_a = this._rejectFn) === null || _a === void 0 ? void 0 : _a.call(this, err); } else { (_b = this._resolveFn) === null || _b === void 0 ? void 0 : _b.call(this); } }); }); } } exports.Task = Task;