diff --git a/packages/client-runtime/src/j5/inbox.ts b/packages/client-runtime/src/j5/inbox.ts index 03624b04aa704..3c1a18b1845e9 100644 --- a/packages/client-runtime/src/j5/inbox.ts +++ b/packages/client-runtime/src/j5/inbox.ts @@ -80,7 +80,9 @@ export function j5SourceNotice(source: J5ReadSource): string | null { case "loading": return `${source.environmentLabel}: loading…`; case "offline": - return `${source.environmentLabel}: offline${source.data === null ? "" : " (showing saved results)"}.`; + return source.reconnecting === true + ? `${source.environmentLabel}: reconnecting…${source.data === null ? "" : " (showing saved results)."}` + : `${source.environmentLabel}: offline${source.data === null ? "" : " (showing saved results)"}.`; case "unsupported": return source.guest === true ? `${source.environmentLabel}: not shared with you.` diff --git a/packages/client-runtime/src/j5/readSources.test.ts b/packages/client-runtime/src/j5/readSources.test.ts index 18320a6044aa9..0a2ef59df4a4d 100644 --- a/packages/client-runtime/src/j5/readSources.test.ts +++ b/packages/client-runtime/src/j5/readSources.test.ts @@ -53,6 +53,103 @@ it("keeps unsupported, authentication failure, and not-yet-connected states dist resolveJ5ReadSource({ ...source, phase: "connecting", result: AsyncResult.initial(false) }) .status, ).toBe("loading"); + // A retry after a failure, or the wait before one, is reconnecting, as upstream presents it: not + // a first load, and its line uses upstream's word. A client with no network reads offline. + const retrying = resolveJ5ReadSource({ + ...source, + phase: "connecting", + reconnecting: true, + result: AsyncResult.success(["cached-row"]), + }); + expect(retrying).toMatchObject({ status: "offline", data: ["cached-row"] }); + expect(j5SourceNotice(retrying)).toBe("Remote: reconnecting… (showing saved results)."); + expect( + j5SourceNotice( + resolveJ5ReadSource({ + ...source, + phase: "connecting", + reconnecting: true, + result: AsyncResult.initial(false), + }), + ), + ).toBe("Remote: reconnecting…"); + expect( + j5SourceNotice( + resolveJ5ReadSource({ + ...source, + phase: "backoff", + reconnecting: true, + result: AsyncResult.initial(false), + }), + ), + ).toBe("Remote: reconnecting…"); + expect( + j5SourceNotice( + resolveJ5ReadSource({ ...source, phase: "offline", result: AsyncResult.initial(false) }), + ), + ).toBe("Remote: offline."); + // "available" is a server nothing is connecting to, so it is not loading. + expect( + resolveJ5ReadSource({ ...source, phase: "available", result: AsyncResult.initial(false) }) + .status, + ).toBe("offline"); +}); + +it("leaves out a server the person turned off, and reads only a first connection as loading", () => { + type Input = Parameters>>[0]; + const on = EnvironmentId.make("on"); + const off = EnvironmentId.make("off"); + const starting = EnvironmentId.make("starting"); + const retrying = EnvironmentId.make("retrying"); + const waiting = EnvironmentId.make("waiting"); + const reads: Array = []; + const sources = createJ5ReadSourcesAtom>({ + label: "test:j5-read-sources-disabled", + capability: "j5ProjectLedger", + catalogValueAtom: Atom.make({ + isReady: true, + entries: new Map([ + [on, { target: { label: "On" }, enabled: true }], + [off, { target: { label: "Off" }, enabled: false }], + [starting, { target: { label: "Starting" }, enabled: true }], + [retrying, { target: { label: "Retrying" }, enabled: true }], + [waiting, { target: { label: "Waiting" }, enabled: true }], + ]), + }) as unknown as Input["catalogValueAtom"], + stateAtom: (id) => + Atom.make( + id === on + ? AsyncResult.success({ phase: "connected" }) + : id === off + ? AsyncResult.success({ phase: "available", desired: false }) + : id === retrying + ? AsyncResult.success({ phase: "connecting", attempt: 4, lastFailure: {} }) + : id === waiting + ? AsyncResult.success({ phase: "backoff", attempt: 4, lastFailure: {} }) + : AsyncResult.initial(true), + ) as unknown as ReturnType, + configValueAtom: (id) => + Atom.make( + id === starting ? null : { environment: { capabilities: { j5ProjectLedger: true } } }, + ) as unknown as ReturnType, + queryAtom: (id) => + Atom.make(() => { + reads.push(id); + return id === on + ? AsyncResult.success, unknown>(["row"]) + : AsyncResult.initial, unknown>(true); + }), + }); + const result = AtomRegistry.make().get(sources).sources; + expect( + result.map((entry) => [entry.environmentLabel, entry.status, j5SourceNotice(entry)]), + ).toEqual([ + ["On", "ready", null], + ["Starting", "loading", "Starting: loading…"], + ["Retrying", "offline", "Retrying: reconnecting…"], + ["Waiting", "offline", "Waiting: reconnecting…"], + ]); + expect(reads).not.toContain(off); }); it("shows environment labels only once items span more than one environment", () => { @@ -77,7 +174,7 @@ const readSourceFor = ( capability: capability as Input["capability"], catalogValueAtom: Atom.make({ isReady: true, - entries: new Map([[environmentId, { target: { label: "Remote" } }]]), + entries: new Map([[environmentId, { target: { label: "Remote" }, enabled: true }]]), }) as unknown as Input["catalogValueAtom"], stateAtom: () => Atom.make(AsyncResult.success({ phase: "connected" })) as unknown as ReturnType< @@ -131,7 +228,7 @@ it("shows nothing from a server once its connection is known to be a guest's", ( capability: "j5ProjectLedger", catalogValueAtom: Atom.make({ isReady: true, - entries: new Map([[source.environmentId, { target: { label: "Remote" } }]]), + entries: new Map([[source.environmentId, { target: { label: "Remote" }, enabled: true }]]), }) as unknown as Input["catalogValueAtom"], stateAtom: () => Atom.make(AsyncResult.success({ phase: "connected" })) as unknown as ReturnType< diff --git a/packages/client-runtime/src/j5/readSources.ts b/packages/client-runtime/src/j5/readSources.ts index 13cc877dfe7f7..ccc0bf66d3d2d 100644 --- a/packages/client-runtime/src/j5/readSources.ts +++ b/packages/client-runtime/src/j5/readSources.ts @@ -4,7 +4,7 @@ import * as Option from "effect/Option"; import { AsyncResult, Atom } from "effect/reactivity"; import type { SupervisorConnectionState } from "../connection/model.ts"; -import type { EnvironmentCatalogState } from "../state/connections.ts"; +import { type EnvironmentCatalogState, enabledEnvironmentIds } from "../state/connections.ts"; /** Environment labels are noise until a read model actually spans more than one environment. */ export const spansMultipleEnvironments = ( @@ -28,6 +28,8 @@ export interface J5ReadSource { readonly refreshing: boolean; /** Set when the source is unsupported because the connection is a guest's, not for the server's age. */ readonly guest?: true; + /** Set when an offline source is retrying after a failure, which upstream calls reconnecting. */ + readonly reconnecting?: true; } export interface J5ReadSources { @@ -39,6 +41,8 @@ export function resolveJ5ReadSource(input: { readonly environmentId: EnvironmentId; readonly environmentLabel: string; readonly phase: SupervisorConnectionState["phase"]; + /** A retry after a failure, or the wait before one, which upstream presents as reconnecting. */ + readonly reconnecting?: boolean; readonly supported?: boolean; readonly guest?: boolean; readonly result: AsyncResult.AsyncResult; @@ -50,7 +54,7 @@ export function resolveJ5ReadSource(input: { const status = unsupported ? "unsupported" : !connected - ? input.phase === "connecting" || input.phase === "available" + ? input.phase === "connecting" && input.reconnecting !== true ? "loading" : "offline" : failure !== null @@ -72,11 +76,13 @@ export function resolveJ5ReadSource(input: { : "The environment request failed.", refreshing: connected && input.result.waiting, ...(input.guest === true ? { guest: true as const } : {}), + ...(status === "offline" && input.reconnecting === true ? { reconnecting: true as const } : {}), }; } /** * Merge read models through the existing catalog; each source keeps its own data and failure state. + * A server the person turned off is left out, as upstream's thread and project lists leave it out. * A server whose config does not report the capability has no J5 ledger: its source is unsupported * and this read is never made. */ @@ -99,7 +105,8 @@ export function createJ5ReadSourcesAtom(input: { return Atom.make((get) => { const catalog = get(input.catalogValueAtom); const sources: Array> = []; - for (const [environmentId, entry] of catalog.entries) { + for (const environmentId of enabledEnvironmentIds(catalog)) { + const entry = catalog.entries.get(environmentId)!; const state = Option.getOrNull(AsyncResult.value(get(input.stateAtom(environmentId)))); const config = get(input.configValueAtom(environmentId)); const guest = input.guestAtom !== undefined && get(input.guestAtom(environmentId)); @@ -112,7 +119,13 @@ export function createJ5ReadSourcesAtom(input: { resolveJ5ReadSource({ environmentId, environmentLabel: entry.target.label, - phase: state?.phase ?? "available", + // Before its supervisor publishes, an enabled server is starting to connect. + phase: state?.phase ?? "connecting", + // Upstream's `presentConnectionState` rule for "Reconnecting". + reconnecting: + state !== null && + (state.phase === "backoff" || + (state.phase === "connecting" && (state.attempt > 1 || state.lastFailure !== null))), ...(supported === undefined ? {} : { supported }), guest, result: