0.1.5 • Published 7 years ago
event-handler-manager v0.1.5
EventHandlerManager
Event Handler Manager is a Typescript (also Javascript) library, that, in a simple way creates "events" (and you can add several actions to those) that you can use into your own classes.
How to install and use
var EventHandlerManager = require('event-handler-manager');
var myEventHandler = new EventHandlerManager.EventHandlerManager();
// first attach the events you want to create
myEventHandler.attach(['beforestart', 'afterstart']);
// Then set an action (as function) on one of the events created
// If you are going to make an async function, make sure to return a promise
// like in the example bellow:
myEventHandler.on('beforestart', (event, sender) => {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log(event + ' 1: executed ' + num++);
resolve();
}, 500);
});
});
// you can add more actions to the same event,
// all will be executed before the trigger function returns.
myEventHandler.on('beforestart', (event, sender) => {
console.log(event + ' 2: executed ' + num++);
});
// once everything is set, you can trigger the event.
// the trigger method receives a second parameter that indicates
// if all the actions queued on that event will be executed on a sequentially way
// by default is set to false. And the third argument for this method,
// sets the execution order of the queue, if the previous parameter is set to true.
// For reverse order set this last paramater as 'reverse'. Like bellow
myEventHandler.trigger('beforestart', true, 'reverse').then(() => {
console.log('event beforestart has ended');
//your custom code here
console.log('some work have been done...');
});
// And thats it...
Check sample folder in code for implementation. Change things to test on your own.
Notes
You can extend from this class to your own class. And then, you will be able to use all the features of this library directly in your class.
If you are going to use this library with webpack, please add this aliases inside your webpack.config.js:
alias: {
"event-handler-manager": "event-handler-manager/dist/event-handler-manager"
}