UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

57 lines (52 loc) 1.56 kB
/** * Copyright © schukai GmbH and all contributing authors, {{copyRightYear}}. All rights reserved. * Node module: @schukai/monster * * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3). * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html * * For those who do not wish to adhere to the AGPLv3, a commercial license is available. * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms. * For more information about purchasing a commercial license, please contact schukai GmbH. * * SPDX-License-Identifier: AGPL-3.0 */ import { internalSymbol } from "../../constants.mjs"; import { getGlobalFunction } from "../../types/global.mjs"; import { validateFunction, validateInteger } from "../../types/validate.mjs"; export { Callback }; /** * @private */ class Callback { /** * * @param {function} callback * @param {int|undefined} time * @throws {TypeError} value is not a function * @throws {TypeError} value is not an integer * @private */ constructor(callback, time) { this[internalSymbol] = { callback: validateFunction(callback), time: validateInteger(time ?? 0), }; } /** * @private * @param {*} data * @return {Promise} */ run(data) { return new Promise((resolve, reject) => { getGlobalFunction("setTimeout")(() => { try { resolve(this[internalSymbol].callback(data)); } catch (e) { reject(e); } }, this[internalSymbol].time); }); } }