Last active
August 12, 2021 07:48
-
-
Save benqus/28c9cfbdb2d2c1b48712371909da270b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compatible with Lightning JS on RDK WPEBrowser - without the latest ES features