{"version":3,"file":"AudioPlaybackArbiter.mjs","names":[],"sources":["../../../../src/components/AudioPlayback/AudioPlaybackArbiter.ts"],"sourcesContent":["import { StateStore } from '@stream-io/state-store';\nimport type { AudioPlayer } from './AudioPlayer';\n\nexport type AudioPlaybackArbiterState = {\n  activeAudioPlayer: AudioPlayer | null;\n};\n\n/**\n * Decides which player may play across every {@link AudioPlayerPool} that shares it, and owns the\n * single `Audio` element they take turns on: starting a player pauses the previous holder.\n * Exclusivity lives here, not in a pool, because each surface (`Channel`, `Thread`) mounts its own\n * pool. One reused element also keeps playback working on iOS, where an element must be unlocked\n * by a user gesture before it can be played programmatically.\n */\nexport class AudioPlaybackArbiter {\n  state = new StateStore<AudioPlaybackArbiterState>({ activeAudioPlayer: null });\n\n  private element: HTMLAudioElement | null = null;\n  private owner: AudioPlayer | null = null;\n\n  get activeAudioPlayer() {\n    return this.state.getLatestValue().activeAudioPlayer;\n  }\n\n  /**\n   * Hands the shared element to `owner`, pausing the previous holder, and loads `src` into it.\n   * @param params.owner - Player that takes the element.\n   * @param params.src - Audio source to load. Only assigned when it differs from the current one.\n   * @returns The shared element.\n   */\n  acquire = ({ owner, src }: { owner: AudioPlayer; src: string }) => {\n    if (!this.element) {\n      this.element = new Audio();\n    }\n\n    if (this.owner && this.owner !== owner) {\n      // Pause and drop the reference, but leave the player registered in its own pool.\n      this.owner.pause();\n      this.owner.releaseElementForHandoff();\n    }\n    this.owner = owner;\n\n    if (this.element.src !== src) {\n      // Setting `src` starts loading; an explicit load() would reset currentTime and flicker.\n      this.element.src = src;\n    }\n    return this.element;\n  };\n\n  /**\n   * Drops `owner`'s claim on the shared element and pauses it. Ignored when `owner` is not the\n   * current holder, so a player torn down in the background cannot stop its successor.\n   * @param owner - Player releasing the element.\n   */\n  release = (owner: AudioPlayer) => {\n    if (this.owner !== owner) return;\n    if (this.element) {\n      try {\n        this.element.pause();\n      } catch {\n        // ignore\n      }\n      this.element.removeAttribute('src');\n      this.element.load();\n    }\n    // Keep the element instance for reuse\n    this.owner = null;\n  };\n\n  setActivePlayer = (activeAudioPlayer: AudioPlayer | null) => {\n    this.state.partialNext({ activeAudioPlayer });\n  };\n}\n"],"mappings":";;;;;;;;;AAcA,IAAa,uBAAb,MAAkC;CAChC,QAAQ,IAAI,WAAsC,EAAE,mBAAmB,KAAK,CAAC;CAE7E,UAA2C;CAC3C,QAAoC;CAEpC,IAAI,oBAAoB;EACtB,OAAO,KAAK,MAAM,eAAe,CAAC,CAAC;CACrC;;;;;;;CAQA,WAAW,EAAE,OAAO,UAA+C;EACjE,IAAI,CAAC,KAAK,SACR,KAAK,UAAU,IAAI,MAAM;EAG3B,IAAI,KAAK,SAAS,KAAK,UAAU,OAAO;GAEtC,KAAK,MAAM,MAAM;GACjB,KAAK,MAAM,yBAAyB;EACtC;EACA,KAAK,QAAQ;EAEb,IAAI,KAAK,QAAQ,QAAQ,KAEvB,KAAK,QAAQ,MAAM;EAErB,OAAO,KAAK;CACd;;;;;;CAOA,WAAW,UAAuB;EAChC,IAAI,KAAK,UAAU,OAAO;EAC1B,IAAI,KAAK,SAAS;GAChB,IAAI;IACF,KAAK,QAAQ,MAAM;GACrB,QAAQ,CAER;GACA,KAAK,QAAQ,gBAAgB,KAAK;GAClC,KAAK,QAAQ,KAAK;EACpB;EAEA,KAAK,QAAQ;CACf;CAEA,mBAAmB,sBAA0C;EAC3D,KAAK,MAAM,YAAY,EAAE,kBAAkB,CAAC;CAC9C;AACF"}