Created
December 17, 2019 02:53
-
-
Save hanayashiki/e2dbe11961863cb007526aee0e63bd4c to your computer and use it in GitHub Desktop.
Allow
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 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