UNPKG

@averagehelper/corde

Version:

A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)

187 lines (186 loc) 5.9 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.Queue = void 0; const guid_1 = require("./guid"); /** * Structure to handle a collection of functions and execute then. * This structure does not remove its values after its executions. */ class Queue { constructor() { this._funcs = new Map(); this._defaultParameters = []; } /** * Gets default parameters added. */ get defaultParameters() { return this._defaultParameters; } /** * Add a function to queue. * * @param fn Functions to be queued */ enqueue(fn) { const guid = guid_1.Guid.new(); this._funcs.set(guid, fn); return guid; } /** * Removes a function from queue * @param fn Function to be removed from queue. */ dequeue(guid) { return this._funcs.delete(guid); } /** * Execute functions with parameters. * @param params Parameters to be injected on function in queue. */ executeAsync(...params) { return __awaiter(this, void 0, void 0, function* () { const parameters = [...params, ...this._defaultParameters]; const returnList = []; for (const [, fn] of this._funcs) { const value = yield fn(...parameters); if (value) { returnList.push(value); } } this.clear(); return returnList; }); } /** * Execute functions with parameters. * @param params Parameters to be injected on function in queue. */ executeSync(...params) { const parameters = [...params, ...this._defaultParameters]; const returnList = []; for (const [guid, fn] of this._funcs) { const value = fn(...parameters); if (value) { returnList.push(value); } } this.clear(); return returnList; } /** * Execute function with exception treatment. * So if any function throw a error, it will be handled by an catch function * provided in parameters. * @param catchAction Function to handle errors. * @param params Parameters to the functions. */ tryExecuteSync(catchAction, ...params) { const returnValues = []; this._funcs.forEach((fn) => { try { const value = fn(...params); if (value) { returnValues.push(value); } } catch (error) { if (catchAction) { catchAction(error); } } }); this.clear(); return returnValues; } /** * Execute function with exception treatment. * So if any function throw a error, it will be handled by an catch function * provided in parameters. * @param catchAction Function to handle errors. * @param params Parameters to the functions. */ tryExecuteAsync(catchAction, ...params) { return __awaiter(this, void 0, void 0, function* () { const returnValues = []; this._funcs.forEach((fn) => __awaiter(this, void 0, void 0, function* () { try { const value = yield fn(...params); if (value) { returnValues.push(value); } } catch (error) { if (catchAction) { catchAction(error); } } })); this.clear(); return returnValues; }); } /** * Function like *tryExecute()* but return all exceptions if they * occur. * @param params Parameters to the functions. */ executeWithCatchCollectSync(...params) { const errors = []; this._funcs.forEach((fn) => { try { fn(params); } catch (error) { errors.push(error); } }); this.clear(); return errors; } /** * Function like *tryExecute()* but return all exceptions if they * occur. * @param params Parameters to the functions. */ executeWithCatchCollectAsync(...params) { return __awaiter(this, void 0, void 0, function* () { const errors = []; this._funcs.forEach((fn) => __awaiter(this, void 0, void 0, function* () { try { yield fn(params); } catch (error) { errors.push(error); } })); this.clear(); return errors; }); } /** * Add parameters to be injected on queued functions * @param parameter Parameter value */ addDefaultParameters(...parameter) { if (parameter) { this._defaultParameters.push(...parameter); } } size() { return this._funcs.size; } clear() { return this._funcs.clear(); } } exports.Queue = Queue;