Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mjancarik/esmj-emitter
Lightweight emitter with zero dependencies for sync/async operation with from listeners result
https://github.com/mjancarik/esmj-emitter
browser eventemitter events javascript nodejs pubsub
Last synced: 3 months ago
JSON representation
Lightweight emitter with zero dependencies for sync/async operation with from listeners result
- Host: GitHub
- URL: https://github.com/mjancarik/esmj-emitter
- Owner: mjancarik
- License: mit
- Created: 2022-08-10T09:22:28.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-21T13:02:16.000Z (7 months ago)
- Last Synced: 2024-10-06T12:01:59.005Z (4 months ago)
- Topics: browser, eventemitter, events, javascript, nodejs, pubsub
- Language: JavaScript
- Homepage:
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Emitter
The `@esmj/emitter` is tiny (O,9kB) async event emitter library.
It works in Node.js and the browser (using a bundler like webpack).
## Requirements
- Node 18+
## Install
```shell
npm install @esmj/emitter
```## Usage
It works for both Javascript modules (ESM and CJS).
```javascript
import { Emitter } from '@esmj/emitter';const emitter = new Emitter();
emitter.on('some-event', async (event) => {
return 2 * event.result;
});emitter.on('some-event', (event) => {
return 3 * event.result;
});emitter.on('some-event', (event) => {
event.stopPropagation();
});emitter.on('some-event', async (event) => {
return 4 * event.result;
});const { result } = await emitter.emit('some-event', { result: 1 });
console.log(result); // 6
```
Or if you want to use custom name for result from your listeners. You can use `RESULT_KEY` symbol as example below.
```javascript
import { Emitter, RESULT_KEY } from '@esmj/emitter';const emitter = new Emitter();
emitter.on('some-event', async (event) => {
return 2 * event.count;
});emitter.on('some-event', (event) => {
return 3 * event.count;
});emitter.on('some-event', (event) => {
event.stopPropagation();
});emitter.on('some-event', async (event) => {
return 4 * event.count;
});const { count } = await emitter.emit('some-event', { count: 1, [RESULT_KEY]: 'count' });
console.log(count); // 6
```
## API
### emitter = new Emitter(options?)Create a new instance of Emitter.
#### options?
Type: `object`
Configure the new instance of Emitter.
##### logger?
Type: `object`
Default: `console`Configure the logger for this instance.
##### debug?
Type: `boolean|function(eventName, event)`
Default: `false`Configure the debugging options for this instance.
#### on(eventName, listener) / addListener(eventName, Listener)
Subscribe listener for event.Returns an unsubscribe method.
#### off(eventName, listener) / removeListener(eventName, Listener)
Remove subscription.#### once(eventName, listener)
Subscribe event for listener only once. It will be unsubscribed after the first call.#### emit(eventName, data?)
Trigger an event asynchronously/synchronously. It is depends on returns value from listener. Listeners are called in the order they were added and executed serially.