{"version":3,"file":"BroadcastChannelLeader.cjs","names":["BaseLeaderElection","safeRandomUUID"],"sources":["../../../src/coordination/BroadcastChannelLeader.ts"],"sourcesContent":["import { safeRandomUUID } from '@tanstack/db'\nimport { BaseLeaderElection } from './LeaderElection'\n\ninterface LeaderMessage {\n  type: `heartbeat` | `election` | `leadership-claim`\n  tabId: string\n  timestamp: number\n}\n\nexport class BroadcastChannelLeader extends BaseLeaderElection {\n  private channelName: string\n  private tabId: string\n  private channel: BroadcastChannel | null = null\n  private heartbeatInterval: number | null = null\n  private electionTimeout: number | null = null\n  private lastLeaderHeartbeat = 0\n  private readonly heartbeatIntervalMs = 5000\n  private readonly electionTimeoutMs = 10000\n\n  constructor(channelName = `offline-executor-leader`) {\n    super()\n    this.channelName = channelName\n    this.tabId = safeRandomUUID()\n    this.setupChannel()\n  }\n\n  private setupChannel(): void {\n    if (!this.isBroadcastChannelSupported()) {\n      return\n    }\n\n    this.channel = new BroadcastChannel(this.channelName)\n    this.channel.addEventListener(`message`, this.handleMessage)\n  }\n\n  private handleMessage = (event: MessageEvent<LeaderMessage>): void => {\n    const { type, tabId, timestamp } = event.data\n\n    if (tabId === this.tabId) {\n      return\n    }\n\n    switch (type) {\n      case `heartbeat`:\n        if (this.isLeaderState && tabId < this.tabId) {\n          this.releaseLeadership()\n        } else if (!this.isLeaderState) {\n          this.lastLeaderHeartbeat = timestamp\n          this.cancelElection()\n        }\n        break\n\n      case `election`:\n        if (this.isLeaderState) {\n          this.sendHeartbeat()\n        } else if (tabId > this.tabId) {\n          this.startElection()\n        }\n        break\n\n      case `leadership-claim`:\n        if (this.isLeaderState && tabId < this.tabId) {\n          this.releaseLeadership()\n        }\n        break\n    }\n  }\n\n  async requestLeadership(): Promise<boolean> {\n    if (!this.isBroadcastChannelSupported()) {\n      return false\n    }\n\n    if (this.isLeaderState) {\n      return true\n    }\n\n    this.startElection()\n\n    return new Promise((resolve) => {\n      setTimeout(() => {\n        resolve(this.isLeaderState)\n      }, 1000)\n    })\n  }\n\n  private startElection(): void {\n    if (this.electionTimeout) {\n      return\n    }\n\n    this.sendMessage({\n      type: `election`,\n      tabId: this.tabId,\n      timestamp: Date.now(),\n    })\n\n    this.electionTimeout = window.setTimeout(() => {\n      const timeSinceLastHeartbeat = Date.now() - this.lastLeaderHeartbeat\n\n      if (timeSinceLastHeartbeat > this.electionTimeoutMs) {\n        this.claimLeadership()\n      }\n\n      this.electionTimeout = null\n    }, this.electionTimeoutMs)\n  }\n\n  private cancelElection(): void {\n    if (this.electionTimeout) {\n      clearTimeout(this.electionTimeout)\n      this.electionTimeout = null\n    }\n  }\n\n  private claimLeadership(): void {\n    this.notifyLeadershipChange(true)\n    this.sendMessage({\n      type: `leadership-claim`,\n      tabId: this.tabId,\n      timestamp: Date.now(),\n    })\n    this.startHeartbeat()\n  }\n\n  private startHeartbeat(): void {\n    if (this.heartbeatInterval) {\n      return\n    }\n\n    this.sendHeartbeat()\n\n    this.heartbeatInterval = window.setInterval(() => {\n      this.sendHeartbeat()\n    }, this.heartbeatIntervalMs)\n  }\n\n  private stopHeartbeat(): void {\n    if (this.heartbeatInterval) {\n      clearInterval(this.heartbeatInterval)\n      this.heartbeatInterval = null\n    }\n  }\n\n  private sendHeartbeat(): void {\n    this.sendMessage({\n      type: `heartbeat`,\n      tabId: this.tabId,\n      timestamp: Date.now(),\n    })\n  }\n\n  private sendMessage(message: LeaderMessage): void {\n    if (this.channel) {\n      this.channel.postMessage(message)\n    }\n  }\n\n  releaseLeadership(): void {\n    this.stopHeartbeat()\n    this.cancelElection()\n    this.notifyLeadershipChange(false)\n  }\n\n  private isBroadcastChannelSupported(): boolean {\n    return typeof BroadcastChannel !== `undefined`\n  }\n\n  static isSupported(): boolean {\n    return typeof BroadcastChannel !== `undefined`\n  }\n\n  dispose(): void {\n    this.releaseLeadership()\n\n    if (this.channel) {\n      this.channel.removeEventListener(`message`, this.handleMessage)\n      this.channel.close()\n      this.channel = null\n    }\n  }\n}\n"],"mappings":";;;AASA,IAAa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