UNPKG

bottleneck

Version:

Distributed task scheduler and rate limiter

84 lines (67 loc) 1.9 kB
"use strict"; var Events; Events = class Events { constructor(instance) { this.instance = instance; this._events = {}; if (this.instance.on != null || this.instance.once != null || this.instance.removeAllListeners != null) { throw new Error("An Emitter already exists for this object"); } this.instance.on = (name, cb) => { return this._addListener(name, "many", cb); }; this.instance.once = (name, cb) => { return this._addListener(name, "once", cb); }; this.instance.removeAllListeners = (name = null) => { if (name != null) { return delete this._events[name]; } else { return this._events = {}; } }; } _addListener(name, status, cb) { var base; if ((base = this._events)[name] == null) { base[name] = []; } this._events[name].push({ cb, status }); return this.instance; } trigger(name, ...args) { if (name !== "debug") { this.trigger("debug", `Event triggered: ${name}`, args); } if (this._events[name] == null) { return; } this._events[name] = this._events[name].filter(function (listener) { return listener.status !== "none"; }); return this._events[name].forEach(listener => { var e, ret; if (listener.status === "none") { return; } if (listener.status === "once") { listener.status = "none"; } try { ret = typeof listener.cb === "function" ? listener.cb(...args) : void 0; return ret != null ? typeof ret.then === "function" ? ret.then(function () {}).catch(e => { return this.trigger("error", e); }) : void 0 : void 0; } catch (error) { e = error; if ("name" !== "error") { return this.trigger("error", e); } } }); } }; module.exports = Events;