Guest User

Untitled

a guest
Jun 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. function Event(name){
  2. this.name = name;
  3. this.callbacks = [];
  4. }
  5. Event.prototype.registerCallback = function(callback){
  6. this.callbacks.push(callback);
  7. }
  8.  
  9. function Reactor(){
  10. this.events = {};
  11. }
  12.  
  13. Reactor.prototype.registerEvent = function(eventName){
  14. var event = new Event(eventName);
  15. this.events[eventName] = event;
  16. };
  17.  
  18. Reactor.prototype.dispatchEvent = function(eventName, eventArgs){
  19. this.events[eventName].callbacks.forEach(function(callback){
  20. callback(eventArgs);
  21. });
  22. };
  23.  
  24. Reactor.prototype.addEventListener = function(eventName, callback){
  25. this.events[eventName].registerCallback(callback);
  26. };
  27.  
  28. var reactor = new Reactor();
  29.  
  30. reactor.registerEvent('big bang');
  31.  
  32. reactor.addEventListener('big bang', function(){
  33. console.log('This is big bang listener yo!');
  34. });
  35.  
  36. reactor.addEventListener('big bang', function(){
  37. console.log('This is another big bang listener yo!');
  38. });
  39.  
  40. reactor.dispatchEvent('big bang');
Add Comment
Please, Sign In to add comment