import type { Jetstream } from "@skyware/jetstream"; /** * Event operation types supported by Jetstream */ export type EventOperation = "create" | "update" | "delete"; /** * Handler function signature for any event operation */ export type EventHandler<_T extends string = string> = (event: any) => Promise; /** * Configuration for a single collection's event handlers */ export interface CollectionHandlers { collection: string; onCreate?: EventHandler; onUpdate?: EventHandler; onDelete?: EventHandler; } /** * Registry for managing Jetstream event handler registrations. * Eliminates boilerplate by automating handler setup for collections. */ export class EventHandlerRegistry { private registrations: CollectionHandlers[] = []; /** * Register handlers for a collection */ register(handlers: CollectionHandlers): this { this.registrations.push(handlers); return this; // Fluent interface } /** * Apply all registered handlers to a Jetstream instance */ applyTo(jetstream: Jetstream): void { for (const { collection, onCreate, onUpdate, onDelete } of this.registrations) { if (onCreate) { jetstream.onCreate(collection as any, onCreate); } if (onUpdate) { jetstream.onUpdate(collection as any, onUpdate); } if (onDelete) { jetstream.onDelete(collection as any, onDelete); } } } /** * Get all registered collection names */ getCollections(): string[] { return this.registrations.map((r) => r.collection); } /** * Clear all registrations (useful for testing) */ clear(): void { this.registrations = []; } }