Skip to content

Instantly share code, notes, and snippets.

@Gabri3l
Created August 14, 2016 08:09
Show Gist options
  • Save Gabri3l/5090aea5cb66ed14726e2adfdc8f3d5b to your computer and use it in GitHub Desktop.
Save Gabri3l/5090aea5cb66ed14726e2adfdc8f3d5b to your computer and use it in GitHub Desktop.
function Event() {
let subscribers = [];
const subscribe = (...values) => {
values.forEach((s) => {
if (typeof s === 'function') subscribers.push(s);
});
};
const unsubscribe = (...values) => {
values.forEach((v) => {
if (subscribers.lastIndexOf(v) > -1) subscribers.splice(subscribers.lastIndexOf(v),1);
});
};
const emit = function(...args) {
const subscribersCopy = subscribers.slice();
subscribersCopy.forEach((s) => s.apply(this, args));
return args;
};
return {
subscribe,
unsubscribe,
emit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment