Class EventBus<TEvents>

EventBus class can be derived from any IEventBusAdapter.

Type Parameters

Implements

Constructors

Methods

  • The getGroup method returns the complete group.

    Returns string

    import { type IEventBus } from "@daiso-tech/core";

    async function main(eventBus: IEventBus): Promise<void> {
    // Will be "@root"
    console.log(eventBus.getGroup())

    const eventBusA = eventBus.withGroup("a");

    // Will be "@root/a"
    console.log(eventBusA.getGroup())
    }
  • The withGroup method returns new instance of IEventBus where all the events names will be prefixed with a given group. This useful for multitennat applications.

    Parameters

    Returns IEventBus<TEvents>

    import { type IEventBus } from "@daiso-tech/core";

    async function main(eventBus: IEventBus): Promise<void> {
    const eventBusA = eventBus.withGroup("a");
    await eventBusA.subscribe("add", (event) => {
    // This will be logged
    console.log("eventBusA:", event);
    });

    const eventBusB = eventBus.withGroup("b");
    await eventBusB.subscribe("add", (event) => {
    // This will never be logged
    console.log("eventBusB:", event);
    });

    await eventBusA.dispatch({
    type: "add",
    a: 1,
    b: 2
    })
    }