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'
Manually
Just download the source and use it in your project.
import EventEmitter from './src_folder/amstramgramEventEmitter'
Usage
Basic
//Basic demo
//Instanciation with two empty strings so events 'eventadded' and 'eventremoved' are not dispatched
const emitter = new EventEmitter('', '')
//Same sillyCallback for two events
emitter.on('firstevent secondevent', sillyCallback)
//stupidCallback will be called once only
emitter.once('thirdevent', stupidCallback)
function sillyCallback(e) {
console.log(e.text + ' And my original name is : ' + e.eventName)
if (e.plus) console.log(e.plus)
}
function stupidCallback(e1, e2, eventName) {
console.log(e1 + eventName + ' !!!', e2)
}
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', "Hello, I'm the ThirdEvent!!!", "You'll never see me again !!!")
emitter.emit('thirdevent', "You'll never see me !!!")
Open your console to see the result...
Note that :
- if there is only one argument and that argument is an object, an eventName property with the name of the event is automatically added to it.
- in all other cases, the event name is 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'
class MyFabulousClass extends EventEmitter {
...
constructor() {
super()
...
setTimeout(() => {
this.emit('initialization-end')
}, 1)
}
...
}
...
const myFabulousClass = new MyFabulousClass()
myFabulousClass.once('initialization-end', _ => console.log("I'm ready !"))
API
constructor
/**
* @param {String} [eventAdded = "eventadded"] name of the event emitted when a new event/callback is added.
* If it's an empty string or not a string, no event will be dispatched when a event/callback is registered.
* @param {String} [eventRemoved = "eventremoved"] name of the event emitted when a new event/callback is removed.
* If it's an empty string or not a string, no event will be dispatched when a event/callback is unregistered
**/
constructor(eventAdded = 'eventadded', eventRemoved = 'eventremoved')
//If you don't need eventadded and eventremoved events :
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)
once
/**
* @description : register a callback for a list of events.
* This callback will be called only once for each registered event.
* @param {string} eventsNames : list of event names separated by a space.
* @param {function} callback : function called only once for each emitted event of eventsNames.
* @returns this so methods can be chained.
**/
emitter.once(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.
**/
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 : {
* callbacks : set listing the registered callbacks
* callbacksOnce : set listing the registered callbacks once
* },
* ......
* eventName0n : {
* callbacks : set listing the registered callbacks
* callbacksOnce : set listing the registered callbacks once
* }
* }
**/
console.log(emitter.eventsAndCallbacks)
//Output true if myCallback will be called by 'myevent', false otherwise
console.log(emitter.events.has('myevent') && emitter.eventsAndCallbacks['myevent'].callbacks.has(myCallback))