Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

arbitrary-emitter

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

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

  • 0.9.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
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

Package last updated on 28 May 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc