1.0.0 • Published 2 years ago

@willfiore/event-dispatcher v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

event-dispatcher

Fully typed event dispatcher.


Simple usage example:

Within your class:

// [1] Define events and payloads
type ConnectionEvent = {
    "connect": { id: string, timestamp: number },
    "disconnect": { id: string, timestamp: number, reason?: string },
};

// [2] Extend your class
class Connection extends EventDispatcher<ConnectionEvent> {
    constructor() { super(); }

    private dispatchDisconnect() {
        // [3] Call dispatch
        this.dispatch(
            "disconnect",
            { id: "abcdefg", timestamp: 1675644952, reason: "timed out" },
        );
    }
}

From calling code:

function handleDisconnect(ev: ConnectionEvent["disconnect"]) {
    console.log("Disconnected:", ev.id, ev.timestamp, ev.reason);
}

const connection = new Connection();

connection.addEventListener("disconnect", handleDisconnect);

API

Public

  • EventDispatcher.addEventListener(name, func)
  • EventDispatcher.removeEventListener(name, func)
  • EventDispatcher.removeAllEventListeners()

Protected

  • EventDispatcher.dispatch(name, payload)
1.0.0

2 years ago