Skip to content

Instantly share code, notes, and snippets.

@harish2704
Created November 23, 2023 08:47
Show Gist options
  • Save harish2704/2cff29440d629a2d21002b3cbb7063bf to your computer and use it in GitHub Desktop.
Save harish2704/2cff29440d629a2d21002b3cbb7063bf to your computer and use it in GitHub Desktop.

Revisions

  1. harish2704 created this gist Nov 23, 2023.
    18 changes: 18 additions & 0 deletions EventBus.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // Source: https://itnext.io/handling-data-with-web-components-9e7e4a452e6e

    class EventBus {
    constructor() {
    this._bus = document.createElement('div');
    }

    register(event, callback) {
    this._bus.addEventListener(event, callback);
    }

    remove(event, callback) {
    this._bus.removeEventListener(event, callback);
    }
    fire(event, detail = {}) {
    this._bus.dispatchEvent(new CustomEvent(event, { detail }));
    }
    }