/*! * @module capillaries * @description Javascript Events * @version 5.0.0 * @link https://github.com/sibiraj-s/capillaries.git * @licence MIT License, https://opensource.org/licenses/MIT */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __typeError = (msg) => { throw TypeError(msg); }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // capillaries.ts var capillaries_exports = {}; __export(capillaries_exports, { AsyncEvents: () => AsyncEvents_default, Events: () => Events_default, Hooks: () => Hooks_default, default: () => capillaries_default }); module.exports = __toCommonJS(capillaries_exports); // src/Events.ts var _events; var Events = class { constructor() { __privateAdd(this, _events, /* @__PURE__ */ new Map()); /** * Create event listener * * @param type A String that specifies the name of the event. * @param listener A function to invoke when the event occurs. * @param context Context to bind to the event handler * * @returns {Function} A function to unsubscribe the listener */ this.on = (type, listener, context = null) => { var _a; if (typeof listener !== "function") { throw new TypeError("Event Listener must be a function"); } const event = (_a = __privateGet(this, _events).get(type)) != null ? _a : []; event.push([listener, context]); __privateGet(this, _events).set(type, event); return () => { var _a2; const events = (_a2 = __privateGet(this, _events).get(type)) != null ? _a2 : []; __privateGet(this, _events).set( type, events.filter((e) => e[0] !== listener) ); }; }; /** * Emit Events * @param type A String that specifies the name of the event. * @param payload Optional payload for event handlers */ this.emit = (type, ...args) => { var _a, _b; const events = (_a = __privateGet(this, _events).get(type)) != null ? _a : []; events.forEach((event) => { const [listenerFn, ctx] = event; listenerFn.apply(ctx, args); }); if (type === "*") { return; } const wildcardEvents = (_b = __privateGet(this, _events).get("*")) != null ? _b : []; wildcardEvents.forEach((event) => { const [listenerFn, ctx] = event; listenerFn.apply(ctx, [type, ...args]); }); }; /** * Unbind all events listeners * * @param type A String that specifies the name of the event. If not specified it will clear all events */ this.unbindAll = (type) => { if (type !== void 0) { __privateGet(this, _events).delete(type); return; } __privateGet(this, _events).clear(); }; Object.freeze(this); } }; _events = new WeakMap(); var Events_default = Events; // src/Hooks.ts var _hooks; var Hooks = class { constructor() { __privateAdd(this, _hooks, /* @__PURE__ */ new Map()); /** * Create a tap * * @param name Name of the hook * @param cb A callback function to invoke when the hook is called * @param ctx Context to bind to the callback function * * @returns {Function} A function to remove the tap */ this.tap = (name, cb, ctx = null) => { var _a; if (typeof cb !== "function") { throw new TypeError("Callback must be a function"); } const hooks = (_a = __privateGet(this, _hooks).get(name)) != null ? _a : []; hooks.push([cb, ctx]); __privateGet(this, _hooks).set(name, hooks); return () => { var _a2; const currentHooks = (_a2 = __privateGet(this, _hooks).get(name)) != null ? _a2 : []; __privateGet(this, _hooks).set( name, currentHooks.filter(([hook]) => hook !== cb) ); }; }; /** * Invokes and awaits on all tapped functions asynchronously * * @param name Name of the hook * @param args Payload for the hook */ this.callAsync = (name, ...args) => __async(this, null, function* () { const hooks = __privateGet(this, _hooks).get(name); if (!hooks) { return; } for (const [hook, ctx] of hooks) { yield hook.apply(ctx, args); } }); /** * Invokes all tapped functions asynchronously in series * The result from one tap is passed to the next, returning the result from the last tap * * @param name Name of the hook * @param arg Initial payload for the hooks * @returns {Promise} Result from the last hook */ this.callAsyncWaterFall = (name, arg) => __async(this, null, function* () { let data = arg; const hooks = __privateGet(this, _hooks).get(name); if (!hooks) { return data; } for (const [hook, ctx] of hooks) { data = yield hook.call(ctx, data); } return data; }); /** * Invokes all tapped functions synchronously * * @param name Name of the hook * @param arg Payload for the hook */ this.call = (name, arg) => { const hooks = __privateGet(this, _hooks).get(name); if (!hooks) { return; } for (const [hook, ctx] of hooks) { hook.call(ctx, arg); } }; /** * Invokes all tapped functions synchronously in series * The result from one tap is passed to the next, returning the result from the last tap * * @param name Name of the hook * @param arg Initial payload for the hooks * @returns {unknown} Result from the last hook */ this.callWaterFall = (name, arg) => { let data = arg; const hooks = __privateGet(this, _hooks).get(name); if (!hooks) { return data; } for (const [hook, ctx] of hooks) { data = hook.call(ctx, data); } return data; }; /** * Remove all hooks or hooks for a specific name * * @param name Optional name of the hook to remove. If not specified, all hooks are cleared */ this.clear = (name) => { if (typeof name !== "undefined") { __privateGet(this, _hooks).delete(name); } else { __privateGet(this, _hooks).clear(); } }; Object.freeze(this); } }; _hooks = new WeakMap(); var Hooks_default = Hooks; // src/AsyncEvents.ts var _events2; var AsyncEvents = class { constructor() { __privateAdd(this, _events2, /* @__PURE__ */ new Map()); /** * Create event listener * * @param name A String that specifies the name of the event. * @param handler A function to invoke when the event is invoked. * * @returns {Function} A function to unsubscribe the listener */ this.on = (name, handler) => { if (__privateGet(this, _events2).has(name)) { throw new Error(`Handler already exists for event: ${String(name)}`); } if (typeof handler !== "function") { throw new TypeError(`Expected handler to be a function, but got: ${typeof handler}`); } __privateGet(this, _events2).set(name, handler); return () => __privateGet(this, _events2).delete(name); }; /** * Invokes the handler function asynchronously * * @param name Name of the event * @param payload Payload for the handler * * @returns {Promise} A promise that resolves when the handler completes */ this.call = (name, ...payload) => { const handler = __privateGet(this, _events2).get(name); if (!handler) { throw new Error(`No handler registered for event: ${String(name)}`); } return handler(...payload); }; /** * Unbind all event listeners * * @param name A String that specifies the name of the event. If not specified, it will clear all events */ this.unbindAll = (name) => { if (typeof name !== "undefined") { __privateGet(this, _events2).delete(name); } else { __privateGet(this, _events2).clear(); } }; } }; _events2 = new WeakMap(); var AsyncEvents_default = AsyncEvents; // capillaries.ts var capillaries_default = Events_default; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AsyncEvents, Events, Hooks }); //# sourceMappingURL=capillaries.cjs.map