SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class EventEmitter {
  2.   constructor() {
  3.     this.cb = {};
  4.   }
  5.    
  6.   on(event, fn) {
  7.     if (!this.cb[event]) this.cb[event] = [];
  8.     this.cb[event].push(fn);
  9.   }
  10.    
  11.   emit(event, payload) {
  12.     this.cb[event] && this.cb[event].forEach(fn => { fn(payload) });
  13.   }
  14. }
  15.  
  16. var server = new EventEmitter;  
  17.  
  18. server.on('request', function(request) {  
  19.   request.approved = true;  
  20. });  
  21.  
  22. server.on('request', function(request) {  
  23.   console.log(request);  
  24. });  
  25.  
  26.  
  27. server.emit('request', {from: "Client"});
  28.  
  29. server.emit('request', {from: "Another Client"});
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top