@solumjs/events
v0.3.5
Published
SolumJS events: in-process event bus with @EventListener
Readme
@solumjs/events
Event-driven architecture with publish/subscribe pattern.
Install
npm install @solumjs/eventsEventBus
import { EventBus } from "@solumjs/events";
import { Bean, inject } from "@solumjs/core";
@Bean("IUserService")
export class UserService {
constructor(@inject("IEventBus") private eventBus: EventBus) {}
async createUser(dto: CreateUserDto): Promise<User> {
const user = await this.userRepo.save(new User(dto));
this.eventBus.publish("USER_CREATED", { userId: user.id, email: user.email });
return user;
}
}@EventListener
import { EventListener } from "@solumjs/events";
export class UserCreatedListener {
@EventListener("USER_CREATED")
async handleUserCreated(payload: { userId: string; email: string }) {
console.log(`User created: ${payload.email}`);
}
}@TransactionalEventListener
Runs after the parent transaction commits. Only fires if the transaction succeeds.
import { TransactionalEventListener } from "@solumjs/events";
export class UserAuditListener {
@TransactionalEventListener("USER_CREATED")
async handleAfterCommit(payload: { userId: string }) {
await this.auditRepo.log("USER_CREATED", payload);
}
}@Async
Runs the listener asynchronously (fire-and-forget). Does not block the publisher.
import { EventListener, Async } from "@solumjs/events";
export class UserWelcomeListener {
@EventListener("USER_CREATED")
@Async()
async sendWelcomeEmail(payload: { userId: string; email: string }) {
await this.emailService.sendWelcome(payload.email);
}
}DomainEvent Interface
import { DomainEvent } from "@solumjs/events";
interface DomainEvent<T = unknown> {
type: string;
payload: T;
occurredAt: string;
}License
MIT
