tab-whisper
Version:
A lightweight, browser-only framework for inter-tab/window communication using the Broadcast Channel API
130 lines (129 loc) • 3.16 kB
TypeScript
import { TabCommunicatorOptions, EventCallback, EventType } from './types.js';
/**
* TabCommunicator - Inter-tab/window communication using BroadcastChannel API
*/
export declare class TabCommunicator {
private readonly _id;
private readonly _registrationId;
private readonly _channelName;
private readonly _channel;
private readonly _peers;
private readonly _eventListeners;
private _isConnected;
private _discoveryTimeout;
private _peerVerificationInterval;
private _heartbeatInterval;
private readonly _onMessage?;
private readonly _onPeerConnected?;
private readonly _onPeerDisconnected?;
private readonly _onError?;
constructor(options: TabCommunicatorOptions);
/**
* Internal ID of this instance (automatically generated)
*/
get id(): string;
/**
* Registration ID of this instance (user-provided)
*/
get registrationId(): string | null;
/**
* Set of active peer IDs (excluding self)
*/
get peers(): Set<string>;
/**
* Name of the communication channel
*/
get channelName(): string;
/**
* Connection status
*/
get isConnected(): boolean;
/**
* Send a message to a specific peer or broadcast to all
*/
send(targetId: string | null, type: string, payload: any): void;
/**
* Register an event listener
*/
on(eventType: EventType, callback: EventCallback): void;
/**
* Remove an event listener
*/
off(eventType: EventType, callback: EventCallback): void;
/**
* Close the communicator and clean up resources
*/
close(): void;
/**
* Initialize the communicator
*/
private _initialize;
/**
* Generate a unique internal ID
*/
private _generateInternalId;
/**
* Handle incoming messages
*/
private _handleMessage;
/**
* Handle internal framework messages
*/
private _handleInternalMessage;
/**
* Handle peer registration
*/
private _handlePeerRegister;
/**
* Handle peer disconnection
*/
private _handlePeerDisconnect;
/**
* Handle peer discovery request
*/
private _handlePeerDiscover;
/**
* Handle peer heartbeat request
*/
private _handlePeerHeartbeat;
/**
* Handle peer heartbeat response
*/
private _handlePeerHeartbeatResponse;
/**
* Set up tab close detection
*/
private _setupTabCloseDetection;
/**
* Start heartbeat system
*/
private _startHeartbeat;
/**
* Send heartbeat to all peers
*/
private _sendHeartbeat;
/**
* Send an internal framework message
*/
private _sendInternalMessage;
/**
* Find a peer by any ID (internal or registration)
*/
private _findPeerByAnyId;
/**
* Validate message parameters
*/
private _validateMessage;
/**
* Ensure the communicator is connected
*/
private _ensureConnected;
/**
* Emit an event to listeners
*/
private _emitEvent;
/**
* Handle errors
*/
private _handleError;
}