Skip to content

Instantly share code, notes, and snippets.

@streamich
Last active March 13, 2018 11:55
Show Gist options
  • Save streamich/85e8066ab358103ff9897424e6051c0d to your computer and use it in GitHub Desktop.
Save streamich/85e8066ab358103ff9897424e6051c0d to your computer and use it in GitHub Desktop.

PubSub class like this:

class PubSub {
  constructor () {
    this.el = document.createElement('div');
  }

  subscribe (eventName, handler) {
    this.el.addEventListener(eventName, handler);
  }

  unsubscribe (eventName, handler) {
    this.el.removeEventListener(eventName, handler);
  }

  publish (eventName, ...args) {
    const event = new Event(eventName);
    event.args = args;
    this.el.dispatchEvent(event);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment