import { ComKey, Item, ItemTypeArray, LocKeyArray, PriKey } from '@fjell/types'; import { BaseEvent } from './events'; import { Subscription, SubscriptionOptions } from './subscription'; /** * Core EventEmitter interface that storage libraries implement. * Each item type gets its own EventEmitter instance for full type safety. * Libraries implement separate EventEmitters per item type (UserEventEmitter, MessageEventEmitter, etc.) */ export interface EventEmitter { /** * Emit a generic event with full control over event properties. * Libraries can use this for custom events or when they need full control. */ emit(event: BaseEvent): Promise; /** * Emit a create event when an item is created. * Convenience method that constructs a properly typed CreateEvent. */ emitCreate(key: PriKey | ComKey, scopes: string[], item: Item): Promise; /** * Emit an update event when an item is modified. * Convenience method that constructs a properly typed UpdateEvent. */ emitUpdate(key: PriKey | ComKey, scopes: string[], changes: string[], before?: Item, after?: Item): Promise; /** * Emit a delete event when an item is deleted. * Convenience method that constructs a properly typed DeleteEvent. */ emitDelete(key: PriKey | ComKey, scopes: string[], item?: Item): Promise; /** * Emit an action event when a custom action is performed. * Convenience method that constructs a properly typed ActionEvent. */ emitAction(key: PriKey | ComKey, scopes: string[], actionName: string, actionData?: Record): Promise; /** * Create a scoped emitter that automatically includes the specified scopes. * Libraries can use this to avoid passing scopes to every emit call. */ withScopes(scopes: string[]): ScopedEventEmitter; } /** * Scoped EventEmitter that automatically includes configured scopes. * Convenience interface for libraries to avoid passing scopes repeatedly. */ export interface ScopedEventEmitter { /** The scopes that will be automatically included in all events */ readonly scopes: string[]; /** * Emit a generic event with automatic scope inclusion. * The event should omit scopes since they'll be added automatically. */ emit(event: Omit, 'scopes'>): Promise; /** * Emit a create event with automatic scope inclusion. */ emitCreate(key: PriKey | ComKey, item: Item): Promise; /** * Emit an update event with automatic scope inclusion. */ emitUpdate(key: PriKey | ComKey, changes: string[], before?: Item, after?: Item): Promise; /** * Emit a delete event with automatic scope inclusion. */ emitDelete(key: PriKey | ComKey, item?: Item): Promise; /** * Emit an action event with automatic scope inclusion. */ emitAction(key: PriKey | ComKey, actionName: string, actionData?: Record): Promise; } /** * EventSubscriber interface for subscribing to and receiving events. * Each item type gets its own EventSubscriber instance for full type safety. */ export interface EventSubscriber { /** * Subscribe to events using a full subscription object. * Returns the subscription ID for later unsubscribing. */ subscribe(subscription: Omit, 'id'>): Promise; /** * Unsubscribe from events using the subscription ID. */ unsubscribe(subscriptionId: string): Promise; /** * Register a callback to be called when events are received. * Multiple callbacks can be registered and they'll all be called. */ onEvent(callback: (event: BaseEvent) => void): void; /** * Remove a previously registered event callback. */ removeEventListener(callback: (event: BaseEvent) => void): void; /** * Convenience method to subscribe to a specific item. * Automatically creates an ItemSubscription with the provided options. */ subscribeToItem(key: PriKey | ComKey, options?: SubscriptionOptions): Promise; /** * Convenience method to subscribe to a location. * Automatically creates a LocationSubscription with the provided options. */ subscribeToLocation(kta: ItemTypeArray, location: LocKeyArray, options?: SubscriptionOptions): Promise; /** * Get all currently active subscriptions. * Useful for debugging and subscription management. */ getActiveSubscriptions(): Subscription[]; /** * Check if an event matches any active subscriptions. * Used internally by libraries to determine if an event should be processed. */ matchesSubscription(event: BaseEvent): boolean; /** * Check if an event matches a specific subscription. * Used internally for subscription matching logic. */ matchesSpecificSubscription(event: BaseEvent, subscription: Subscription): boolean; } /** * Combined EventSystem interface that includes both emitter and subscriber. * Libraries can implement this interface to provide both event emission and subscription. */ export interface EventSystem { /** Event emitter for publishing events */ readonly emitter: EventEmitter; /** Event subscriber for receiving events */ readonly subscriber: EventSubscriber; } /** * Factory function type for creating EventSystems. * Libraries implement this to create properly configured event systems. */ export type EventSystemFactory = (scopes: string[]) => EventSystem; export type UserEventEmitter = EventEmitter<'User'>; export type UserEventSubscriber = EventSubscriber<'User'>; export type UserEventSystem = EventSystem<'User'>; export type MessageEventEmitter = EventEmitter<'Message', L1, L2>; export type MessageEventSubscriber = EventSubscriber<'Message', L1, L2>; export type MessageEventSystem = EventSystem<'Message', L1, L2>;