diode

6.3.2 • Public • Published

NPM


Build Status Coverage Status


A dead simple event emitter. Diode only has one event.

Diode.listen(callback)
Diode.emit()

It is also small (see API). We found ourselves building something similar to it on several projects and decided it was better to keep it in one place.

Diode is both an event emitter and a decorator that can add event subscription to another object:

var MyData = Diode({
  data: [],
  add: function(record) {
    this.data.push(record)
    this.publish()
  }
})

New instances of Diode

Diode also supports the new operator:

var myDiode = new Diode()

Managing scope

By providing a second argument to listen, callbacks will be executed within a given context:

var emitter = new Diode()
 
emitter.listen(function() {
  assert.equal(this, 'custom context')
}, 'custom context')
 
emitter.emit()

API

listen(callback, &scope)

Add a callback. If a second argument is provided, the callback will be executed within that context.

Alias: subscribe.

ignore(callback)

Remove a callback.

Alias: unsubscribe.

emit(...arguments)

Trigger all subscriptions.

Alias: 'publish'


Code At Viget

Visit code.viget.com to see more projects from Viget.

Readme

Keywords

none

Package Sidebar

Install

npm i diode

Weekly Downloads

613

Version

6.3.2

License

MIT

Last publish

Collaborators

  • nhunzaker
  • lawson
  • viget