Skip to content

Commit ab746e9

Browse files
committed
fix(web): await healthy usage rescans
1 parent 296e270 commit ab746e9

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎apps/web/src/state/usage.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ import {
1313
type UsageSummary,
1414
type UsageSummaryInput,
1515
} from "@t3tools/contracts";
16-
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
16+
import { EnvironmentRpcUnavailableError } from "@t3tools/client-runtime/rpc";
17+
import {
18+
executeAtomQuery,
19+
runAtomCommand,
20+
squashAtomCommandFailure,
21+
} from "@t3tools/client-runtime/state/runtime";
1722
import * as Option from "effect/Option";
23+
import * as Schema from "effect/Schema";
1824
import { AsyncResult, Atom } from "effect/unstable/reactivity";
1925
import { useCallback, useMemo } from "react";
2026

@@ -23,6 +29,8 @@ import { appAtomRegistry } from "../rpc/atomRegistry";
2329
import { environmentPresentations } from "./presentation";
2430
import { serverEnvironment } from "./server";
2531

32+
const isEnvironmentRpcUnavailable = Schema.is(EnvironmentRpcUnavailableError);
33+
2634
export interface EnvironmentUsageStatus {
2735
readonly environmentId: EnvironmentId;
2836
readonly label: string;
@@ -121,12 +129,38 @@ export function useUsage(
121129
await Promise.all(
122130
selectedEnvironments.map(async ({ environmentId }) => {
123131
const query = serverEnvironment.usageSummary({ environmentId, input });
124-
await runAtomCommand(
125-
appAtomRegistry,
126-
serverEnvironment.refreshUsageRates,
127-
{ environmentId, input: {} },
128-
{ reportFailure: false },
129-
).finally(() => appAtomRegistry.refresh(query));
132+
const presentationAtom = environmentPresentations.presentationAtom(environmentId);
133+
const controller = new AbortController();
134+
const abortWhenDisconnected = () => {
135+
if (appAtomRegistry.get(presentationAtom)?.connection.phase !== "connected") {
136+
controller.abort();
137+
}
138+
};
139+
const unsubscribe = appAtomRegistry.subscribe(presentationAtom, abortWhenDisconnected);
140+
abortWhenDisconnected();
141+
142+
try {
143+
const ratesResult = await runAtomCommand(
144+
appAtomRegistry,
145+
serverEnvironment.refreshUsageRates,
146+
{ environmentId, input: {} },
147+
{ reportFailure: false },
148+
);
149+
const sessionUnavailable =
150+
ratesResult._tag === "Failure" &&
151+
isEnvironmentRpcUnavailable(squashAtomCommandFailure(ratesResult));
152+
if (sessionUnavailable || controller.signal.aborted) {
153+
appAtomRegistry.refresh(query);
154+
return;
155+
}
156+
await executeAtomQuery(appAtomRegistry, query, {
157+
refresh: true,
158+
reportFailure: false,
159+
signal: controller.signal,
160+
});
161+
} finally {
162+
unsubscribe();
163+
}
130164
}),
131165
);
132166
},

‎packages/client-runtime/src/state/runtime.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,24 @@ describe("executeAtomQuery", () => {
683683

684684
registry.dispose();
685685
});
686+
687+
it("settles when its caller aborts a waiting query", async () => {
688+
const registry = AtomRegistry.make();
689+
const controller = new AbortController();
690+
const resultPromise = executeAtomQuery(registry, Atom.make(Effect.never), {
691+
reportDefect: false,
692+
signal: controller.signal,
693+
});
694+
695+
controller.abort();
696+
697+
const result = await resultPromise;
698+
expect(result._tag).toBe("Failure");
699+
if (result._tag === "Failure") {
700+
expect(Cause.hasInterruptsOnly(result.cause)).toBe(true);
701+
}
702+
registry.dispose();
703+
});
686704
});
687705

688706
describe("runtime command runner", () => {

‎packages/client-runtime/src/state/runtime.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ export interface AtomQueryOptions extends AtomCommandOptions {
336336
* verification flows where a cached failure must not satisfy a retry.
337337
*/
338338
readonly refresh?: boolean;
339+
/** Interrupt the query wait when its caller no longer wants the result. */
340+
readonly signal?: AbortSignal;
339341
}
340342

341343
export async function executeAtomQuery<A, E>(
@@ -362,7 +364,11 @@ export async function executeAtomQuery<A, E>(
362364
});
363365
}),
364366
);
365-
return executeAtomCommand(() => Effect.runPromiseExit(query), options, reporter);
367+
return executeAtomCommand(
368+
() => Effect.runPromiseExit(query, { signal: options.signal }),
369+
options,
370+
reporter,
371+
);
366372
}
367373

368374
export function createRuntimeCommand<R, ER, W, A, E>(

0 commit comments

Comments
 (0)