@powertoys/relay
Version:
Tab 2 tab communication on web applications
151 lines (143 loc) • 4.69 kB
TypeScript
import * as react_jsx_runtime from 'react/jsx-runtime';
import React from 'react';
import { TabInfo as TabInfo$1, TabListItem as TabListItem$1 } from 'src/types';
interface Subscriber<T> {
next: (value: T) => void;
error?: (error: any) => void;
complete?: () => void;
}
declare class Observable<T> {
private subscribers;
subscribe(subscriber: Subscriber<T>): () => void;
next(value: T): void;
error(err: any): void;
complete(): void;
}
interface TabInfo {
tabId: string;
tabName: string;
}
interface TabListItem extends TabInfo {
lastSeen: number;
}
interface ConnectedEvent {
type: "connected";
tabInfo: TabInfo;
}
interface DisconnectedEvent {
type: "disconnected";
}
interface TabListUpdatedEvent {
type: "tabListUpdated";
tabs: TabListItem[];
}
interface TabRegisteredEvent {
type: "tabRegistered";
tabId: string;
tabName: string;
}
interface ActionResultEvent {
type: "actionResult";
requestId: string;
result: any;
}
type TabCommunicatorEvent = ConnectedEvent | DisconnectedEvent | TabListUpdatedEvent | TabRegisteredEvent | ActionResultEvent;
type ActionHandler<TPayload = any, TResult = any> = (payload: TPayload, requestId: string, requestorId: string) => Promise<TResult>;
type ActionHandlerMap = Record<string, ActionHandler>;
interface RelayOptions {
handlers?: ActionHandlerMap;
getTabInfo?: () => Promise<TabInfo>;
heartbeatInterval?: number;
workerUrl?: string;
}
declare class Relay {
private port;
private tabInfo;
private heartbeatInterval;
private requestIdCounter;
private pendingRequests;
private handlers;
private isInitialized;
private options;
private status;
private channel;
private workerUrl?;
private events$;
private tabList$;
constructor(options: RelayOptions);
private handleChannelMessage;
/**
* Initialize the tab communicator
* @returns A promise that resolves when initialization is complete
*/
initialize(): Promise<void>;
/**
* Send a heartbeat to keep the connection alive
*/
private sendHeartbeat;
/**
* Get the list of open tabs
*/
getTabList(): void;
/**
* Request an action to be performed by another tab
* @param targetTabId The ID of the target tab
* @param action The action to perform
* @param payload The payload for the action
* @param onSuccess Optional callback for success
* @param onError Optional callback for error
* @returns The request ID or null if failed
*/
requestAction<TResult = any>(targetTabId: string, action: string, payload?: any, onSuccess?: (result: TResult) => void, onError?: (error: string) => void): string | null;
/**
* Handle an action request from another tab
* @param message The action request message
*/
private handleActionRequest;
/**
* Handle messages from the shared worker
* @param event The message event
*/
private handleWorkerMessage;
/**
* Send a message to the shared worker
* @param message The message to send
*/
private sendMessage;
/**
* Clean up the tab communicator
*/
cleanup(): void;
/**
* Subscribe to all events
* @param subscriber The subscriber function or object
* @returns An unsubscribe function
*/
onEvent(subscriber: Subscriber<TabCommunicatorEvent>): () => void;
/**
* Subscribe to tab list updates
* @param subscriber The subscriber function or object
* @returns An unsubscribe function
*/
onTabListUpdated(subscriber: Subscriber<TabListItem[]>): () => void;
/**
* Get the tab info for this tab
* @returns The tab info
*/
getTabInfo(): TabInfo | null;
/**
* Check if the tab communicator is initialized
* @returns True if initialized
*/
isConnected(): boolean;
}
interface RelayProviderProps {
relay: Relay;
children: React.ReactNode;
}
declare function RelayProvider({ relay, children }: RelayProviderProps): react_jsx_runtime.JSX.Element;
declare function useRelay(): Relay;
declare function useTabInfo(): TabInfo$1 | null;
declare function useTabList(): TabListItem$1[];
declare function useRequestAction(): <TResult = any>(targetTabId: string, action: string, payload?: any, onSuccess?: (result: TResult) => void, onError?: (error: string) => void) => Promise<TResult>;
export { type ActionHandler, type ActionHandlerMap, Observable, Relay, type RelayOptions, RelayProvider, type Subscriber, type TabCommunicatorEvent, type TabInfo, type TabListItem, useRelay, useRequestAction, useTabInfo, useTabList };