arbitrary-emitter
Advanced tools
Comparing version 1.1.0 to 2.0.0
{ | ||
"name": "arbitrary-emitter", | ||
"version": "1.1.0", | ||
"description": "High performance event emitter in ~450 bytes", | ||
"version": "2.0.0", | ||
"description": "Map based event emitter in ~350 bytes", | ||
"main": "arbitrary-emitter.js", | ||
"scripts": { | ||
"linter": "standard arbitrary-emitter.js tests/tests.js", | ||
"test": "node ./tests/tests.js" | ||
"linter": "eslint src/*.js", | ||
"test": "node ./src/tests.js" | ||
}, | ||
@@ -26,5 +26,10 @@ "repository": { | ||
"devDependencies": { | ||
"standard": "^8.0.0", | ||
"tape": "^4.6.0" | ||
"eslint": "6.8.0", | ||
"eslint-config-standard": "14.1.0", | ||
"eslint-plugin-import": "2.19.1", | ||
"eslint-plugin-node": "11.0.0", | ||
"eslint-plugin-promise": "4.2.1", | ||
"eslint-plugin-standard": "4.0.1", | ||
"tape": "4.12.1" | ||
} | ||
} |
arbitrary-emitter | ||
================= | ||
High performance event emitter for modern browsers in ~450 bytes. | ||
ES6 Map based event emitter in ~350 bytes | ||
[![Build Status](https://travis-ci.org/jacoborus/arbitrary-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/arbitrary-emitter) [![npm version](https://badge.fury.io/js/arbitrary-emitter.svg)](https://www.npmjs.com/package/arbitrary-emitter) ![npm dependencies](https://david-dm.org/jacoborus/arbitrary-emitter.svg) | ||
Arbitrary-emitter stores listeners in **ES6 maps**, so you can use any kind of value as key for your events | ||
## Features | ||
This event emitter was designed with 4 goals in mind: | ||
- Be lightweight: **~450 bytes** when gzipped | ||
- Be fast: it's optimized for being quick even with lots of emitters | ||
- Be conventional: with conventional api (`on`, `off`, `once` and `emit`) | ||
- Be modern: it stores listeners in **ES6 maps**, so you can use any kind of value as key for the listeners (See usage example) | ||
It's written in vanilla ES6, so you will have to transpile it before using it in old browsers or node.js < v5.9 | ||
## Usage | ||
Install with [npm](https://www.npmjs.com/package/arbitrary-emitter), clone the repo or download and extract the [zip](https://github.com/jacoborus/arbitrary-emitter/archive/master.zip). Then import or insert it as script tag. | ||
```js | ||
@@ -32,2 +18,13 @@ const emitter = arbitraryEmitter() | ||
- **~350 bytes** when gzipped | ||
- conventional api (`on`, `off`, `once` and `emit`) | ||
- check [weak-emitter](https://github.com/jacoborus/weak-emitter) for a version that uses weakmaps to store events | ||
## Install | ||
Install with [npm](https://www.npmjs.com/package/arbitrary-emitter) or yarn, clone the repo or download and extract the [zip](https://github.com/jacoborus/arbitrary-emitter/archive/master.zip). | ||
Then import or insert it as script tag. | ||
## Emitter API | ||
@@ -42,5 +39,5 @@ | ||
<a name="emitter-on-api"></a> | ||
### on(eventKey, listener) | ||
### on(key, handler) | ||
Adds the `listener` function to the end of the listeners array for the event tagged with `eventKey`. `eventKey` can be any type of value. A check is made to see if the listener has already been added so it won't be called multiple times. Event listeners are invoked in the order they are added. | ||
Adds the `handler` function to the event tagged with `key`. `key` can be any type of value. Every handler will be added once, despite the number of times it was added to the event. Handlers are invoked in the order they were added. | ||
@@ -55,6 +52,6 @@ ```js | ||
<a name="emitter-addonce-api"></a> | ||
### once(eventKey, listener) | ||
<a name="emitter-once-api"></a> | ||
### once(key, handler) | ||
Same as `on`, but `listener` will be triggered just one time, then it will be removed. | ||
Same as `on`, but `listener` will be triggered just once, then it will be removed. | ||
@@ -71,5 +68,5 @@ ```js | ||
<a name="emitter-emit-api"></a> | ||
### emit(eventKey[, options]) | ||
### emit(key[, ...args]) | ||
Synchronously calls each of the listeners registered for the event tagged with `eventKey`, passing the supplied argument `options` to each | ||
Invoke all handlers tagged with `key`, passing the rest of the arguments | ||
@@ -85,7 +82,6 @@ ```js | ||
<a name="emitter-off-api"></a> | ||
### off([eventKey[, listener]]) | ||
### off([key[, handler]]) | ||
- When no argument is passed all the listeners and its eventKeys will be removed from the emitter | ||
- If an `eventKey` but no `listener` is passed all the listeners and its key will be removed | ||
- If `eventKey` and `listener` are passed as arguments just the listener will be removed from its group | ||
- If a `key` but no `handler` is passed the event will be removed | ||
- If `key` and `handler` are passed as arguments just the handler will be removed from the event | ||
@@ -95,20 +91,19 @@ ```js | ||
emitter.off(key) // will remove all the listeners tagged with `key`, and the tag itself | ||
emitter.off() // will remove all the listeners from all the eventKeys and the eventKeys themselves | ||
``` | ||
<a name="emitter-listeners-api"></a> | ||
### listeners(eventKey) | ||
### listeners(key) | ||
Returns a copy of the array of listeners for the event tagged `eventKey` | ||
Create and return an array with all the handlers for the event tagged with `key` | ||
```js | ||
const key = {} | ||
const f1 = () => console.log('f1') | ||
const f2 = () => console.log('f2') | ||
emitter.on(key, f1) | ||
emitter.on(key, f2) | ||
emitter.listeners(key)[0] === f1 // true | ||
emitter.listeners(key)[1] === f2 // true | ||
const f1 = () => alert('hi') | ||
const f2 = () => alert('ho') | ||
emitter.on('a', f1) | ||
emitter.on('a', f2) | ||
emitter.emitters() | ||
// ==> [f1, f2] | ||
``` | ||
@@ -120,4 +115,2 @@ | ||
### Node | ||
```sh | ||
@@ -127,16 +120,2 @@ npm test | ||
### Browser | ||
Build browser tests (`npm run build-tests`) and open `test/test.html` | ||
<a name="building"></a> | ||
## Building | ||
- Build UMD file: `npm run build-umd` | ||
- Build browser tests: `npm run build-tests` | ||
- Run both builds: `npm run build` | ||
<br><br> | ||
@@ -146,2 +125,2 @@ | ||
© 2016 [Jacobo Tabernero](http://jacoborus.codes) - Released under [MIT License](https://raw.github.com/jacoborus/arbitrary-emitter/master/LICENSE) | ||
© 2020 [Jacobo Tabernero](http://jacoborus.codes) - Released under [MIT License](https://raw.github.com/jacoborus/arbitrary-emitter/master/LICENSE) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
10140
7
216
118
1