interface IEventBus
Since 0.1.0

In-memory publish/subscribe event bus for domain events.

Examples

Example 1

const events = ctx.services.get<IEventBus>(CAPABILITIES.EVENTS);
events.subscribe<UserCreated>('UserCreated', async (event) => {
  await sendWelcomeEmail(event.data.email);
});

Methods

publish<T>(event: IDomainEvent<T>): Promise<void>

Publishes an event to every subscriber of its type.

publishBatch(events: IDomainEvent[]): Promise<void>

Publishes multiple events, each to its own subscribers.

subscribe<T>(
type: string,
handler: EventHandler<T>
): Unsubscribe

Subscribes to an event type.

Usage

import { type IEventBus } from "events-plugin/src/index.ts";