kestanous:emitter

v0.1.0Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

EventEmitter

@raix seems to be working on his version so I guess I will depreacated this in favor of his. For those using this, I have never had an issue with this package so there is no demand to switch. But if you adding this package for the first time you will likely want raix's package.

A server and client event package. On the server it will use node's events and on the client it will use jQuery. Whats nice about this package is it uses the same API for both server and client. This lets you write events that are not specific to the environment.

New emitter

Create a new emitter.

1var emitter = new EventEmitter();

Emitting events

The emit function takes an event name argument and any number of optional arguments.

1emitter.emit('eventName', /*args*/);

The emitter handle will manage your namespacing for you.

1
2var emitter = new EventEmitter();
3emitter.emit('sleep', {userIsSleeping: true});

Listening to events

If you want to listen to the event in the above example you would use the on function.

1emitter.on('sleep', function (state) {
2  if (state.userIsSleeping) {
3    //do something useful
4  }
5});

Api

Node / jQuery like api:

  • on / addListener
  • once / once
  • emit / trigger
  • off / removeListener
  • removeAllListeners

TODO

  • TESTS!
  • More functions? Make an issue or pull request as needed.