import { LeaderEvent } from '../types'; export declare type LeaderSettings = Readonly<{ electionTimeoutMs: number; heartbeatIntervalMs: number; heartbeatTimeoutMs: number; }>; export declare const DEFAULT_LEADER_SETTINGS: LeaderSettings; /** * This class takes a best-effort approach to picking a single leader amongst * multiple clients assuming a shared broadcast channel. * * Whenever a client comes online, it requests an election. * * 1. If there is already a leader, it will announce itself, cancelling the election. * 2. Otherwise, everyone announces themselves as available. * * After an election timeout, the client with the alphanumerically smallest * clientId announces itself as the leader to everyone. * * If a leader receives an announcement from another leader, a re-election will * occur, and all but one of them will be demoted back to non-leader. */ export declare class LeaderManager { /** * A unique identifier for each client. This can simply be a uuid */ private readonly clientId; /** * A callback for when this client gains or loses leadership */ private readonly onLeaderChange; /** * A callback for when this class needs to broadcast messages to other clients */ private readonly broadcastEvent; private readonly settings; private closed; private currentLeaderId?; private isLeader; private potentialLeaders; private electionTimeout?; private leaderHeartbeat?; private heartbeatTimeout?; constructor( /** * A unique identifier for each client. This can simply be a uuid */ clientId: string, /** * A callback for when this client gains or loses leadership */ onLeaderChange: (isLeader: boolean) => void, /** * A callback for when this class needs to broadcast messages to other clients */ broadcastEvent: (event: LeaderEvent) => void, settings?: LeaderSettings); private elect; private cancelElection; private finishElection; private setLeader; private onLeaderHeartbeat; private onHeartbeatTimeout; receiveEvent({ action, clientId: otherClientId }: LeaderEvent): void; shutdown(cleanShutdown?: boolean): void; }