@carry0987/event-emitter
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -49,19 +49,47 @@ class EventEmitter { | ||
} | ||
async emit(event, ...args) { | ||
emit(event, ...args) { | ||
const eventName = event; | ||
// Initialize the event | ||
this.init(eventName); | ||
// If there are callbacks for this event | ||
if (this.callbacks[eventName].length > 0) { | ||
// Execute all callbacks and wait for them to complete if they are promises | ||
await Promise.all(this.callbacks[eventName].map(async (value) => await value(...args))); | ||
// If there are no callbacks, return false | ||
if (this.callbacks[eventName].length <= 0) { | ||
return false; | ||
} | ||
// Get all results | ||
const results = this.callbacks[eventName].map(callback => { | ||
try { | ||
// Execute callback and capture the result | ||
const result = callback(...args); | ||
// If result is a promise, wrap it in Promise.resolve to handle uniformly | ||
return result instanceof Promise ? result : Promise.resolve(result); | ||
} | ||
catch (e) { | ||
console.error(`Error in event listener for event: ${eventName}`, e); // Logging error | ||
// Even if an error occurs, continue processing other callbacks | ||
return Promise.resolve(); | ||
} | ||
}); | ||
// Check if any result is a promise | ||
const hasPromise = results.some(result => result instanceof Promise); | ||
// If there is at least one promise, return a promise that resolves when all promises resolve | ||
if (hasPromise) { | ||
return Promise.all(results).then(() => true).catch((e) => { | ||
console.error(`Error handling promises for event: ${eventName}`, e); // Logging error | ||
return false; | ||
}); | ||
} | ||
else { | ||
// If no promises, return true | ||
return true; | ||
} | ||
return false; | ||
} | ||
once(event, listener) { | ||
this.checkListener(listener); | ||
const onceListener = async (...args) => { | ||
await listener(...args); | ||
const onceListener = (...args) => { | ||
// Use a sync wrapper to ensure the listener is removed immediately after execution | ||
const result = listener(...args); | ||
// Remove the listener immediately | ||
this.off(event, onceListener); | ||
// Handle async listeners by wrapping the result in Promise.resolve | ||
return result instanceof Promise ? result : Promise.resolve(result); | ||
}; | ||
@@ -72,4 +100,4 @@ return this.on(event, onceListener); | ||
const version = '1.3.0'; | ||
const version = '1.4.0'; | ||
export { EventEmitter, version }; |
@@ -1,1 +0,1 @@ | ||
!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).eventEmitter={})}(this,(function(t){"use strict";t.EventEmitter=class{callbacks={};init(t){t&&!this.callbacks[t]&&(this.callbacks[t]=[])}checkListener(t){if("function"!=typeof t)throw new TypeError("The listener must be a function")}hasEvent(t){return void 0!==this.callbacks[t]}listeners(){return this.callbacks}addListener(t,s){return this.on(t,s)}clearListener(t){return t?this.callbacks[t]=[]:this.callbacks={},this}on(t,s){return this.checkListener(s),this.init(t),this.callbacks[t].push(s),this}off(t,s){this.checkListener(s);const e=t;return this.init(),this.callbacks[e]&&0!==this.callbacks[e].length?(this.callbacks[e]=this.callbacks[e].filter((t=>t!=s)),this):this}async emit(t,...s){const e=t;return this.init(e),this.callbacks[e].length>0&&(await Promise.all(this.callbacks[e].map((async t=>await t(...s)))),!0)}once(t,s){this.checkListener(s);const e=async(...i)=>{await s(...i),this.off(t,e)};return this.on(t,e)}},t.version="1.3.0"})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).eventEmitter={})}(this,(function(e){"use strict";e.EventEmitter=class{callbacks={};init(e){e&&!this.callbacks[e]&&(this.callbacks[e]=[])}checkListener(e){if("function"!=typeof e)throw new TypeError("The listener must be a function")}hasEvent(e){return void 0!==this.callbacks[e]}listeners(){return this.callbacks}addListener(e,t){return this.on(e,t)}clearListener(e){return e?this.callbacks[e]=[]:this.callbacks={},this}on(e,t){return this.checkListener(t),this.init(e),this.callbacks[e].push(t),this}off(e,t){this.checkListener(t);const s=e;return this.init(),this.callbacks[s]&&0!==this.callbacks[s].length?(this.callbacks[s]=this.callbacks[s].filter((e=>e!=t)),this):this}emit(e,...t){const s=e;if(this.init(s),this.callbacks[s].length<=0)return!1;const i=this.callbacks[s].map((e=>{try{const s=e(...t);return s instanceof Promise?s:Promise.resolve(s)}catch(e){return console.error(`Error in event listener for event: ${s}`,e),Promise.resolve()}}));return!i.some((e=>e instanceof Promise))||Promise.all(i).then((()=>!0)).catch((e=>(console.error(`Error handling promises for event: ${s}`,e),!1)))}once(e,t){this.checkListener(t);const s=(...i)=>{const n=t(...i);return this.off(e,s),n instanceof Promise?n:Promise.resolve(n)};return this.on(e,s)}},e.version="1.4.0"})); |
@@ -15,3 +15,3 @@ type EventArgs<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T]; | ||
off<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
emit<EventName extends keyof EventTypes>(event: EventName, ...args: EventArgs<EventTypes[EventName]>): Promise<boolean>; | ||
emit<EventName extends keyof EventTypes>(event: EventName, ...args: EventArgs<EventTypes[EventName]>): boolean | Promise<boolean>; | ||
once<EventName extends keyof EventTypes>(event: EventName, listener: (...args: EventArgs<EventTypes[EventName]>) => void | Promise<void>): EventEmitter<EventTypes>; | ||
@@ -18,0 +18,0 @@ } |
{ | ||
"name": "@carry0987/event-emitter", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "EventEmitter is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application.", | ||
@@ -9,7 +9,4 @@ "type": "module", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build:js": "rollup --config rollup.config.js --environment BUILD:js", | ||
"build:dts": "rollup --config rollup.config.js --environment BUILD:dts", | ||
"build": "npm run build:js && npm run build:dts" | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
@@ -35,11 +32,20 @@ "files": [ | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-replace": "^5.0.7", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@types/jest": "^29.5.12", | ||
"jest": "^29.7.0", | ||
"rollup": "^4.18.0", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup-plugin-dts": "^6.1.1", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.4.5" | ||
"ts-jest": "^29.1.4", | ||
"ts-node": "^10.9.2", | ||
"tslib": "^2.6.3" | ||
}, | ||
"scripts": { | ||
"test": "jest --config jest.config.ts", | ||
"build:js": "rollup --config rollup.config.ts --configPlugin typescript --environment BUILD:js", | ||
"build:dts": "rollup --config rollup.config.ts --configPlugin typescript --environment BUILD:dts", | ||
"build": "npm run build:js && npm run build:dts" | ||
} | ||
} | ||
} |
# EventEmitter-JS | ||
[](https://www.npmjs.com/package/@carry0987/event-emitter) | ||
[](https://www.npmjs.com/package/@carry0987/event-emitter) [] | ||
`EventEmitter` is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application, supporting standard event operations such as `on`, `off`, `emit`, `once`, and more. | ||
@@ -4,0 +4,0 @@ |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
12628
123
0
12