UNPKG

@sovanderpol/tabtalk

Version:

Tabtalk enables browsing contexts within the same domain to communicate with each other.

68 lines (67 loc) 2.64 kB
import { TabtalkMessage, TabtalkMessageMeta } from './tabtalkMessageFactory'; export declare type TabtalkMessageEventCallback = <T = any>(message: TabtalkMessage<T>) => void; export declare type TabtalkMessageEventHandler = (event: CustomEvent) => void; export declare class Tabtalk { private id; private TABTALK_MESSAGE_KEY_PREFIX; private TABTALK_MESSAGE_EVENT_NAME; private messageEventHandlersMap; private garbageCollectionDelay; constructor(); /** * Permanently deactivates the Tabtalk instance by removing event handlers. Note that the event * handlers of instances of Tabtalk within the same browsing context will be affected as well. */ destroy(): void; getId(): string; /** * Set the garbage collection delay. All messages that have existed for longer than this * delay will be considered due for garbage collection. * @param delay Time in milliseconds between creation and garbage collection. */ setGarbageCollectionDelay(delay: number): void; /** * Adds a Tabtalk message event handler with the passed callback. */ subscribe(callback: TabtalkMessageEventCallback): void; /** * Removes the Tabtalk message event handler that belongs to the passed callback. * If no callback is passed it will remove all the Tabtalk message event handlers. * * @param callbackToUnsubscribe (optional) The callback of the event listener to remove. */ unsubscribe(callbackToUnsubscribe?: TabtalkMessageEventCallback): void; private unsubscribeAllEventHandlers; private createTabtalkMessageEventHandler; /** * Handles storage events that are caused by Tabtalk and transmits the serialized message if necessary. */ private onStorageUpdate; /** * Deserializes a JSON string to a TabtalkMessage object. */ private deserializeMessage; /** * Sends a message to all Tabtalk instances. */ broadcast(body: any): void; /** * Sends a message to a specific Tabtalk instance. */ sendMessage(recipientId: TabtalkMessageMeta['recipientId'], body: any): void; /** * Removes all Tabtalk messages from local storage that are due for garbage collection. */ garbageCollect(): void; private getAllTabtalkMessageKeys; /** * Removes a Tabtalk message from the local storage. */ private clearMessage; private generateMessageKey; private isTabtalkMessage; private isMessageAddressedToMe; private isMessageAddressedToEveryone; private isMessageBeingDeleted; private isMessageDueForGarbageCollection; }