Open History to see the last commit
1import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
2import type {
3 EnvironmentId,
4 PreviewCloseInput,
5 PreviewSessionSnapshot,
6 ScopedThreadRef,
7} from "@t3tools/contracts";
8
9import { beginPreviewSessionClose, cancelPreviewSessionClose } from "~/previewStateStore";
10
11interface ClosePreviewSessionInput<E> {
12 readonly closePreview: (input: {
13 readonly environmentId: EnvironmentId;
14 readonly input: PreviewCloseInput;
15 }) => Promise<AtomCommandResult<void, E>>;
16 readonly snapshot: PreviewSessionSnapshot | null;
17 readonly tabId: string;
18 readonly threadRef: ScopedThreadRef;
19}
20
21/**
22 * Optimistically closes a preview while suppressing stale list responses for
23 * the same tab. A failed close restores the last known snapshot.
24 */
25export async function closePreviewSession<E>(
26 input: ClosePreviewSessionInput<E>,
27): Promise<AtomCommandResult<void, E>> {
28 beginPreviewSessionClose(input.threadRef, input.tabId);
29 const result = await input.closePreview({
30 environmentId: input.threadRef.environmentId,
31 input: { threadId: input.threadRef.threadId, tabId: input.tabId },
32 });
33 if (result._tag === "Failure") {
34 cancelPreviewSessionClose(input.threadRef, input.snapshot, input.tabId);
35 }
36 return result;
37}