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 Map/Set sugar for modern browsers and node.js


Version published
Weekly downloads
1
Maintainers
1
Install size
14.4 kB
Created
Weekly downloads
 

Readme

Source

arbitrary-emitter

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

Build Status npm version

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 (~500 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 obj = {}
emitter.on(obj, () => doSomething())
// will `doSomething`
emitter.emit(obj)

Emitter API

on(key, action)

Adds the listener action to the Set for the event tagged with key. key can be any type of value.

on returns removeListener method

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

once(key, action)

Adds the listener action to the Set for the event tagged with key. action will be triggered just one time, then it will be removed. key can be any type of value

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

emit(key[, ...args])

Synchronously calls each of the listeners registered for the event tagged with key, and pass the rest of the arguments to them

emitter.on('test', (a, b) => console.log(a + b))
emitter.emit('test', 1, 2) // => 3

trigger(key[, args])

Synchronously calls each of the listeners registered for the event tagged with key, and pass args array as arguments to them

emitter.on('test', (a, b) => console.log(a + b))
emitter.trigger('test', [1, 2]) // => 3

off(key[, action])

Remove action from listeners tagged with key. If no action is specified will remove all listeners tagged with key

emitter.off(key, action) // will remove action from listeners
emitter.off(key) // will remove all the listeners tagged with `key`, and the tag itself

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 28 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