{"version":3,"file":"sessionChannelHoldsStore.mjs","names":[],"sources":["../../../src/web/stores/sessionChannelHoldsStore.ts"],"sourcesContent":["import { Store } from '@tanstack/store';\n\nimport { traceStore } from '../lib/uiTrace';\n\n/** Dialogs can observe their target without taking focus or stealing another observer's subscription. */\nexport const heldSessionChannels = traceStore('sessionChannelHolds', new Store<ReadonlyMap<string, number>>(new Map()));\n\nconst observers = new Set<{ sessionId: string; receive: (frame: Record<string, unknown>) => void }>();\n\nexport function holdSessionChannels(sessionId: string, receive?: (frame: Record<string, unknown>) => void): () => void {\n  const observer = receive === undefined ? undefined : { sessionId, receive };\n  if (observer) observers.add(observer);\n  heldSessionChannels.setState((current) => new Map(current).set(sessionId, (current.get(sessionId) ?? 0) + 1));\n  let released = false;\n  return () => {\n    if (released) return;\n    released = true;\n    if (observer) observers.delete(observer);\n    heldSessionChannels.setState((current) => {\n      const next = new Map(current);\n      const remaining = (next.get(sessionId) ?? 1) - 1;\n      if (remaining === 0) next.delete(sessionId);\n      else next.set(sessionId, remaining);\n      return next;\n    });\n  };\n}\n\n/** Deliver to the observing dialog's composition, even when the target has never been focused. */\nexport function dispatchHeldSessionChannel(frame: Record<string, unknown>): void {\n  if (typeof frame.type !== 'string' || typeof frame.sessionId !== 'string') return;\n  for (const observer of observers) {\n    if (observer.sessionId !== frame.sessionId) continue;\n    try {\n      observer.receive(frame);\n    } catch (error) {\n      console.error('Session channel observer failed', error);\n    }\n  }\n}\n\n/** Voice alone retains an already mounted composition and permits host background connectivity. */\nexport const heldVoiceSessions = traceStore('voiceSessionHolds', new Store<ReadonlyMap<string, number>>(new Map()));\n\nexport function holdVoiceSession(sessionId: string): () => void {\n  const releaseChannels = holdSessionChannels(sessionId);\n  heldVoiceSessions.setState((current) => new Map(current).set(sessionId, (current.get(sessionId) ?? 0) + 1));\n  let released = false;\n  return () => {\n    if (released) return;\n    released = true;\n    heldVoiceSessions.setState((current) => {\n      const next = new Map(current);\n      const count = (next.get(sessionId) ?? 1) - 1;\n      if (count === 0) next.delete(sessionId);\n      else next.set(sessionId, count);\n      return next;\n    });\n    releaseChannels();\n  };\n}\n"],"mappings":";;;;AAKA,MAAa,sBAAsB,WAAW,uBAAuB,IAAI,sBAAmC,IAAI,IAAI,CAAC,CAAC;AAEtH,MAAM,4BAAY,IAAI,IAA8E;AAEpG,SAAgB,oBAAoB,WAAmB,SAAgE;CACrH,MAAM,WAAW,YAAY,KAAA,IAAY,KAAA,IAAY;EAAE;EAAW;CAAQ;CAC1E,IAAI,UAAU,UAAU,IAAI,QAAQ;CACpC,oBAAoB,UAAU,YAAY,IAAI,IAAI,OAAO,CAAC,CAAC,IAAI,YAAY,QAAQ,IAAI,SAAS,KAAK,KAAK,CAAC,CAAC;CAC5G,IAAI,WAAW;CACf,aAAa;EACX,IAAI,UAAU;EACd,WAAW;EACX,IAAI,UAAU,UAAU,OAAO,QAAQ;EACvC,oBAAoB,UAAU,YAAY;GACxC,MAAM,OAAO,IAAI,IAAI,OAAO;GAC5B,MAAM,aAAa,KAAK,IAAI,SAAS,KAAK,KAAK;GAC/C,IAAI,cAAc,GAAG,KAAK,OAAO,SAAS;QACrC,KAAK,IAAI,WAAW,SAAS;GAClC,OAAO;EACT,CAAC;CACH;AACF;;AAGA,SAAgB,2BAA2B,OAAsC;CAC/E,IAAI,OAAO,MAAM,SAAS,YAAY,OAAO,MAAM,cAAc,UAAU;CAC3E,KAAK,MAAM,YAAY,WAAW;EAChC,IAAI,SAAS,cAAc,MAAM,WAAW;EAC5C,IAAI;GACF,SAAS,QAAQ,KAAK;EACxB,SAAS,OAAO;GACd,QAAQ,MAAM,mCAAmC,KAAK;EACxD;CACF;AACF;;AAGA,MAAa,oBAAoB,WAAW,qBAAqB,IAAI,sBAAmC,IAAI,IAAI,CAAC,CAAC;AAElH,SAAgB,iBAAiB,WAA+B;CAC9D,MAAM,kBAAkB,oBAAoB,SAAS;CACrD,kBAAkB,UAAU,YAAY,IAAI,IAAI,OAAO,CAAC,CAAC,IAAI,YAAY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