Events is a script that you can bind and trigger custom events into your javascript objects.
No dependencies
npm i https://github.com/mowses/Events
Include Events.js in your project:
import Events from '@mowses/events';
Defining an object and its custom events:
const Game = function () {
this.events = new Events([
'load game',
'refresh game',
'attack',
'attacked',
]);
}
No other events can be bound or triggered if not present in the array above.
Setting callbacks for the defined events:
var game = new Game();
game.events
.on(['load game', 'refresh game'], function () {
console.log('game loaded or refreshed');
})
.on('attack', function (enemy, damage) {
enemy.hp -= damage;
})
.on('attacked', function(attacker) {
notification.send(`You have been attacked by ${attacker.name}`);
});
Triggering your events:
// example of triggering an invalid event
game.events.trigger('invalid event'); // will throw a console warning
// passing custom argument to event callback
game.events.trigger('attack', enemy, 50);
game.events.trigger('attacked', enemy);