| 1 | import { app } from "./bridge"; |
| 2 | import type { SessionHistoryReadHandle } from "../generated/desktopContract.generated"; |
| 3 | |
| 4 | export type Binding = { promise: Promise<SessionHistoryReadHandle> }; |
| 5 | const bindings = new Map<string, Binding>(); |
| 6 | |
| 7 | export function supportsHistoryRead(): boolean { |
| 8 | return typeof app.BeginSessionHistoryReadForTab === "function" |
| 9 | && typeof app.ReleaseSessionHistoryRead === "function" |
| 10 | && typeof app.ReadSessionHistoryWindow === "function"; |
| 11 | } |
| 12 | |
| 13 | export function acquireHistoryRead(tabId: string): Binding { |
| 14 | const previous = bindings.get(tabId); |
| 15 | if (previous) return previous; |
| 16 | const binding: Binding = { promise: Promise.resolve().then(() => app.BeginSessionHistoryReadForTab(tabId)) }; |
| 17 | bindings.set(tabId, binding); |
| 18 | void binding.promise.catch(() => { if (bindings.get(tabId) === binding) bindings.delete(tabId); }); |
| 19 | return binding; |
| 20 | } |
| 21 | |
| 22 | /** Retire the exact request scope, including a Begin reply arriving after navigation. */ |
| 23 | export function releaseHistoryRead(tabId: string): void { |
| 24 | const binding = bindings.get(tabId); |
| 25 | if (!binding) return; |
| 26 | bindings.delete(tabId); |
| 27 | void binding.promise.then(handle => app.ReleaseSessionHistoryRead(handle.id)).catch(() => {}); |
| 28 | } |
| 29 | |
| 30 | export const currentHistoryReadBinding = (tabId: string): Binding | undefined => bindings.get(tabId); |
| 31 |