Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created November 1, 2020 14:51
Show Gist options
  • Save etienne-dldc/61130da1bdfe4068b6ae97301e557ea7 to your computer and use it in GitHub Desktop.
Save etienne-dldc/61130da1bdfe4068b6ae97301e557ea7 to your computer and use it in GitHub Desktop.
Naive pub/sub
function Subscription() {
const subs: Array<() => void> = [];
return {
subscribe(cb: () => void) {
subs.push(cb);
// return unsub
return () => {
const index = subs.indexOf(cb);
subs.splice(index, 1);
}
},
emit() {
subs.forEach((cb) => {
cb();
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment