import { invoke } from "@tauri-apps/api/core"; const sessions = new Set(); const opening = new Set>(); const closing = new Map>(); let stopped = false; export async function closeTerminals(): Promise { stopped = true; await Promise.allSettled([...opening]); const results = await Promise.allSettled([...sessions].map(ptyClose)); const errors = results.filter(result => result.status === "rejected"); if (errors.length) throw new Error(errors.map(result => String(result.reason)).join("\n")); } export function ptyOpen(id: string, cols: number, rows: number, cwd: string, profile?: string, directory?: string): Promise { if (stopped) return Promise.reject(new Error("This plugin is disabled.")); sessions.add(id); const pending = invoke("pty_open", { id, cols, rows, cwd, profile, directory }); opening.add(pending); void pending.finally(() => opening.delete(pending)).catch(() => {}); return pending; } export function ptyProfiles(): Promise { return invoke("pty_profiles"); } export function ptyNewWindow(): Promise { return invoke("pty_new_window", { id: crypto.randomUUID() }); } export function ptyDictation(): Promise { return invoke("pty_dictation"); } export function ptyWrite(id: string, data: string): Promise { return invoke("pty_write", { id, data }); } export function ptyResize(id: string, cols: number, rows: number): Promise { return invoke("pty_resize", { id, cols, rows }); } export function ptyClose(id: string): Promise { const pending = closing.get(id); if (pending) return pending; const close = invoke("pty_close", { id }).then(() => { sessions.delete(id); }).finally(() => closing.delete(id)); closing.set(id, close); return close; }