TypeScript implementation of an event bus with priority-based listener execution.
npm installnpm test # Run tests
npm run dev:match # Run soccer match example
npm run dev:download # Run download queue example
npm run build # Build projectimport { EventBus } from "./EventBus";
import { Event, EventPriority } from "./types";
type UserEvent = Event<"user:login", { userId: string }>;
const eventBus = new EventBus();
eventBus.on<UserEvent>("user:login", (event) => {
console.log(`User ${event.data.userId} logged in`);
});
eventBus.post<UserEvent>({
type: "user:login",
timestamp: Date.now(),
data: { userId: "123" },
});import { EventBus } from "./EventBus";
import { DownloadQueue } from "./DownloadQueue";
const eventBus = new EventBus();
// Sync mode: downloads sequentially, events fire in order
const syncQueue = new DownloadQueue(eventBus, { mode: "sync" });
await syncQueue.download(["url1", "url2", "url3"]);
// Async mode: downloads concurrently, events fire as ready
const asyncQueue = new DownloadQueue(eventBus, { mode: "async" });
await asyncQueue.download(["url1", "url2", "url3"]);Run demo: npm run dev:download
on(eventType, listener, options?) - Register a listener
off(eventType, listener) - Unregister a listener
post(event) - Post an event to listeners
clear(eventType?) - Remove listeners
listenerCount(eventType) - Get listener count
eventTypes() - Get registered event types
Listeners execute in priority order (highest first):
eventBus.on("event", listener1, { priority: EventPriority.HIGH });
eventBus.on("event", listener2, { priority: EventPriority.NORMAL });