Extend the node EventEmitter class to support an emitAndListen method to automatically check for new listeners.
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
specs
.gitignore
README.md
package.json

README.md

#FutureEventEmitter

Installation

npm install FutureEventEmitter

Usage

var emitter = new FutureEventEmitter();

emitter.emitAndListen('eventName', 42);

emitter.on('eventName', function(answer) {
	console.log('Called event with: ' + answer);
});

Outputs:

Called event with: 42

Using emitAndListen, the event will be called whether the listener is created before and after it.

This replaces the traditional pattern with the standard EventEmitter which would look like this:

var emitter = new EventEmitter();

emitter.emit('eventName', 42);
emitter.on('newListener', function(eventName, listener) {
	if (eventName == 'eventName') {
		listener(42);
	}
});

emitter.on('eventName', function(answer) {
	console.log('Called event with: ' + answer);
});