Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/client-runtime/src/j5/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export function j5SourceNotice(source: J5ReadSource<unknown>): 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.`
Expand Down
101 changes: 99 additions & 2 deletions packages/client-runtime/src/j5/readSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createJ5ReadSourcesAtom<ReadonlyArray<string>>>[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<EnvironmentId> = [];
const sources = createJ5ReadSourcesAtom<ReadonlyArray<string>>({
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<Input["stateAtom"]>,
configValueAtom: (id) =>
Atom.make(
id === starting ? null : { environment: { capabilities: { j5ProjectLedger: true } } },
) as unknown as ReturnType<Input["configValueAtom"]>,
queryAtom: (id) =>
Atom.make(() => {
reads.push(id);
return id === on
? AsyncResult.success<ReadonlyArray<string>, unknown>(["row"])
: AsyncResult.initial<ReadonlyArray<string>, 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", () => {
Expand All @@ -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<
Expand Down Expand Up @@ -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<
Expand Down
21 changes: 17 additions & 4 deletions packages/client-runtime/src/j5/readSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -28,6 +28,8 @@ export interface J5ReadSource<A> {
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<A> {
Expand All @@ -39,6 +41,8 @@ export function resolveJ5ReadSource<A>(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<A, unknown>;
Expand All @@ -50,7 +54,7 @@ export function resolveJ5ReadSource<A>(input: {
const status = unsupported
? "unsupported"
: !connected
? input.phase === "connecting" || input.phase === "available"
? input.phase === "connecting" && input.reconnecting !== true
? "loading"
: "offline"
: failure !== null
Expand All @@ -72,11 +76,13 @@ export function resolveJ5ReadSource<A>(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.
*/
Expand All @@ -99,7 +105,8 @@ export function createJ5ReadSourcesAtom<A>(input: {
return Atom.make((get) => {
const catalog = get(input.catalogValueAtom);
const sources: Array<J5ReadSource<A>> = [];
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));
Expand All @@ -112,7 +119,13 @@ export function createJ5ReadSourcesAtom<A>(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:
Expand Down
Loading