Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 19 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 (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-25T09:25:58.000Z (over 5 years ago)
- Last Synced: 2024-10-11T21:32:44.969Z (25 days 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
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
[![Node version](https://img.shields.io/node/v/es6-emitter.svg?style=flat-square)](https://www.npmjs.org/package/es6-emitter)
[![Build Status](https://img.shields.io/travis/kutyel/es6-emitter/master.svg?style=flat-square)](https://travis-ci.org/kutyel/es6-emitter)
[![Coverage Status](https://img.shields.io/coveralls/kutyel/es6-emitter.svg?style=flat-square)](https://coveralls.io/github/kutyel/es6-emitter)
[![Dependency status](https://img.shields.io/david/kutyel/es6-emitter.svg?style=flat-square)](https://david-dm.org/kutyel/es6-emitter)
[![Dev Dependencies Status](https://img.shields.io/david/dev/kutyel/es6-emitter.svg?style=flat-square)](https://david-dm.org/kutyel/es6-emitter#info=devDependencies)
[![NPM Status](https://img.shields.io/npm/dm/es6-emitter.svg?style=flat-square)](https://www.npmjs.org/package/es6-emitter)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](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).