UNPKG

react-simple-channel

Version:

Lightweight and reactive tab-to-tab communication tool for React & non-React contexts. 一个轻量的 React 多标签页通信工具,支持传统函数和 Hook 两种方式。

40 lines (39 loc) 1.04 kB
export type BroadcastCallback<T> = (data: T, fromRemote: boolean) => void; interface BroadcastSyncOptions { debounceMs?: number; throttleMs?: number; } /** * 广播同步工具(单例模式) * 用于跨标签页同步状态,支持监听远程/本地更新 */ export declare class BroadcastSync<T> { private channelName; private options?; private static instances; private channel; private callbacks; private postMessageFn; private constructor(); /** * 获取指定频道的单例 */ static query<T>(channelName: string, options?: BroadcastSyncOptions): BroadcastSync<T>; /** * 添加监听器 */ addEventListener(callback: BroadcastCallback<T>): this; /** * 移除监听器 */ removeEventListener(callback: BroadcastCallback<T>): this; /** * 发送数据,并触发本地回调(fromRemote: false) */ post(data: T): this; /** * 关闭通道(手动销毁该频道) */ close(): this; } export {};