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.
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
})
}
The IGroupableEventBus contract defines a way for dispatching and listening to events independent of underlying technology. It commes with one extra method which is useful for multitennat applications compared to IEventBus.