Skip to content

Instantly share code, notes, and snippets.

@drewdaemon
Last active August 20, 2019 03:40
Show Gist options
  • Save drewdaemon/6fbabc40b99ade57816e3b7079616a16 to your computer and use it in GitHub Desktop.
Save drewdaemon/6fbabc40b99ade57816e3b7079616a16 to your computer and use it in GitHub Desktop.
const createObservable = () => {
let observers = [];
const subscribe = (observer) => {
observers.push(observer);
return () => {
observers = observers.filter(o => o !== observer);
};
};
const publish = (toPublish) => {
observers.forEach(observer => observer(toPublish));
};
return {subscribe, publish};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment