UNPKG

@deckyfx/dioxus-react-bridge

Version:

React hooks and components for Dioxus IPC communication - lightweight bridge between React and Rust

150 lines 4.1 kB
/** * EventBridge - Lightweight event emitter for IPC communication * * Replaces RxJS with a minimal implementation that provides Observable-compatible API. * This saves ~50-80KB from the bundle size while maintaining the same functionality. * * @example * ```typescript * const bridge = new EventBridge(); * * // Subscribe to events * const subscription = bridge.on('my-channel').subscribe({ * next: (data) => console.log('Received:', data), * error: (err) => console.error('Error:', err), * complete: () => console.log('Complete') * }); * * // Emit events * bridge.emit('my-channel', { message: 'Hello!' }); * * // Unsubscribe * subscription.unsubscribe(); * ``` */ /** * Event handler function type */ type EventHandler = (data: any) => void; /** * Error handler function type */ type ErrorHandler = (error: any) => void; /** * Complete handler function type */ type CompleteHandler = () => void; /** * Observer interface compatible with RxJS Observable.subscribe() */ interface Observer { /** Called when new data is emitted */ next: EventHandler; /** Called when an error occurs (optional) */ error?: ErrorHandler; /** Called when the stream completes (optional) */ complete?: CompleteHandler; } /** * Subscription object returned by subscribe() * Compatible with RxJS Subscription interface */ interface Subscription { /** Unsubscribe from the event channel */ unsubscribe: () => void; } /** * Observable-like interface for event channels * Provides RxJS-compatible subscribe() method */ export interface Observable { /** Subscribe to events on this channel */ subscribe(observer: Observer): Subscription; } /** * EventBridge - Minimal pub/sub event system * * Provides a lightweight alternative to RxJS for IPC event handling. * Maintains API compatibility with RxJS Observable for easy migration. */ export declare class EventBridge { /** * Map of channel names to sets of event handlers * Each channel can have multiple subscribers */ private listeners; /** * Map of channel names to error handlers * Errors are broadcast to all error handlers on a channel */ private errorHandlers; /** * Subscribe to events on a specific channel * * Returns an Observable-compatible object with subscribe() method. * * @param channel - Event channel name * @returns Observable with subscribe method * * @example * ```typescript * bridge.on('user-login').subscribe({ * next: (user) => console.log('User logged in:', user), * error: (err) => console.error('Login error:', err) * }); * ``` */ on(channel: string): Observable; /** * Emit data to all subscribers on a channel * * @param channel - Event channel name * @param data - Data to send to subscribers * * @example * ```typescript * bridge.emit('notification', { * type: 'info', * message: 'Task completed!' * }); * ``` */ emit(channel: string, data: any): void; /** * Emit an error to all error handlers on a channel * * @param channel - Event channel name * @param error - Error to send to error handlers * * @example * ```typescript * bridge.emitError('api-call', new Error('Network timeout')); * ``` */ emitError(channel: string, error: any): void; /** * Get all active channels * * @returns Array of channel names with active subscribers */ getActiveChannels(): string[]; /** * Get subscriber count for a channel * * @param channel - Event channel name * @returns Number of active subscribers */ getSubscriberCount(channel: string): number; /** * Clear all subscribers from a channel * * @param channel - Event channel name */ clearChannel(channel: string): void; /** * Clear all subscribers from all channels */ clearAll(): void; } export {}; //# sourceMappingURL=EventBridge.d.ts.map