Created
October 31, 2020 21:53
-
-
Save Yamo93/92f0a36fb0a6568635e0b659b7824293 to your computer and use it in GitHub Desktop.
Extended subscribe with unsubscribe return value
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
function subscribe(eventName, callback) { | |
if (!Array.isArray(subscribers[eventName])) { | |
subscribers[eventName] = [] | |
} | |
subscribers[eventName].push(callback); | |
const index = subscribers[eventName].length - 1; | |
return () => { | |
subscribers[eventName].splice(index, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment