The use method will throw an error if you provide it unregisted driver. If no default driver is defined an error will be thrown by use method.
import { type IEventBusFactory } from "@daiso-tech/core";
async function main(eventFactory: IEventBusFactory): Promise<void> {
// Will dispatch envent using the default driver
await eventBusFactory
.use()
.dispatch({ type: "add", a: 1, b: 2 });
// Will dispatch envent using the redis addapter
await eventBusFactory
.use("redis")
.dispatch({ type: "add", a: 1, b: 2 });
}
The withTypes method is used to set the event types of the IEventBus.
import { type IEventBusFactory } from "@daiso-tech/core";
async function main(eventBusFactory: IEventBusFactory): Promise<void> {
type AddEvent = {
type: "add";
a: number;
b: number;
};
await eventBusFactory
.withTypes<AddEvent>()
.use()
// You will se an typescript error
.dispatch({ type: "add" });
}
Example