UNPKG

@synet/realtime

Version:

Realtime Communication server/client implementations

70 lines (69 loc) 2.25 kB
import type { RealtimeChannel, ChannelOptions, EventSelector, RealtimeEvent } from '@synet/patterns/realtime'; import { ChannelState } from '@synet/patterns/realtime'; /** * GUN implementation of RealtimeChannel */ export declare class GunRealtimeChannel<TIn extends RealtimeEvent = RealtimeEvent, TOut extends RealtimeEvent = RealtimeEvent> implements RealtimeChannel<TIn, TOut> { private gunNode; readonly topic: string; private options; readonly id: `${string}-${string}-${string}-${string}-${string}`; private state; private eventHandlers; private unsubscribers; private recentlyProcessedEvents; /** * Create a new GUN channel * @param gunNode The GUN node to use for this channel * @param topic The topic this channel is subscribed to * @param options Channel configuration options */ constructor(gunNode: any, topic: string, options?: ChannelOptions); /** * Get the current state of the channel */ getState(): ChannelState; /** * Connect to the channel */ connect(): Promise<void>; /** * Subscribe to events on this channel * @param selector Event selector (type or pattern) * @param handler Function to call when event is received */ on<T extends TIn = TIn>(selector: EventSelector, handler: (event: T) => void): () => void; /** * Set up a GUN subscription for events matching the selector */ private setupGunSubscription; /** * Convert GUN data to a RealtimeEvent, resolving any references */ private gunDataToEvent; /** * Resolve a GUN reference to get the actual data */ private resolveGunReference; /** * Notify handlers with an event that has resolved data */ private notifyHandlersWithResolvedData; /** * Check if an event matches a selector */ private eventMatchesSelector; /** * Send an event through this channel * @param event The event to send */ emit(event: TOut): Promise<void>; /** * Recursively serialize Date objects to ISO strings to avoid GUN serialization issues */ private serializeDates; /** * Close the channel */ close(): Promise<void>; }