https://github.com/kutyel/es6-emitter
🚀 Smallest event emitter for JavaScript with all the power of ES6 Maps!
https://github.com/kutyel/es6-emitter
es6 es6-map eventemitter events pubsub
Last synced: 17 days ago
JSON representation
🚀 Smallest event emitter for JavaScript with all the power of ES6 Maps!
- Host: GitHub
- URL: https://github.com/kutyel/es6-emitter
- Owner: kutyel
- License: mit
- Created: 2017-07-24T11:52:27.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-25T09:25:58.000Z (almost 6 years ago)
- Last Synced: 2025-02-16T06:01:43.960Z (3 months ago)
- Topics: es6, es6-map, eventemitter, events, pubsub
- Language: JavaScript
- Homepage:
- Size: 74.2 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# es6-emitter
[](https://github.com/feross/standard)
[](https://www.npmjs.org/package/es6-emitter)
[](https://travis-ci.org/kutyel/es6-emitter)
[](https://coveralls.io/github/kutyel/es6-emitter)
[](https://david-dm.org/kutyel/es6-emitter)
[](https://david-dm.org/kutyel/es6-emitter#info=devDependencies)
[](https://www.npmjs.org/package/es6-emitter)
[](https://paypal.me/flaviocorpa)> Smallest event emitter for JavaScript with all the power of ES6 Maps!
## Install
```bash
$ npm install es6-emitter --save
```## Usage
```javascript
import Emitter from 'es6-emitter'const em = new Emitter()
```## API
### subscribe (name, callback)
Allows you to add subscriptions to your emitter given a certain name, **multiple subscriptions** under the same name are allowed!
```js
const sub1 = em.subscribe('myEvent', foo => console.log('a callback!'));const sub2 = em.subscribe('myEvent', (bar, baz) => console.log('another callback!'));
sub1(); // releases the first subscription
```**Returns** a function to release the subscription while the others remain intact.
#### name
*Required*
Type: `any`Can be literally [any](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) type, and corresponds to the name of the event that you want the Emitter to subscribe to.
#### callback
*Required*
Type: `function`Side effect that you want to associate with the name of the event.
### emit (name[, args])
Allows you to emit any subscription added to the emitter, with any number of arguments.
```js
em.emit('myEvent', 1, '2', null, () => 4);
```**Returns** an array with every return value for each subscription callback.
```js
const s1 = em.subscribe('otherEvent', () => 1);
const s2 = em.subscribe('otherEvent', () => 2);
const s3 = em.subscribe('otherEvent', () => true);
const result = em.emit('otherEvent'); // > [1, 2, true]
```#### name
*Required*
Type: `any`Just as in the `subscribe` function, this is the `name` of the event you want to emit.
#### args
Any number of values of `any` type to be passed to the subscription functions.
## ES6 Map Awesomeness ✨
Thanks to **ES Maps** goodness, the `name` of the event subscribed can be literally *anything*, even another function! 😱
```js
const em = new Emitter();const eventObject = { name: 'click', debounce: 300 };
const eventFunction = e => console.log(e);const sub1 = em.subscribe(NaN, () => 'not a number');
const sub2 = em.subscribe(eventObject, () => 'called with an object!');
const sub3 = em.subscribe(eventFunction, () => 'called with a function!');em.emit(NaN); // > ['not a number']
em.emit(eventObject); // > ['called with an object!']
em.emit(eventFunction); // > ['called with a function!']
```## License
MIT © [Flavio Corpa](https://github.com/kutyel).