| 1 | import type { SessionOperationAuthority, SessionResource } from "./useResourceOperations"; |
| 2 | |
| 3 | export async function executeCancelRuntimeJob( |
| 4 | target: SessionResource, |
| 5 | jobId: string, |
| 6 | ports: { cancelForTab: (tabId: string, jobId: string) => Promise<boolean>; refresh: () => Promise<void> }, |
| 7 | authority: SessionOperationAuthority, |
| 8 | ): Promise<boolean> { |
| 9 | authority.checkpoint(); |
| 10 | const cancelled = await ports.cancelForTab(target.tabId, jobId); |
| 11 | authority.checkpoint(); |
| 12 | if (authority.ownsUI()) await ports.refresh(); |
| 13 | authority.checkpoint(); |
| 14 | return cancelled; |
| 15 | } |
| 16 | |
| 17 | export async function executeTerminalOutputInsertion( |
| 18 | target: SessionResource, |
| 19 | sessionId: string, |
| 20 | ports: { read: (tabId: string, sessionId: string) => Promise<string>; apply: (text: string) => void }, |
| 21 | format: (output: string) => string, |
| 22 | authority: SessionOperationAuthority, |
| 23 | ): Promise<boolean> { |
| 24 | authority.checkpoint(); |
| 25 | const text = format(await ports.read(target.tabId, sessionId)); |
| 26 | authority.checkpoint(); |
| 27 | if (!text) return false; |
| 28 | if (authority.ownsUI()) ports.apply(text); |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | export type ClearSessionPorts = { |
| 33 | clearSession: () => Promise<void>; |
| 34 | clearRemoteSession: (tabId: string) => Promise<void>; |
| 35 | retryRemoteHydration: () => Promise<void>; |
| 36 | }; |
| 37 | |
| 38 | export async function executeClearSession( |
| 39 | target: SessionResource, |
| 40 | input: { remote: boolean }, |
| 41 | ports: ClearSessionPorts, |
| 42 | authority: SessionOperationAuthority, |
| 43 | ): Promise<void> { |
| 44 | authority.checkpoint(); |
| 45 | if (input.remote) { |
| 46 | await ports.clearRemoteSession(target.tabId); |
| 47 | authority.checkpoint(); |
| 48 | await ports.retryRemoteHydration(); |
| 49 | } else { |
| 50 | await ports.clearSession(); |
| 51 | } |
| 52 | authority.checkpoint(); |
| 53 | } |
| 54 |