leader-tab
Version:
An algorithm for selecting a leader from all the open tabs of a website.
36 lines (34 loc) • 1.08 kB
TypeScript
type ElectorParams = {
/**
* The name of the channel to use for the `BroadcastChannel`.
*/
channelName?: string;
/**
* Interval for sending heartbeats
*/
heartbeatInterval?: number;
/**
* Timeout for considering a tab dead if we have received no hearbeat.
*/
deathTimeout?: number;
};
type ElectorCallbacks = {
/**
* Function to be run when the current tab is elected the leader.
*/
onLeaderElected: () => void;
/**
* Function to be run when the current tab is demoted from being the leader.
*/
onLeaderDemoted: () => void;
};
type Elector = ReturnType<typeof elector>;
/**
* Sets up elector selection mechanism.
*/
declare function elector({ onLeaderElected, onLeaderDemoted }?: ElectorCallbacks, { channelName, heartbeatInterval, deathTimeout, }?: ElectorParams): Readonly<{
isLeader: () => boolean;
getTabId: () => `${string}-${string}-${string}-${string}-${string}`;
destroy: () => void;
}>;
export { type Elector, type ElectorCallbacks, type ElectorParams, elector };