UNPKG

porter-source

Version:

Messaging Library for Web Extensions

43 lines (42 loc) 1.51 kB
import { Runtime } from 'webextension-polyfill'; import { AgentInfo } from '../porter.model'; import { Logger } from '../porter.utils'; export type DisconnectCallback = () => void; export type ReconnectCallback = (info: AgentInfo) => void; export declare class AgentConnectionManager { private readonly namespace; private readonly CONNECTION_TIMEOUT; private readonly RECONNECT_INTERVAL; private connectionTimer; private reconnectTimer; private agentInfo; private port; private readonly logger; private readonly connectionId; private readonly messageQueue; private isReconnecting; private reconnectAttemptCount; private disconnectCallbacks; private reconnectCallbacks; constructor(namespace: string, logger: Logger); /** * Register a callback to be called when the connection is lost * @returns Unsubscribe function */ onDisconnect(callback: DisconnectCallback): () => void; /** * Register a callback to be called when reconnection succeeds * @returns Unsubscribe function */ onReconnect(callback: ReconnectCallback): () => void; private emitDisconnect; private emitReconnect; initializeConnection(): Promise<void>; private processQueuedMessages; getPort(): Runtime.Port | null; getAgentInfo(): AgentInfo | null; getNamespace(): string; handleDisconnect(port: Runtime.Port): void; private startReconnectionAttempts; queueMessage(message: any, target?: any): void; }