JSPM

event-handler-manager

0.1.5
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 10
    • Score
      100M100P100Q47785F
    • License MIT

    A typescript (also javascript) simple Event Handler Manager

    Package Exports

    • event-handler-manager

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (event-handler-manager) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    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"
    }