terminals.svelte.ts view source
import {Terminals} from '@fuzdev/zzz/terminals.svelte.js'; App-level collection of terminal runs — outlives the views that render them, so navigating away and back reattaches to live terminals and their output.
Every terminal spawns a shell (sh) and types its command line into it,
so the session stays alive for follow-up input.
After a reconnect, reconcile marks the terminals the backend no longer has
as lost (from the session_load snapshot's terminal_ids).
TODO the list is in-memory, so a page reload loses it while the backend PTYs
keep running — session_load lists their ids, but reattaching would also
need their commands and output.
inheritance
Cell<typeof TerminalsJson>items
type IndexedCollection<Terminal>
constructor
type new (options: TerminalsOptions): Terminals
options
get_by_terminal_id
Looks up a terminal by its backend terminal_id.
type (terminal_id: string & $brand<"Uuid">): Terminal | undefined
terminal_id
UuidTerminal | undefinedadd
Adds a terminal cell without spawning a process. See create to spawn one.
type (json?: { id?: string | undefined; created?: string | undefined; updated?: string | undefined; name?: string | undefined; command?: string | undefined; args?: string[] | undefined; cwd?: string | undefined; ... 4 more ...; preset_id?: string | ... 1 more ... | undefined; } | undefined): Terminal
json?
create
Adds a terminal and spawns its process. A spawn failure is recorded on the
terminal (status: 'failed' + error_message), so this doesn't reject.
type (json: Pick<{ id?: string | undefined; created?: string | undefined; updated?: string | undefined; name?: string | undefined; command?: string | undefined; args?: string[] | undefined; cwd?: string | undefined; ... 4 more ...; preset_id?: string | ... 1 more ... | undefined; }, "name" | ... 3 more ... | "preset_id">): Promise<...>
json
Pick<TerminalJsonInput, "name" | "command" | "args" | "cwd" | "preset_id">Promise<Terminal>restart
Closes the terminal's process if it's running, then spawns a fresh one
with a new terminal_id and cleared output. Does nothing if the close
fails — the error is on the terminal — to avoid orphaning a live process.
type (terminal: Terminal): Promise<void>
terminal
Promise<void>remove
Closes the terminal's process if it's running, then removes and disposes the terminal. Keeps it if the close fails, so the process stays reachable. A terminal removed while starting has its process closed once spawned.
type (terminal: Terminal): Promise<void>
terminal
Promise<void>clear
Removes and disposes every terminal without closing their processes.
type (): void
voidreceive_output
Routes a terminal_data notification to its terminal, buffering it
whether or not a view is mounted.
type (terminal_id: string & $brand<"Uuid">, data: string, sent_bytes: number): void
terminal_id
Uuiddata
stringsent_bytes
numbervoidreceive_exited
Routes a terminal_exited notification to its terminal, recording the exit
whether or not a view is mounted.
type (terminal_id: string & $brand<"Uuid">, exit_code: number | null): void
terminal_id
Uuidexit_code
number | nullvoidrunning_terminal_ids
The backend ids of the running terminals.
type (): Set<string & $brand<"Uuid">>
Set<Uuid>mark_output_gap
Marks every running terminal as possibly missing output (see
Terminal.output_gap) — called when the socket reconnects.
type (): void
voidreconcile
Reconciles with the backend's live terminals from a session snapshot: a
running terminal it doesn't list is marked lost, since its
terminal_exited (if any) was missed.
type (live_terminal_ids: ReadonlySet<string & $brand<"Uuid">>, known_terminal_ids: ReadonlySet<string & $brand<"Uuid">>, reason: string): void
live_terminal_ids
the snapshot's terminal_ids
ReadonlySet<Uuid>known_terminal_ids
the ids running when the snapshot was requested; only these are judged, since a terminal started after the request may postdate the snapshot
ReadonlySet<Uuid>reason
the lost terminals' error_message
stringvoiddispose
type (): void
void