Skip to content

Instantly share code, notes, and snippets.

@Yamo93
Created October 31, 2020 21:53
Show Gist options
  • Save Yamo93/92f0a36fb0a6568635e0b659b7824293 to your computer and use it in GitHub Desktop.
Save Yamo93/92f0a36fb0a6568635e0b659b7824293 to your computer and use it in GitHub Desktop.
Extended subscribe with unsubscribe return value
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