@janiscommerce/events
Advanced tools
Comparing version
@@ -9,4 +9,13 @@ # Changelog | ||
## [Unreleased] | ||
## [0.2.0] - 2022-12-20 | ||
### Added | ||
- support for `async` callbacks | ||
- `off()` methods to remove listeners of an event | ||
### Changed | ||
- Not using node `events` module anymore | ||
## [0.1.0] - 2021-10-06 | ||
### Added | ||
- Events class |
'use strict'; | ||
const EventEmitter = require('events'); | ||
let events = {}; | ||
let oneTimeEvents = {}; | ||
module.exports = class Events { | ||
static get eventEmitter() { | ||
static async emit(eventName, ...args) { | ||
if(!this._eventEmitter) | ||
this._eventEmitter = new EventEmitter(); | ||
const callbacks = [ | ||
...events[eventName]?.length ? events[eventName] : [], | ||
...oneTimeEvents[eventName]?.length ? oneTimeEvents[eventName] : [] | ||
]; | ||
return this._eventEmitter; | ||
} | ||
if(!callbacks.length) | ||
return; | ||
static emit(eventName, ...args) { | ||
this.eventEmitter.emit(eventName, ...args); | ||
await Promise.all(callbacks.map(callback => callback(...args))); | ||
if(oneTimeEvents[eventName]?.length) | ||
oneTimeEvents[eventName] = []; | ||
} | ||
static on(eventName, callback) { | ||
this.eventEmitter.on(eventName, callback); | ||
if(!events[eventName]) | ||
events[eventName] = []; | ||
events[eventName].push(callback); | ||
} | ||
static once(eventName, callback) { | ||
this.eventEmitter.once(eventName, callback); | ||
if(!oneTimeEvents[eventName]) | ||
oneTimeEvents[eventName] = []; | ||
oneTimeEvents[eventName].push(callback); | ||
} | ||
static off(eventName) { | ||
if(!eventName) { | ||
events = {}; | ||
oneTimeEvents = {}; | ||
} | ||
if(events[eventName]?.length) | ||
delete events[eventName]; | ||
if(oneTimeEvents[eventName]?.length) | ||
delete oneTimeEvents[eventName]; | ||
} | ||
}; |
{ | ||
"name": "@janiscommerce/events", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A package to handle Janis Events", | ||
@@ -5,0 +5,0 @@ "main": "lib/events.js", |
@@ -9,7 +9,9 @@ # events | ||
This package wraps the `events` from Node.js to ensure using the same EventEmitter instance. | ||
### async/await | ||
**Important**: _Since 0.2.0_ async callbacks are allowed, `on()` and `once()` accepts async functions and `emit()` will wait when `await` is used. | ||
### Emit | ||
You can easily emit a new event using the _static_ method `emit(eventName, ...args)`. | ||
You can easily emit a new event using the _static_ method `async emit(eventName, ...args)`. | ||
@@ -21,10 +23,9 @@ ```js | ||
Events.emit('some-random-event', 'foo', 'bar'); | ||
await Events.emit('some-random-event', 'foo', 'bar'); | ||
``` | ||
For more information see the Node.js documentation [emitter.emit()](https://nodejs.org/api/events.htmlevents_emitter_emit_eventname_args). | ||
### Listen every time | ||
### Listen | ||
To create listener for an event you should call the _static_ method `on(eventName, callback)`. | ||
To create listener for an event you should call the _static_ method `on(eventName, callback)`. | ||
The callback will be called **every time** the event was emitted. | ||
@@ -37,3 +38,3 @@ | ||
Events.on('some-random-event', (...args) => { | ||
Events.on('some-random-event', async (...args) => { | ||
// do some serious stuff with the args | ||
@@ -43,7 +44,6 @@ }); | ||
For more information see the Node.js documentation [emitter.on()](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener). | ||
### Listen one time only | ||
### One time listener | ||
To create listener that is only **called once** should register the listener with the _static_ method `once(eventName, callback)`. | ||
To create listener that is only **called once** should register the listener with the _static_ method `once(eventName, callback)`. | ||
The callback will be called **only the first time** the event was emitted. | ||
@@ -56,3 +56,3 @@ | ||
Events.once('one-time-event', (...args) => { | ||
Events.once('one-time-event', async (...args) => { | ||
// do some serious stuff with the args | ||
@@ -62,2 +62,62 @@ }); | ||
For more information see the Node.js documentation [emitter.once()](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener). | ||
### Remove listeners | ||
To remove listener of an event previously registered you should use the _static_ method `off(eventName)`. | ||
This will remove all listeners (clean all callbacks): registered with `on()` or `once()` | ||
```js | ||
'use strict'; | ||
const Events = require('@janiscommerce/events'); | ||
Events.on('my-event', async () => { | ||
console.log('my-event occurred!!!') | ||
}); | ||
Events.once('my-event', async () => { | ||
console.log('my-event occurred for the first time!!!') | ||
}); | ||
await Event.emit('my-event'); | ||
// expected output: my-event occurred!!! | ||
// expected output: my-event occurred for the first time!!! | ||
await Event.emit('my-event'); | ||
// expected output: my-event occurred!!! | ||
Events.off('my-event'); | ||
await Event.emit('my-event'); // no output expected | ||
``` | ||
### Remove all listeners | ||
To remove all listener registered you should use the _static_ method `off()`. | ||
```js | ||
'use strict'; | ||
const Events = require('@janiscommerce/events'); | ||
Events.on('my-event', async () => { | ||
console.log('my-event occurred!!!') | ||
}); | ||
Events.once('my-other-event', async () => { | ||
console.log('my-other-event occurred for the first time!!!') | ||
}); | ||
await Event.emit('my-event'); | ||
// expected output: my-event occurred!!! | ||
await Event.emit('my-event-event'); | ||
// expected output: my-other-event occurred for the first time!!! | ||
Events.off(); | ||
await Event.emit('my-event'); // no output expected | ||
await Event.emit('my-event-event'); // no output expected | ||
``` |
5537
47.5%36
100%118
103.45%