UNPKG

@nori-zk/mina-token-bridge

Version:

Nori ethereum state settelment and nETH token bridge zkApp

68 lines (67 loc) 2.88 kB
import { type WebSocketSubjectConfig } from 'rxjs/webSocket'; import { Subject, BehaviorSubject, Observable, Subscription } from 'rxjs'; /** * Enum-like type representing the internal state of the WebSocket connection. */ type WebSocketwebSocketConnectionState = 'connecting' | 'open' | 'closed' | 'reconnecting' | 'permanently-closed'; /** * Extension of WebSocketSubjectConfig that includes optional reconnection parameters. */ interface ReconnectingWebSocketConfig<T> extends WebSocketSubjectConfig<T> { reconnect?: { initialDelayMs?: number; maxDelayMs?: number; maxRetries?: number; }; } /** * A Subject wrapper over `WebSocketSubject` that adds automatic reconnection behavior. * * Reconnection attempts use exponential backoff with an upper delay bound. * The class exposes: * - an observable stream for connection state changes, * - an outgoing message buffer to prevent message loss during reconnects, * - and a subscription proxy for incoming messages. * * Once the maximum number of retries is exceeded (if provided), the connection transitions * to `permanently-closed`, completing all observables and releasing resources. * * @template T Type of message payload sent/received over the socket. */ export declare class ReconnectingWebSocketSubject<T> extends Subject<T> { webSocketConnectionState$: Observable<WebSocketwebSocketConnectionState>; private webSocketConnectionStateSubject; private outgoingBuffer; private incomingSubject; private socketSub; private socket; private reconnectAttempt; private reconnectTimerSub?; private isReconnecting; private config; constructor(config: ReconnectingWebSocketConfig<T>, webSocketConnectionStateSubject: BehaviorSubject<WebSocketwebSocketConnectionState>); forceReconnect(): void; private _connect; private _reconnect; next(value: T): void; error(err: unknown): void; complete(): void; subscribe(...args: unknown[]): Subscription; multiplex<R>(subMsg: () => T, unsubMsg: () => T, messageFilter: (value: T) => boolean): Observable<R>; } /** * Factory for creating a reconnecting WebSocket and its associated connection state stream. * * The returned socket behaves like a regular `WebSocketSubject` but includes * automatic reconnection with exponential backoff and a persistent state observable. * * @param config Configuration for WebSocketSubject and reconnection strategy. * @returns Object containing: * - `webSocket$`: the ReconnectingWebSocketSubject instance. * - `webSocketConnectionState$`: observable emitting connection state changes. */ export declare function reconnectingWebSocket<T>(config: ReconnectingWebSocketConfig<T>): { webSocket$: ReconnectingWebSocketSubject<T>; webSocketConnectionState$: Observable<WebSocketwebSocketConnectionState>; }; export {};