Why
Just because simplicity is the ultimate sophistication...
If you need a basic event emitter system, you are in the right place.
For 419 bytes once gzipped, you get on, off and emit methods...
Enjoy !
Let's play !!!

Event listeners (Crtl+Click on one toggles all) :
Playing :
|Left collision :
|Top collision :
|Right collision :
|Bottom collision :
|Paddle collision :
|Brick collision :
|Installation
npm
npm i -D amstramgram-event-emitter
And then in your javascript :
import EventEmitter from 'Amstramgram-Event-Emitter/Light'
Manually
Just download the source and use it in your project.
import EventEmitter from './src_folder/amstramgramEventEmitterLight'
Usage
Basic
//Basic demo
//Instanciation
const emitter = new EventEmitter()
//Same sillyCallback for two events
emitter.on('firstevent secondevent', sillyCallback)
//stupidCallback
emitter.on('firstevent', stupidCallback)
emitter.on('thirdevent', function(text, eventName){
console.log(text + eventName + ' !!!')
})
function sillyCallback(e) {
console.log(e.text, "My nickname is " + e.eventName, e.plus ? "." : "and I'm called by a sillyCallback...")
if (e.plus) console.log(e.plus)
}
function stupidCallback(e) {
console.log(e.text, "My nickname is " + e.eventName, "and I'm called by a stupidCallback...")
}
emitter.emit('firstevent', { text: "Hello, I'm the FirstEvent!!!" })
emitter.emit('secondevent', { text: "Hello, I'm the SecondEvent!!!", plus: "Have a nice day !" })
emitter.emit('thirdevent', "Hi, I am the ThirdEvent ans my nickname is ")
emitter.off('firstevent')
emitter.emit('firstevent', { text: "Hello, I'm the FirstEvent but you'll never see me !!!" })
Open your console to see the result...
Note that the event name is always passed as last argument to the callback.
Normal
However, if you really need a event emitter, it is probably for use its features in your own class :
import EventEmitter from 'Amstramgram-Event-Emitter/Light'
class MyFabulousClass extends EventEmitter {
...
constructor() {
super()
...
setTimeout(() => {
this.emit('initialization-end')
}, 1)
}
...
}
...
const myFabulousClass = new MyFabulousClass()
myFabulousClass.on('initialization-end', _ => {
console.log("I'm ready !")
myFabulousClass.off('initialization-end')
})
API
constructor
const emitter = new EventEmitter()
on
/**
* @description : register a callback for a list of events
* @param {string} eventsNames : list of event names separated by a space
* @param {function} callback : function called each time one event of eventsNames is emitted.
* @returns this so methods can be chained.
**/
emitter.on(eventsNames, callback)
off
/**
* @description : remove a callback for a list of events.
* @param {string} eventsNames : list of event names separated by a space.
* @param {function} callback : callback to removed.
* @returns this so methods can be chained.
**/
emitter.off(eventsNames, callback)
/**
* @description : remove all the registered callbacks for a list of events.
* @param {string} eventsNames : list of event names separated by a space.
* @returns this so methods can be chained.
**/
emitter.off(eventsNames)
/**
* @description : remove the callback for all registered events.
* @param {function} callback : callback to removed.
* @returns this so methods can be chained.
**/
emitter.off(callback)
/**
* @description : remove all registered events and callbacks.
* @returns this so methods can be chained.
**/
emitter.off()
emit
/**
* @description : emit a given event.
* @param {String} eventName : event to emit.
* @param {...any} args parameters passed to the callbacks.
* If args is an object, the property eventName
* with the name of the emitted event is automatically added to it.
* In all other cases, the event name is passed as last argument to the callback.
* @returns this so methods can be chained.
**/
emitter.emit(eventName, args)
events
/**
* @getter {Set} a Set of all registered events.
**/
console.log(emitter.events)
//Output true if 'myevent' is a registered event, false otherwise
console.log(emitter.events.has('myevent'))
eventsAndCallbacks
/**
* @getter {Object}
* Returns an object which contains all the registered events and their callbacks
* {
* eventName01 : set listing the registered callbacks,
* ......
* eventName0n : set listing the registered callbacks
* }
**/
console.log(emitter.eventsAndCallbacks)
//Output true if myCallback will be called by 'myevent', false otherwise
console.log(emitter.events.has('myevent') && emitter.eventsAndCallbacks['myevent'].has(myCallback))