Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _CustomEvent = (function(){
  2.  
  3.     function _CustomEvent(name, callbacks){
  4.         this.name = name;
  5.         this.callbacks = callbacks || [];
  6.     }
  7.  
  8.     _CustomEvent.prototype.registerCallback = function(callback){
  9.         this.callbacks.push(callback);
  10.     }
  11.  
  12.     return function(name, callbacks){
  13.         return new _CustomEvent(name, callbacks);
  14.     }
  15.  
  16. })();
  17.  
  18. var _Reactor = (function(){
  19.  
  20.     function _Reactor(events){
  21.         this.events = events || {};
  22.     }
  23.  
  24.     _Reactor.prototype.exists = function(eventName){
  25.         return this.events.hasOwnProperty(eventName);
  26.     }
  27.  
  28.     _Reactor.prototype.addEventListener = function(eventName, callback){
  29.         if(!this.exists(eventName)){
  30.             this.events[eventName] = new _CustomEvent(eventName);
  31.         }
  32.  
  33.         this.events[eventName].registerCallback(callback);
  34.     }
  35.  
  36.     _Reactor.prototype.dispatchEvent = function(eventName, eventArgs){
  37.         this.events[eventName].callbacks.forEach(function(callback){
  38.             callback(eventArgs);
  39.         });
  40.     }
  41.  
  42.     return function(events){
  43.         return new _Reactor(events);
  44.     }
  45.  
  46. })();
  47.  
  48. export default _Reactor;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement