Keep retrying the connection and say why; never lose a message that didn't send

As the web's connection supervisor and composer:
- The first connection retries too, so tui3 opens without a server and
  connects when it's up. One-off commands still fail fast.
- The header says why it couldn't connect and when it retries; r, a
  click on it, or the palette retries now.
- Enter while disconnected keeps the message and says so.
- A failed send, or a new thread that doesn't start, puts the message,
  its attachments and its mentions back in the composer.
- Opening a PR in the browser says why it failed and copies the link.
- Error notices stay 8s instead of 4s.
This commit is contained in:
Callum Alpass committed 2026-11-02 23:13:06 +11:00
1 parent 275a2bcde1
commit 2c5eedc869
9 files changed
+169 -42

No files matched your search

+20 -7
View File
@@ -89,11 +89,12 @@ import { applyShellStreamEvent, mergeShellSnapshotProjects } from "../../vendor/
import { applyCommandOutputFrame, EMPTY_COMMAND_OUTPUT, type CommandOutputView } from "@t3tools/client-runtime/state/command-output";
import { connect, type Connection } from "./rpc.ts";
/** As the web's supervisor: it keeps retrying, first connection included, with the last failure as the reason. */
export type ConnectionStatus =
| { readonly type: "connecting" }
| { readonly type: "live" }
| { readonly type: "reconnecting"; readonly message: string }
| { readonly type: "error"; readonly message: string };
/** `retryAt`: when the next attempt starts (epoch ms); null while one is under way. */
| { readonly type: "reconnecting"; readonly message: string; readonly retryAt: number | null };
export interface ShellState {
readonly status: ConnectionStatus;
@@ -207,9 +208,7 @@ export class T3Session {
} catch (error) {
this.scope = null;
await Effect.runPromise(Scope.close(scope, Exit.void));
if (this.reconnectAttempt > 0) return this.scheduleReconnect(messageOf(error));
this.setShell({ status: { type: "error", message: messageOf(error) } });
return;
return this.scheduleReconnect(messageOf(error));
}
this.recovering = false;
this.setShell({ serverUrl: this.connection.target.httpBaseUrl });
@@ -224,6 +223,14 @@ export class T3Session {
await this.closeScope();
}
/** Skips the wait before the next reconnect attempt. */
retryNow(): void {
if (this.stopped || !this.reconnectTimer) return;
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
this.attempt();
}
getShell(): ShellState {
return this.shell;
}
@@ -1109,13 +1116,19 @@ export class T3Session {
if (this.stopped || this.reconnectTimer) return;
const delay = RECONNECT_DELAYS_MS[Math.min(this.reconnectAttempt, RECONNECT_DELAYS_MS.length - 1)]!;
this.reconnectAttempt++;
this.setShell({ status: { type: "reconnecting", message } });
this.setShell({ status: { type: "reconnecting", message, retryAt: Date.now() + delay } });
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
void this.start();
this.attempt();
}, delay);
}
private attempt(): void {
const status = this.shell.status;
if (status.type === "reconnecting") this.setShell({ status: { ...status, retryAt: null } });
void this.start();
}
private async closeScope(): Promise<void> {
const scope = this.scope;
this.scope = null;