import { authSessionId } from "../../../services/authStorage"; import type { AthenaRecipientTuple, ReleaseWrite } from "../../../services/RecordsReleaseService.tsx"; import { viewerEpoch } from "../../../utils/writeThroughHold"; /** A confirmation as first sent: retried exactly, never rebuilt from the inputs shown now. */ export interface HeldConfirmation { tuple: AthenaRecipientTuple; write: ReleaseWrite; held?: boolean; said?: string; } // Kept outside any one opening for this viewer's session, so closing the chooser never answers it. A new viewer or // session, including the same account signing in again, gets a new key and no custody of another's confirmation. const holds = new Map(); const viewerPart = () => `${viewerEpoch()}|${authSessionId() ?? ""}|`; const keyOf = (organizationId: string, requestId: string) => `${viewerPart()}${organizationId}|${requestId}`; const forgetOtherViewers = () => { const mine = viewerPart(); for (const key of holds.keys()) if (!key.startsWith(mine)) holds.delete(key); }; /** The confirmation this viewer left unanswered on this request at this practice, if any. */ export const heldConfirmation = (organizationId: string, requestId: string): HeldConfirmation | undefined => { forgetOtherViewers(); return holds.get(keyOf(organizationId, requestId)); }; /** Recorded before the confirmation is sent, and kept until its own operation or a later version settles it. */ export const holdConfirmation = (organizationId: string, requestId: string, held: HeldConfirmation): void => { forgetOtherViewers(); holds.set(keyOf(organizationId, requestId), held); }; /** Released only for the very operation that was held. */ export const releaseConfirmation = (organizationId: string, requestId: string, operationId: string): void => { const key = keyOf(organizationId, requestId); if (holds.get(key)?.write.operationId === operationId) holds.delete(key); };