Class EventBus<TEvents>

EventBus class can be derived from any IEventBusAdapter.

IMPORT_PATH: "@daiso-tech/core/event-bus/implementations/derivables"

Type Parameters

Implements

Constructors

Methods

  • Type Parameters

    Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    const listener: Invokable<AddEvent> = event => {
    console.log(event);
    }
    await eventBus.addListener(AddEvent, listener);
    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
  • Type Parameters

    Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}
    class SubEvent extends BaseEvent<{ c: number, d: number }> {}

    const listener: Invokable<AddEvent | SubEvent> = event => {
    console.log(event)
    }
    await eventBus.addListenerMany([AddEvent, SubEvent], listener);
    await eventBus.dispatchMany([
    new AddEvent({ a: 1, b: 2 }),
    new SubEvent({ c: 1, d: 2 }),
    ]);
  • Type Parameters

    Parameters

    Returns LazyPromise<EventInstance<TEventClass>>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    eventBus.asPromise(AddEvent).then(event => {
    console.log(event);
    });

    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
  • Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
  • Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}
    class SubEvent extends BaseEvent<{ c: number, d: number }> {}

    await eventBus.dispatchMany([
    new AddEvent({ a: 1, b: 2 }),
    new SubEvent({ c: 1, d: 2 }),
    ]);
  • Returns string

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    // Will print "@global"
    console.log(eventBus.getGroup());
  • Type Parameters

    Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    await eventBus.listenOnce(AddEvent, event => {
    console.log(event);
    });

    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
  • Type Parameters

    Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    const listener: Invokable<AddEvent> = event => {
    console.log(event);
    }
    await eventBus.addListener(AddEvent, listener);
    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
    await eventBus.removeListener(AddEvent, listener);
  • Type Parameters

    Parameters

    Returns LazyPromise<void>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent, type Listener } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}
    class SubEvent extends BaseEvent<{ c: number, d: number }> {}

    const listener: Invokable<AddEvent | SubEvent> = event => {
    console.log(event);
    }
    await eventBus.addListenerMany([AddEvent, SubEvent], listener);
    await eventBus.dispatchMany([
    new AddEvent({ a: 1, b: 2 }),
    new SubEvent({ c: 1, d: 2 }),
    ]);
    await eventBus.removeListenerMany([AddEvent, SubEvent], listener);
  • Type Parameters

    Parameters

    Returns LazyPromise<Unsubscribe>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}

    const unsubscribe = await eventBus.subscribe(AddEvent, event => {
    console.log(event);
    });
    await eventBus.dispatch(new AddEvent({ a: 1, b: 2,}));
    await unsubscribe();
  • Type Parameters

    Parameters

    Returns LazyPromise<Unsubscribe>

    import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    class AddEvent extends BaseEvent<{ a: number, b: number }> {}
    class SubEvent extends BaseEvent<{ c: number, d: number }> {}

    const unsubscribe = await eventBus.subscribeMany([AddEvent, SubEvent], event => {
    console.log(event);
    });
    await eventBus.dispatchMany([
    new AddEvent({ a: 1, b: 2 }),
    new SubEvent({ c: 1, d: 2 }),
    ]);
    await unsubscribe();
  • Parameters

    Returns IEventBus<TEvents>

    import type { IGroupableEventBus, IEventBus } from "@daiso-tech/core/event-bus/contracts";
    import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
    import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
    import { BaseEvent } from "@daiso-tech/core/event-bus/contracts";

    const eventBus: IGroupableEventBus = new EventBus({
    adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
    });

    // Will print "@global"
    console.log(eventBus.getGroup());

    const groupedEventBus: IEventBus = eventBus.withGroup("company-1");

    // Will print "@global/company-1"
    console.log(groupedEventBus.getGroup());