-
-
Save mrvicadai/acdf3429f45989e2e851924348ba5532 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PubSub { | |
constructor() { | |
this.handlers = []; | |
} | |
subscribe(event, handler, context) { | |
if (typeof context === 'undefined') { | |
context = handler; | |
} | |
this.handlers.push({event: event, handler: handler.bind(context)}); | |
} | |
publish(event) { | |
for (let i = 0; i < this.handlers.length; i++) { | |
if (this.handlers[i].event === event) { | |
this.handlers[i].handler.call(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PubSub = function() { | |
this.handlers = []; | |
}; | |
PubSub.prototype = { | |
constructor: PubSub, | |
subscribe: function(event, handler, context) { | |
if (typeof context === 'undefined') { | |
context = handler; | |
} | |
this.handlers.push({event: event, handler: handler.bind(context)}); | |
}, | |
publish: function(event) { | |
for (var i = 0; i < this.handlers.length; i++) { | |
if (this.handlers[i].event === event) { | |
this.handlers[i].handler.call(); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment