Socket
Socket
Sign inDemoInstall

arbitrary-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    arbitrary-emitter

Event emitter with ES6 Map sugar for modern browsers and node.js


Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Install size
12.9 kB
Created
Weekly downloads
 

Readme

Source

arbitrary-emitter

Event emitter with ES6 Map sugar for modern browsers and node.js (~400 bytes). arbitrary-emitter.jacoborus.codes

Build Status npm version npm dependencies

arbitrary-emitter stores listeners and actions in Maps, this allows to use arbitrary values as keys for your listeners.

It's written in vanilla ES6, so you will have to transpile it before using it in old browsers or node.js < v5.9

Features

  • works in browsers and node.js
  • allows to use arbitrary values as keys for listeners
  • really small footprint (~400 bytes when gzipped)
  • blazing fast
  • conventional api (on, off, once and emit)
  • on method returns an unsubscription function (like in redux.js)

Usage

Install with npm, clone the repo or download and extract the zip. Then import or insert it as script tag.

const emitter = arbitraryEmitter()
const key = {}
emitter.on(key, () => doSomething())
// will `doSomething`
emitter.emit(key)

Emitter API

on(eventKey, listener)

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.

on returns removeListener method

const key = {}
let removeListener = emitter.on(key, () => doSomething())
emitter.emit(key) // will `doSomething`
emitter.emit(key) // will `doSomething`
removeListener()
emitter.emit(key) // won't do anything

once(eventKey, listener)

Same as on, but listener will be triggered just one time, then it will be removed.

const key = {}
emitter.once(key, () => doSomethingOnce())
emitter.emit(key) // will `doSomethingOnce`
emitter.emit(key) // won't do anything

emit(eventKey[, options])

Synchronously calls each of the listeners registered for the event tagged with eventKey, passing the supplied argument options to each

emitter.on('test', (opts) => console.log(opts.test))
const options = { test: 'Testing!' }
emitter.emit('test', options) // => 'Testing!'

off([eventKey[, listener]])

  • 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
emitter.off(key, action) // will remove action from listeners
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

Testing

Node

npm test

Browser

Build browser tests (npm run build-tests) and open test/test.html

Building

  • Build UMD file: npm run build-umd
  • Build browser tests: npm run build-tests
  • Run both builds: npm run build




Š 2016 Jacobo Tabernero - Released under MIT License

Keywords

FAQs

Last updated on 31 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc