EE

a small event emitter library in JS

EE is a small event emitter library in JS. It weighs < 1 kilobyte minified and approx. 350 bytes minified and gzip'd.
It is inspired by Smoke Signals, which is another event emitter library in JS. You can download it from below.

Download

Download EE as ee2.js

License

This software ( !! ) is licensed under MIT License.
See this page for more info.

Usage

Include it in your HTML document by using a html tag:


<script src="ee2.js" async></script>

Or, in a node.js environment:


var EE = require('ee');

Or, use it as a AMD module:


require(['EE'] , function(EE){
 // ...
});

It can be used in many ways.

Such as:


var e = EE;
// or,
var e = EE.copy();
// NEW: or,
var e = EE.convert({});
/// you can use any object to convert into an event listener
e.on('earthquake',function(){
 you.fleeAway({outside:'house'});
});
// after some time...
e.emit('earthquake');
// More ways:
EE.on('e1',function(){
 event1();
}).on('e2',function(){
 event2();
}).on('e3',function(){
 event3();
}).end();
// then
EE.emit('e2').emit('e3').emit('e1');
// Some other way...
EE.once('once-event',function(){
 console.log('Once upon a time,' , ' there was a quick brown fox that jumped over a lazy dog.');
}).emit('once-event');
// prints what you expect, but
EE.emit('once-event'); //  doesn't print anything
/**
* All in one
*/
EE.convert({}).on('run',function(){
document.write('Run!');
}).once('run',function(){
document.write('Once Run!');
}).on('what',function(){
document.write('What?!');
}).on('play',function(){
document.write('Playing!');
}).emit('run').off('what').emit('play').emit('run').copy().end();

API

EE puts itself in global context.

It has only 7 functions/methods.

EE.on(eventName,callback) adds callback as a listener for eventName.
callback is a function and eventName is a string. The callback may receive arguments when emit is be called.

EE.off(eventName[,callback]) removes all the listeners or the specified callback (if it is given) for eventName .
eventName is a string and callback is an optional function.

EE.once(eventName,callback) adds callback as a listener for eventName . The difference from the EE.on function is simple, the callback will be called only once.
eventName is a string and callback is a function.

EE.emit(eventName[,arg1[,arg2[,arg3...]]]) triggers eventName event by calling all callbacks registered for eventName. If more arguments are given, those will be given to the callback functions. This function can be called multiple times.
eventName is a string and arg1,arg2,arg3... can be of any type including undefined. So, they are optional.

EE.copy() returns an object that has all of the functions listed here. You can use this function if you don't like or want or can't use EE.

EE.convert(obj) converts obj into an event emitter by putting all of these functions into the obj and returns the changed obj.
obj is an object.

EE.end() is a dummy function to end the method chain created by the above functions.
It returns nothing.

Version

2.0-alpha

NOTE:

If you use object retured by EE.copy() or EE.convert() , please keep in mind that the list of listeners remain unchanged and listeners which are added by calling the returned object add the listeners to the internal listeners.
So, the following code doesn't throw any error:


var e = EE.convert({});
e.on('load', function(){
 console.log(arguments.join("\n"));
});
EE.emit('load',true,false,1,2,3);
/// outputs:
//   true
//   false
//   1
//   2
//   3
// that is, no errors! 

This feature may be good or bad, it will be removed in v 3.0 .

Bugs and Contribution

Send an email to admin at my website's domain name (see the address bar of your browser).


You can play it with by opening your browser's JavaScript console and access it as EE.


Thanks for visting this website!


Source Code

Now, the source code is available as Github Gist. See the source below.