node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »

energy

energy

simple cross-platform event emitter based on EventEmitter

API

Create an emitter instance

var energy = require('energy')
var emitter = energy()

Or use new if you prefer. Either way works :)

var energy = require('energy')
var emitter = new energy()

Methods

Parameters

  • emitter refers to an energy() object
  • event refers to an event name
  • listener refers to a function that listens to an event

#### emitter.on(event, listener)

  • Add listener for event
  • @return emitter

#### emitter.off(event?, listener?, quota?)

  • Remove listener(s)
    • .off() removes all listeners for all events
    • .off(event) removes all event listeners
    • .off(event, listener) removes all occurrences of event listener
    • .off(event, listener, quota) remove quota occurrences of event listener
  • @return emitter

#### emitter.emit(event, ...args)

  • Fire event listeners (in sequence) with the supplied arguments
  • Listeners run in the context of emitter
  • @return integer (number of listeners that fired)

#### emitter.once(event, listener)

  • Add a one-time event listener
  • @return emitter

#### emitter.clone()

  • Clone an emitter at its current state
  • @return a new emitter

#### emitter.listeners(event)

  • Access the listeners array for the specified event
  • @return array (reference)

#### emitter.init()

  • Reinitialize an emitter
  • @return emitter

#### emitter.to(target)

  • Make target emitter-like based on emitter as the source.
  • @return emitter

#### energy.to(target)

  • Make target emitter-like based on a energy() object as the source
  • @return target with emitter methods and properties

#### energy.applies(fns, scope, args, breaker?)

  • Apply each function with scope and args
  • If breaker is defined, then functions can return breaker to abort subsequent applies
  • @return integer (number of functions that fired)

## Compatibility

Works in node and in any browser. Tested in node, Chrome, FF, Opera, IE8

Contribute

To make edits, first fork the repo, clone your fork, and cd into it. Run tests via the commands below and/or in a browser. Make edits in src and test as needed. Push your changes and then submit a pull request. Builds (in the project root) are created later via grunt and should not be changed in pull requests. CLI commands require node and the grunt-cli on your system.

$ npm install -g grunt-cli # install grunt-cli if you haven't already 
$ npm install # install devDependencies from package.json 
$ grunt jshint:sub # lint sub dirs 
$ grunt test # run tests 

Fund

Support this project by tipping the developer =)

License

MIT