JSPM

  • Created
  • Published
  • Downloads 318712
  • Score
    100M100P100Q166885F
  • License MIT

Simple and tiny (101 bytes) event emitter library

Package Exports

  • nanoevents
  • nanoevents/unbind-all

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nanoevents) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Nano Events

Simple and tiny event emitter library for JavaScript.

  • Only 101 bytes (minified and gzipped). It uses Size Limit to control size.
  • on method returns unbind function. You don’t need to save callback to variable for removeListener.
  • No aliases, just emit and on methods. No Node.js EventEmitter compatibility.
import NanoEvents from 'nanoevents'
const emitter = new NanoEvents()

const unbind = emitter.on('tick', volume => {
  summary += volume
})

emitter.emit('tick', 2)
summary //=> 2

unbind()
emitter.emit('tick', 2)
summary //=> 2
Sponsored by Evil Martians

Usage

Mixing to Object

Because Nano Events API has only just 2 methods, you could just create proxy methods in your class.

class Ticker {
  constructor() {
    this.emitter = new NanoEvents()
  }
  on() {
    return this.emitter.on.apply(this.emitter, arguments)
  }
  tick() {
    this.emitter.emit('tick')
  }
}

It allows you to have custom JSDoc or type definitions.

Add Listener

Use on method to add listener for specific event:

emitter.on('tick', number => {
  console.log(number)
})

emitter.emit('tick', 1)
// Prints 1
emitter.emit('tick', 5)
// Prints 5

In case of your listener relies on some particular context (if it uses this within itself) you have to bind required context explicitly before passing function in as a callback.

const app = {
  userId = 1,
  showUserId() {
    console.log(this.userId)
  }
}
emitter.on('print', app.showUserId.bind(app))

Remove Listener

Methods on returns unbind function. Call it and this listener will be removed from event.

const unbind = emitter.on('tick', number => {
  console.log('on ' + number)
})

emitter.emit('tick', 1)
// Prints "on 1"

unbind()
emitter.emit('tick', 2)
// Prints nothing

Execute Listeners

Method emit will execute all listeners. First argument is event name, others will be passed to listeners.

emitter.on('tick', (a, b) => {
  console.log(a, b)
})
emitter.emit('tick', 1, 'one')
// Prints 1, 'one'

Events List

You can get used events list by events property.

const unbind = emitter.on('tick', () => { })
Object.keys(emitter.events) //=> ["tick"]

unbind()
Object.keys(emitter.events) //=> []

Once

If you need add event listener only for first event dispatch, you can use this snippet:

class Ticker {
  constructor() {
    this.emitter = new NanoEvents()
  }once (event, callback) {
    const unbind = this.emitter.on(event, function () {
      unbind()
      callback.apply(this, arguments)
    })
    return unbind
  }
}

Remove all listeners

unbindAll method will remove all listeners to all events.

import unbindAll from 'nanoevents/unbind-all';

emitter.on('event1', () => { })
emitter.on('event2', () => { })

unbindAll(emitter);

Object.keys(emitter.events) //=> { }