UNPKG

@broadcaster/core

Version:

Cross window serverless messaging system based on BroadcastChannel API. Allows to send messages and keep track about instances between browsing contexts (tabs, windows, workers, etc..)

82 lines (81 loc) 2.29 kB
import { BroadcasterBridge } from "./Bridge"; /** * **BroadcastChannel Bridge: Serverless browsing context bridge** * * Communicates with other Broadcaster instances via * [BroadcastChannel API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API). * * BroadcastChannel API allows to communicate with different browsing contexts inside a browser * (tabs, window, workers, etc..) without need of a server. * * ____ * * Minimal requirements: * * | Chromium | Edge | Safari | Firefox | * |:--------:|:----:|:------:|:-------:| * | 54 | 79 | 15.4 | 38 | */ export declare class BroadcastChannelBridge<Payload, Metadata> extends BroadcasterBridge<Payload, Metadata> { /** * Instance of BroadcastChannel API. */ private messageChannel; /** * Channel used as identifier for communication */ private channelName; /** * Connects to a BroadcasterChannel stream * * @param channelName communication identifier */ connect(channelName: string): void; protected disconnect(): void; /** * Sort incoming messages and push it to corresponding stream * * @param event BroadcasterChannel Event */ private extractMessageAndPush; /** * Wraps action to try catch, and in case of an error, * it will transform it to BroadcastError and push it to subscribers * * @param action callback action, which will be called inside try scope */ private guardPostMessageErrors; /** * Incoming message is a valid BroadcastChannelBridge message * * @param data * @returns */ private isMessage; /** * A BroadcasterChannel message is a public message * * @param data * @returns */ private isPublicMessage; /** * A BroadcasterChannel message is a state change message * * @param data * @returns */ private isStateChangeMessage; /** * Sends public message to all BroadcasterChannelBridge instances * * @param payload */ postMessage(payload: Payload): void; /** * Sends new state of this instance to all BroadcasterChannelBridge instances * * @param payload */ setState(payload: Metadata): void; }