New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

capillaries

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capillaries - npm Package Compare versions

Comparing version 4.1.0 to 5.0.0

capillaries.d.cts

224

capillaries.d.ts

@@ -1,122 +0,114 @@

export class Events<E extends Record<string | symbol | '*', unknown>> {
/**
* 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
*/
on<K extends keyof E>(type: K, listener: (...payload: E[K][]) => void, context?: unknown): () => void;
/**
* Create a wildcard listener to listen to all events
*
* @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
*/
on<K extends keyof E>(type: '*', listener: (type: K, ...payload: E[K][]) => void, context?: unknown): () => void;
/**
* Emit Events
* @param type A String that specifies the name of the event.
* @param payload Optional payload for event handlers
*/
emit<K extends keyof E>(type: K, ...payload: E[K][]): void;
/**
* Unbind all events listeners
*
* @param type A String that specifies the name of the event. If not specified it will clear all events
*/
unbindAll<K extends keyof E>(type?: K): void;
type Listener<E, K extends keyof E> = (...payload: E[K][]) => void;
type WildcardListener<E> = <K extends keyof E>(type: K, ...payload: E[K][]) => void;
declare class Events<E extends Record<string | symbol | '*', unknown>> {
#private;
constructor();
/**
* 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
*/
on: {
(type: '*', listener: WildcardListener<E>, context?: unknown): () => void;
<K extends keyof E>(type: K, listener: Listener<E, K>, context?: unknown): () => void;
};
/**
* Emit Events
* @param type A String that specifies the name of the event.
* @param payload Optional payload for event handlers
*/
emit: <K extends keyof E>(type: K, ...args: E[K][]) => void;
/**
* Unbind all events listeners
*
* @param type A String that specifies the name of the event. If not specified it will clear all events
*/
unbindAll: <K extends keyof E>(type?: K) => void;
}
export class AsyncEvents<E extends Record<string, unknown>> {
/**
* Create event listener
*
* @param type A String that specifies the name of the event.
* @param handler A function to invoke when the event is invoked.
*
* @returns {Function} A functionn to unsubscribe the listener
*/
on<K extends keyof E>(name: K, handler: (...payload: E[K][]) => void): () => void;
/**
* Invokes all tapped functions synchronously
*
* @param name Name of the hook
* @param payload Payload for the tap
*
*/
call<K extends keyof E>(name: K, ...payload: E[K][]): Promise<void>;
/**
* Unbind all events listeners
*
* @param type A String that specifies the name of the event. If not specified it will clear all events
*/
unbindAll<K extends keyof E>(type?: K): void;
declare class Hooks<H extends Record<string, unknown>> {
#private;
constructor();
/**
* 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
*/
tap: <K extends keyof H>(name: K, cb: (payload: H[K]) => void, ctx?: unknown) => (() => void);
/**
* Invokes and awaits on all tapped functions asynchronously
*
* @param name Name of the hook
* @param args Payload for the hook
*/
callAsync: <K extends keyof H>(name: K, ...args: H[K][]) => Promise<void>;
/**
* 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<unknown>} Result from the last hook
*/
callAsyncWaterFall: <K extends keyof H>(name: K, arg?: H[K]) => Promise<unknown>;
/**
* Invokes all tapped functions synchronously
*
* @param name Name of the hook
* @param arg Payload for the hook
*/
call: <K extends keyof H>(name: K, arg?: H[K]) => void;
/**
* 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
*/
callWaterFall: <K extends keyof H>(name: K, arg?: H[K]) => unknown;
/**
* 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
*/
clear: <K extends keyof H>(name?: K) => void;
}
export class Hooks<H extends Record<string, unknown>> {
/**
* Create a tap
*
* @param type Name of the hook
* @param cb A callback function to invoke when the tap is called
* @param context Context to bind to the callback function
*
* @returns {Function} A function to remove the tap
*/
tap<K extends keyof H>(name: K, cb: (payload: H[K]) => void, context?: unknown): () => void;
/**
* Invokes all tapped functions synchronously
*
* @param name Name of the hook
* @param payload Payload for the tap
*/
call<K extends keyof H>(name: K, payload?: H[K]): void;
/**
* Invokes all tapped functions synchronously
* The result from one tap is passed over to the other in series and
* will return the response from last tap as result
*
* @param name A String that specifies the name of the event.
* @param payload Optional payload for event handlers
*/
callWaterFall<K extends keyof H>(name: K, payload?: H[K]): unknown;
/**
* Invokes and awaits on all tapped functions
*
* @param name A String that specifies the name of the event.
* @param payload Optional payload for event handlers
*/
callAsync<K extends keyof H>(name: K, payload?: H[K]): Promise<void>;
/**
* Invokes all tapped functions and awaits them
* the result from one tap is passed over to the other in series and
* will return the response from last tap as result
*
* @param name Name of the hook to invoke.
* @param payload Optional payload for hooks
*/
callAsyncWaterFall<K extends keyof H>(name: K, payload?: H[K]): Promise<unknown>;
/**
* Remove all hooks
* @param name Name of the hook to remove.
*/
clear<K extends keyof H>(name?: K): void;
declare class AsyncEvents<E extends Record<string, unknown>> {
#private;
/**
* 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
*/
on: <K extends keyof E>(name: K, handler: (...payload: E[K][]) => Promise<void>) => (() => void);
/**
* Invokes the handler function asynchronously
*
* @param name Name of the event
* @param payload Payload for the handler
*
* @returns {Promise<void>} A promise that resolves when the handler completes
*/
call: <K extends keyof E>(name: K, ...payload: E[K][]) => Promise<void>;
/**
* Unbind all event listeners
*
* @param name A String that specifies the name of the event. If not specified, it will clear all events
*/
unbindAll: <K extends keyof E>(name?: K) => void;
}
export default Events;
export { AsyncEvents, Events, Hooks, Events as default };
/*!
* @module capillaries
* @description Javascript Events
* @version 4.1.0
* @version 5.0.0
* @link https://github.com/sibiraj-s/capillaries.git

@@ -9,581 +9,279 @@ * @licence MIT License, https://opensource.org/licenses/MIT

function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
var __typeError = (msg) => {
throw TypeError(msg);
};
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);
}
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
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());
});
};
return _typeof(obj);
}
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
// 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");
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
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;
}
_next(undefined);
});
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
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;
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _createForOfIteratorHelper(o, allowArrayLike) {
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
if (!it) {
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
// 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)
);
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = it.call(o);
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
};
/**
* 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;
}
}
};
}
function _classPrivateFieldGet(receiver, privateMap) {
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
return _classApplyDescriptorGet(receiver, descriptor);
}
function _classExtractFieldDescriptor(receiver, privateMap, action) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to " + action + " private field on non-instance");
}
return privateMap.get(receiver);
}
function _classApplyDescriptorGet(receiver, descriptor) {
if (descriptor.get) {
return descriptor.get.call(receiver);
}
return descriptor.value;
}
var _events$1 = /*#__PURE__*/new WeakMap();
var Events = function Events() {
var _this = this;
_classCallCheck(this, Events);
_events$1.set(this, {
writable: true,
value: new Map()
});
_defineProperty(this, "on", function (type, listener) {
var ctx = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (typeof listener !== 'function') {
throw new TypeError('Event Listener must be a function');
}
var event = _classPrivateFieldGet(_this, _events$1).get(type) || [];
event.push([listener, ctx]);
_classPrivateFieldGet(_this, _events$1).set(type, event);
return function () {
var events = _classPrivateFieldGet(_this, _events$1).get(type) || [];
_classPrivateFieldGet(_this, _events$1).set(type, events.filter(function (e) {
return e[0] !== listener;
}));
};
});
_defineProperty(this, "emit", function (type) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var events = _classPrivateFieldGet(_this, _events$1).get(type) || [];
events.forEach(function (event) {
var _event = _slicedToArray(event, 2),
listenerFn = _event[0],
ctx = _event[1];
listenerFn.apply(ctx, args);
for (const [hook, ctx] of hooks) {
yield hook.apply(ctx, args);
}
});
var wildcardEvents = type === '*' ? [] : _classPrivateFieldGet(_this, _events$1).get('*') || [];
wildcardEvents.forEach(function (event) {
var _event2 = _slicedToArray(event, 2),
listenerFn = _event2[0],
ctx = _event2[1];
listenerFn.apply(ctx, [type].concat(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<unknown>} 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;
});
});
_defineProperty(this, "unbindAll", function (type) {
if (type) {
_classPrivateFieldGet(_this, _events$1)["delete"](type);
return;
}
_classPrivateFieldGet(_this, _events$1).clear();
});
Object.freeze(this);
};
var _hooks = /*#__PURE__*/new WeakMap();
var Hooks = function Hooks() {
var _this = this;
_classCallCheck(this, Hooks);
_hooks.set(this, {
writable: true,
value: new Map()
});
_defineProperty(this, "tap", function (name, cb) {
var ctx = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (typeof cb !== 'function') {
throw new TypeError('Callback must be a function');
}
var hook = _classPrivateFieldGet(_this, _hooks).get(name) || [];
hook.push([cb, ctx]);
_classPrivateFieldGet(_this, _hooks).set(name, hook);
return function () {
var hooks = _classPrivateFieldGet(_this, _hooks).get(name) || [];
_classPrivateFieldGet(_this, _hooks).set(name, hooks.filter(function (e) {
return e[0] !== cb;
}));
/**
* 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);
}
};
});
_defineProperty(this, "callAsync", /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(name) {
var hooks,
_len,
args,
_key,
_iterator,
_step,
_step$value,
hook,
ctx,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
hooks = _classPrivateFieldGet(_this, _hooks).get(name);
if (hooks) {
_context.next = 3;
break;
}
return _context.abrupt("return");
case 3:
for (_len = _args.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = _args[_key];
}
_iterator = _createForOfIteratorHelper(hooks);
_context.prev = 5;
_iterator.s();
case 7:
if ((_step = _iterator.n()).done) {
_context.next = 13;
break;
}
_step$value = _slicedToArray(_step.value, 2), hook = _step$value[0], ctx = _step$value[1];
_context.next = 11;
return hook.apply(ctx, args);
case 11:
_context.next = 7;
break;
case 13:
_context.next = 18;
break;
case 15:
_context.prev = 15;
_context.t0 = _context["catch"](5);
_iterator.e(_context.t0);
case 18:
_context.prev = 18;
_iterator.f();
return _context.finish(18);
case 21:
case "end":
return _context.stop();
}
}
}, _callee, null, [[5, 15, 18, 21]]);
}));
return function (_x) {
return _ref.apply(this, arguments);
/**
* 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;
};
}());
_defineProperty(this, "callAsyncWaterFall", /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(name, arg) {
var data, hooks, _iterator2, _step2, _step2$value, hook, ctx;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
data = arg;
hooks = _classPrivateFieldGet(_this, _hooks).get(name);
if (hooks) {
_context2.next = 4;
break;
}
return _context2.abrupt("return", data);
case 4:
_iterator2 = _createForOfIteratorHelper(hooks);
_context2.prev = 5;
_iterator2.s();
case 7:
if ((_step2 = _iterator2.n()).done) {
_context2.next = 14;
break;
}
_step2$value = _slicedToArray(_step2.value, 2), hook = _step2$value[0], ctx = _step2$value[1];
_context2.next = 11;
return hook.call(ctx, data);
case 11:
data = _context2.sent;
case 12:
_context2.next = 7;
break;
case 14:
_context2.next = 19;
break;
case 16:
_context2.prev = 16;
_context2.t0 = _context2["catch"](5);
_iterator2.e(_context2.t0);
case 19:
_context2.prev = 19;
_iterator2.f();
return _context2.finish(19);
case 22:
return _context2.abrupt("return", data);
case 23:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[5, 16, 19, 22]]);
}));
return function (_x2, _x3) {
return _ref2.apply(this, arguments);
/**
* 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;
_defineProperty(this, "call", function (name, arg) {
var hooks = _classPrivateFieldGet(_this, _hooks).get(name);
if (!hooks) {
return;
}
var _iterator3 = _createForOfIteratorHelper(hooks),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _step3$value = _slicedToArray(_step3.value, 2),
hook = _step3$value[0],
ctx = _step3$value[1];
hook.call(arg, ctx);
// 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)}`);
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
});
_defineProperty(this, "callWaterFall", function (name, arg) {
var data = arg;
var hooks = _classPrivateFieldGet(_this, _hooks).get(name);
if (!hooks) {
return data;
}
var _iterator4 = _createForOfIteratorHelper(hooks),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var _step4$value = _slicedToArray(_step4.value, 2),
hook = _step4$value[0],
ctx = _step4$value[1];
data = hook.call(ctx, data);
if (typeof handler !== "function") {
throw new TypeError(`Expected handler to be a function, but got: ${typeof handler}`);
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
return data;
});
_defineProperty(this, "clear", function (name) {
if (name) {
_classPrivateFieldGet(_this, _hooks)["delete"](name);
return;
}
_classPrivateFieldGet(_this, _hooks).clear();
});
Object.freeze(this);
__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<void>} 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;
var _events = /*#__PURE__*/new WeakMap();
var AsyncEvents = function AsyncEvents() {
var _this = this;
_classCallCheck(this, AsyncEvents);
_events.set(this, {
writable: true,
value: new Map()
});
_defineProperty(this, "on", function (name, handler) {
if (_classPrivateFieldGet(_this, _events).has(name)) {
throw new Error("Handler already exists for: ".concat(name));
}
if (typeof handler !== 'function') {
throw new Error("Expected handler to be a function. But got: ".concat(_typeof(handler)));
}
_classPrivateFieldGet(_this, _events).set(name, handler);
return function () {
return _classPrivateFieldGet(_this, _events)["delete"](name);
};
});
_defineProperty(this, "call", function (name, payload) {
var handler = _classPrivateFieldGet(_this, _events).get(name);
if (!handler) {
throw new Error("No handler registered for event: ".concat(name));
}
return handler(payload);
});
_defineProperty(this, "unbindAll", function () {
_classPrivateFieldGet(_this, _events).clear();
});
// capillaries.ts
var capillaries_default = Events_default;
export {
AsyncEvents_default as AsyncEvents,
Events_default as Events,
Hooks_default as Hooks,
capillaries_default as default
};
export default Events;
export { AsyncEvents, Events, Hooks };
//# sourceMappingURL=capillaries.js.map
//# sourceMappingURL=capillaries.js.map
/*!
* @module capillaries
* @description Javascript Events
* @version 4.1.0
* @version 5.0.0
* @link https://github.com/sibiraj-s/capillaries.git
* @licence MIT License, https://opensource.org/licenses/MIT
*/
function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function asyncGeneratorStep(e,t,r,n,a,o,i){try{var s=e[o](i),l=s.value}catch(e){return void r(e)}s.done?t(l):Promise.resolve(l).then(n,a)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise((function(n,a){var o=e.apply(t,r);function i(e){asyncGeneratorStep(o,n,a,i,s,"next",e)}function s(e){asyncGeneratorStep(o,n,a,i,s,"throw",e)}i(void 0)}))}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_unsupportedIterableToArray(e,t)||_nonIterableRest()}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _iterableToArrayLimit(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,a,o=[],i=!0,s=!1;try{for(r=r.call(e);!(i=(n=r.next()).done)&&(o.push(n.value),!t||o.length!==t);i=!0);}catch(e){s=!0,a=e}finally{try{i||null==r.return||r.return()}finally{if(s)throw a}}return o}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _createForOfIteratorHelper(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,a=function(){};return{s:a,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,i=!0,s=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){s=!0,o=e},f:function(){try{i||null==r.return||r.return()}finally{if(s)throw o}}}}function _classPrivateFieldGet(e,t){return _classApplyDescriptorGet(e,_classExtractFieldDescriptor(e,t,"get"))}function _classExtractFieldDescriptor(e,t,r){if(!t.has(e))throw new TypeError("attempted to "+r+" private field on non-instance");return t.get(e)}function _classApplyDescriptorGet(e,t){return t.get?t.get.call(e):t.value}var _events$1=new WeakMap,Events=function e(){var t=this;_classCallCheck(this,e),_events$1.set(this,{writable:!0,value:new Map}),_defineProperty(this,"on",(function(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if("function"!=typeof r)throw new TypeError("Event Listener must be a function");var a=_classPrivateFieldGet(t,_events$1).get(e)||[];return a.push([r,n]),_classPrivateFieldGet(t,_events$1).set(e,a),function(){var n=_classPrivateFieldGet(t,_events$1).get(e)||[];_classPrivateFieldGet(t,_events$1).set(e,n.filter((function(e){return e[0]!==r})))}})),_defineProperty(this,"emit",(function(e){for(var r=arguments.length,n=new Array(r>1?r-1:0),a=1;a<r;a++)n[a-1]=arguments[a];var o=_classPrivateFieldGet(t,_events$1).get(e)||[];o.forEach((function(e){var t=_slicedToArray(e,2),r=t[0],a=t[1];r.apply(a,n)}));var i="*"===e?[]:_classPrivateFieldGet(t,_events$1).get("*")||[];i.forEach((function(t){var r=_slicedToArray(t,2),a=r[0],o=r[1];a.apply(o,[e].concat(n))}))})),_defineProperty(this,"unbindAll",(function(e){e?_classPrivateFieldGet(t,_events$1).delete(e):_classPrivateFieldGet(t,_events$1).clear()})),Object.freeze(this)},_hooks=new WeakMap,Hooks=function e(){var t=this;_classCallCheck(this,e),_hooks.set(this,{writable:!0,value:new Map}),_defineProperty(this,"tap",(function(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if("function"!=typeof r)throw new TypeError("Callback must be a function");var a=_classPrivateFieldGet(t,_hooks).get(e)||[];return a.push([r,n]),_classPrivateFieldGet(t,_hooks).set(e,a),function(){var n=_classPrivateFieldGet(t,_hooks).get(e)||[];_classPrivateFieldGet(t,_hooks).set(e,n.filter((function(e){return e[0]!==r})))}})),_defineProperty(this,"callAsync",function(){var e=_asyncToGenerator(regeneratorRuntime.mark((function e(r){var n,a,o,i,s,l,c,u,f,v=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=_classPrivateFieldGet(t,_hooks).get(r)){e.next=3;break}return e.abrupt("return");case 3:for(a=v.length,o=new Array(a>1?a-1:0),i=1;i<a;i++)o[i-1]=v[i];s=_createForOfIteratorHelper(n),e.prev=5,s.s();case 7:if((l=s.n()).done){e.next=13;break}return c=_slicedToArray(l.value,2),u=c[0],f=c[1],e.next=11,u.apply(f,o);case 11:e.next=7;break;case 13:e.next=18;break;case 15:e.prev=15,e.t0=e.catch(5),s.e(e.t0);case 18:return e.prev=18,s.f(),e.finish(18);case 21:case"end":return e.stop()}}),e,null,[[5,15,18,21]])})));return function(t){return e.apply(this,arguments)}}()),_defineProperty(this,"callAsyncWaterFall",function(){var e=_asyncToGenerator(regeneratorRuntime.mark((function e(r,n){var a,o,i,s,l,c,u;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a=n,o=_classPrivateFieldGet(t,_hooks).get(r)){e.next=4;break}return e.abrupt("return",a);case 4:i=_createForOfIteratorHelper(o),e.prev=5,i.s();case 7:if((s=i.n()).done){e.next=14;break}return l=_slicedToArray(s.value,2),c=l[0],u=l[1],e.next=11,c.call(u,a);case 11:a=e.sent;case 12:e.next=7;break;case 14:e.next=19;break;case 16:e.prev=16,e.t0=e.catch(5),i.e(e.t0);case 19:return e.prev=19,i.f(),e.finish(19);case 22:return e.abrupt("return",a);case 23:case"end":return e.stop()}}),e,null,[[5,16,19,22]])})));return function(t,r){return e.apply(this,arguments)}}()),_defineProperty(this,"call",(function(e,r){var n=_classPrivateFieldGet(t,_hooks).get(e);if(n){var a,o=_createForOfIteratorHelper(n);try{for(o.s();!(a=o.n()).done;){var i=_slicedToArray(a.value,2),s=i[0],l=i[1];s.call(r,l)}}catch(e){o.e(e)}finally{o.f()}}})),_defineProperty(this,"callWaterFall",(function(e,r){var n=r,a=_classPrivateFieldGet(t,_hooks).get(e);if(!a)return n;var o,i=_createForOfIteratorHelper(a);try{for(i.s();!(o=i.n()).done;){var s=_slicedToArray(o.value,2),l=s[0],c=s[1];n=l.call(c,n)}}catch(e){i.e(e)}finally{i.f()}return n})),_defineProperty(this,"clear",(function(e){e?_classPrivateFieldGet(t,_hooks).delete(e):_classPrivateFieldGet(t,_hooks).clear()})),Object.freeze(this)},_events=new WeakMap,AsyncEvents=function e(){var t=this;_classCallCheck(this,e),_events.set(this,{writable:!0,value:new Map}),_defineProperty(this,"on",(function(e,r){if(_classPrivateFieldGet(t,_events).has(e))throw new Error("Handler already exists for: ".concat(e));if("function"!=typeof r)throw new Error("Expected handler to be a function. But got: ".concat(_typeof(r)));return _classPrivateFieldGet(t,_events).set(e,r),function(){return _classPrivateFieldGet(t,_events).delete(e)}})),_defineProperty(this,"call",(function(e,r){var n=_classPrivateFieldGet(t,_events).get(e);if(!n)throw new Error("No handler registered for event: ".concat(e));return n(r)})),_defineProperty(this,"unbindAll",(function(){_classPrivateFieldGet(t,_events).clear()}))};export default Events;export{AsyncEvents,Events,Hooks};
//# sourceMappingURL=capillaries.min.js.map
var _events,__typeError=t=>{throw TypeError(t)},__accessCheck=(t,e,s)=>e.has(t)||__typeError("Cannot "+s),__privateGet=(t,e,s)=>(__accessCheck(t,e,"read from private field"),s?s.call(t):e.get(t)),__privateAdd=(t,e,s)=>e.has(t)?__typeError("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,s),__async=(t,e,s)=>new Promise(((r,n)=>{var a=t=>{try{_(s.next(t))}catch(t){n(t)}},o=t=>{try{_(s.throw(t))}catch(t){n(t)}},_=t=>t.done?r(t.value):Promise.resolve(t.value).then(a,o);_((s=s.apply(t,e)).next())})),Events=class{constructor(){__privateAdd(this,_events,new Map),this.on=(t,e,s=null)=>{var r;if("function"!=typeof e)throw new TypeError("Event Listener must be a function");const n=null!=(r=__privateGet(this,_events).get(t))?r:[];return n.push([e,s]),__privateGet(this,_events).set(t,n),()=>{var s;const r=null!=(s=__privateGet(this,_events).get(t))?s:[];__privateGet(this,_events).set(t,r.filter((t=>t[0]!==e)))}},this.emit=(t,...e)=>{var s,r;if((null!=(s=__privateGet(this,_events).get(t))?s:[]).forEach((t=>{const[s,r]=t;s.apply(r,e)})),"*"===t)return;(null!=(r=__privateGet(this,_events).get("*"))?r:[]).forEach((s=>{const[r,n]=s;r.apply(n,[t,...e])}))},this.unbindAll=t=>{void 0===t?__privateGet(this,_events).clear():__privateGet(this,_events).delete(t)},Object.freeze(this)}};_events=new WeakMap;var _hooks,Events_default=Events,Hooks=class{constructor(){__privateAdd(this,_hooks,new Map),this.tap=(t,e,s=null)=>{var r;if("function"!=typeof e)throw new TypeError("Callback must be a function");const n=null!=(r=__privateGet(this,_hooks).get(t))?r:[];return n.push([e,s]),__privateGet(this,_hooks).set(t,n),()=>{var s;const r=null!=(s=__privateGet(this,_hooks).get(t))?s:[];__privateGet(this,_hooks).set(t,r.filter((([t])=>t!==e)))}},this.callAsync=(t,...e)=>__async(this,null,(function*(){const s=__privateGet(this,_hooks).get(t);if(s)for(const[t,r]of s)yield t.apply(r,e)})),this.callAsyncWaterFall=(t,e)=>__async(this,null,(function*(){let s=e;const r=__privateGet(this,_hooks).get(t);if(!r)return s;for(const[t,e]of r)s=yield t.call(e,s);return s})),this.call=(t,e)=>{const s=__privateGet(this,_hooks).get(t);if(s)for(const[t,r]of s)t.call(r,e)},this.callWaterFall=(t,e)=>{let s=e;const r=__privateGet(this,_hooks).get(t);if(!r)return s;for(const[t,e]of r)s=t.call(e,s);return s},this.clear=t=>{void 0!==t?__privateGet(this,_hooks).delete(t):__privateGet(this,_hooks).clear()},Object.freeze(this)}};_hooks=new WeakMap;var _events2,Hooks_default=Hooks,AsyncEvents=class{constructor(){__privateAdd(this,_events2,new Map),this.on=(t,e)=>{if(__privateGet(this,_events2).has(t))throw new Error(`Handler already exists for event: ${String(t)}`);if("function"!=typeof e)throw new TypeError("Expected handler to be a function, but got: "+typeof e);return __privateGet(this,_events2).set(t,e),()=>__privateGet(this,_events2).delete(t)},this.call=(t,...e)=>{const s=__privateGet(this,_events2).get(t);if(!s)throw new Error(`No handler registered for event: ${String(t)}`);return s(...e)},this.unbindAll=t=>{void 0!==t?__privateGet(this,_events2).delete(t):__privateGet(this,_events2).clear()}}};_events2=new WeakMap;var AsyncEvents_default=AsyncEvents,capillaries_default=Events_default;export{AsyncEvents_default as AsyncEvents,Events_default as Events,Hooks_default as Hooks,capillaries_default as default};//# sourceMappingURL=capillaries.min.js.map

@@ -17,2 +17,10 @@ # CHANGELOG

## v5.0.0 (2024-06-22)
#### Enhancements
- rewrite to typescript ([bbb93e0](https://github.com/sibiraj-s/capillaries/commit/bbb93e0))
- improved typings ([bbb93e0](https://github.com/sibiraj-s/capillaries/commit/bbb93e0))
- switch from jest to vitest ([8b3855b](https://github.com/sibiraj-s/capillaries/commit/8b3855b))
## v4.1.0 (2021-06-25)

@@ -34,3 +42,3 @@

```js
event.on("*", (payload) => {});
event.on('*', (payload) => {});
```

@@ -41,3 +49,3 @@

```js
event.on("*", (type, payload) => {});
event.on('*', (type, payload) => {});
```

@@ -97,8 +105,8 @@

event.on("a", handler);
event.on('a', handler);
// To Unsubscribe
event.off("a", handler);
event.off('a', handler);
// or
event.off("a");
event.off('a');
```

@@ -113,3 +121,3 @@

const unsubscribe = event.on("a", handler);
const unsubscribe = event.on('a', handler);

@@ -119,3 +127,3 @@ // To Unsubscribe

// or
event.unbindAll("a");
event.unbindAll('a');
```

@@ -122,0 +130,0 @@

{
"name": "capillaries",
"version": "4.1.0",
"version": "5.0.0",
"description": "Javascript Events",

@@ -12,2 +12,3 @@ "author": "sibiraj-s",

"sideEffects": false,
"type": "module",
"keywords": [

@@ -19,3 +20,15 @@ "javascript-events",

"module": "capillaries.js",
"types": "capillaries.d.ts"
"types": "capillaries.d.ts",
"exports": {
".": {
"require": {
"types": "./capillaries.d.cts",
"default": "./capillaries.cjs"
},
"import": {
"types": "./capillaries.d.ts",
"default": "./capillaries.js"
}
}
}
}

@@ -101,3 +101,3 @@ # Capillaries [![Tests](https://github.com/sibiraj-s/capillaries/workflows/Tests/badge.svg)](https://github.com/sibiraj-s/capillaries/actions)

// remove all hooks
hooks.clear()
hooks.clear();
```

@@ -118,2 +118,1 @@

[yarn]: https://yarnpkg.com/lang/en/
[umd]: https://github.com/umdjs/umd

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc