Skip to content

Instantly share code, notes, and snippets.

@zinevych
Created May 30, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinevych/5ee2cce44878b491074fdfaa22c7c2a1 to your computer and use it in GitHub Desktop.
Save zinevych/5ee2cce44878b491074fdfaa22c7c2a1 to your computer and use it in GitHub Desktop.
function Click() {
this.handlers = []; // observers
}
Click.prototype = {
subscribe: (fn) => {
this.handlers.push(fn);
},
unsubscribe: (fn) => {
this.handlers = this.handlers.filter(...);
},
notify: (o, thisObj) => {...}
}
function run() {
const clickHandler = (item) => {
log.add("fired: " + item);
};
let click = new Click();
click.subscribe(clickHandler);
click.notify('event #1');
click.unsubscribe(clickHandler);
click.notify('event #2');
click.subscribe(clickHandler);
click.notify('event #3');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment