import type { SessionState } from "./state" /** * Ensure only one command runs at a time on the SSH session. * Additional calls are queued and executed in order. */ export async function withMutex(state: SessionState, fn: () => Promise): Promise { return new Promise((resolve, reject) => { const run = async () => { state.running = true try { resolve(await fn()) } catch (e) { reject(e) } finally { state.running = false const next = state.commandQueue.shift() if (next) next() } } if (state.running) { state.commandQueue.push(run) } else { run() } }) }