Skip to content

Instantly share code, notes, and snippets.

@hanayashiki
Created December 17, 2019 02:53
Show Gist options
  • Save hanayashiki/e2dbe11961863cb007526aee0e63bd4c to your computer and use it in GitHub Desktop.
Save hanayashiki/e2dbe11961863cb007526aee0e63bd4c to your computer and use it in GitHub Desktop.
Allow
class CallbackSequence extends Function {
constructor() {
super();
this.callbacks = {};
return new Proxy(this, {
apply: (target, thisArg, argumentsList) => {
this.__call__();
}
});
}
__call__ = () => {
for (let [key, cb] of Object.entries(this.callbacks)) {
cb();
}
};
add = (key, callback) => {
this.callbacks[key] = callback;
};
remove = (key) => {
delete this.callbacks[key];
};
}
/*
* If you want to call multiple functions when a event, for example, 'window.resize' happens, do:
* window.onresize = windows.onresize || new CallbackSequence();
* window.onresize.add("1", () => { console.log ("We wish you a merry Christmas. "); })
* window.onresize.add("2", () => { console.log ("We wish you a merry Christmas. "); })
* window.onresize.add("3", () => { console.log ("And happy new year! "); })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment