Skip to content

Instantly share code, notes, and snippets.

@benqus
Last active August 12, 2021 07:48
Show Gist options
  • Save benqus/28c9cfbdb2d2c1b48712371909da270b to your computer and use it in GitHub Desktop.
Save benqus/28c9cfbdb2d2c1b48712371909da270b to your computer and use it in GitHub Desktop.
export class PubSub {
constructor() {
this._subs = new Map()
}
subscribe(action, fn) {
if (!this._subs.has(action)) {
this._subs.set(action, new Set())
}
this._subs.get(action).add(fn)
}
unsubscribe(action, fn) {
const sub = this._subs.get(action)
sub && sub.delete(fn)
}
publish(action, ...payload) {
const sub = this._subs.get(action)
sub && sub.forEach(fn => fn(...payload))
}
}
@benqus
Copy link
Author

benqus commented Aug 11, 2021

Compatible with Lightning JS on RDK WPEBrowser - without the latest ES features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment