An extremely small, single event emitter
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
benchmarks
src
test
.coveralls.yml
.gitignore
.npmignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md
package.json
yarn.lock

README.md

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.