Skip to content

Commit c989141

Browse files
fix(client-runtime): searches and refreshes no longer wait on an unreachable environment
A query an environment had never answered stayed pending for as long as that environment was connecting or backing off, which can be minutes. On mobile, a pull-to-refresh of a searched pull request list waited on it; the web pull request list and thread search showed loading the same way. While retrying after a failure, a query with nothing to show now fails with that failure, as it already does offline, and re-runs on reconnect. A query with an answer, live or cached, keeps showing it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 08bd2ec commit c989141

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ describe("environment query lifecycle", () => {
553553
harness.supervisorState,
554554
queryConnectionState({ generation: 2 }),
555555
);
556+
yield* Effect.yieldNow;
556557
expect(
557558
yield* AtomRegistry.getResult(registry, harness.atom, {
558559
suspendOnWaiting: true,
@@ -707,6 +708,33 @@ describe("environment query lifecycle", () => {
707708
),
708709
);
709710

711+
it.effect.each([
712+
{ phase: "backoff", stage: null, retryAt: 1 },
713+
{ phase: "connecting", stage: "opening", retryAt: null },
714+
] as const)(
715+
"settles a query it never answered while its environment retries ($phase)",
716+
(retrying) =>
717+
Effect.scoped(
718+
Effect.gen(function* () {
719+
const harness = yield* makeEnvironmentQueryHarness(Effect.succeed("connected"), {
720+
initialState: queryConnectionState({ ...retrying, lastFailure: OFFLINE_QUERY_FAILURE }),
721+
});
722+
const registry = yield* mountEnvironmentQuery(harness.atom);
723+
yield* Effect.yieldNow;
724+
725+
const result = registry.get(harness.atom);
726+
expect(result.waiting).toBe(false);
727+
expect(AsyncResult.isFailure(result) && Cause.squash(result.cause)).toBe(
728+
OFFLINE_QUERY_FAILURE,
729+
);
730+
731+
yield* SubscriptionRef.set(harness.supervisorState, queryConnectionState());
732+
yield* Effect.yieldNow;
733+
expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("connected");
734+
}),
735+
),
736+
);
737+
710738
it.effect("keeps a genuine query failure settled while reconnecting", () =>
711739
Effect.scoped(
712740
Effect.gen(function* () {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,18 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
647647
: Effect.never;
648648
case "connecting":
649649
case "backoff":
650-
return Effect.never;
650+
// A retry can take minutes. A query with an answer keeps showing it while it
651+
// waits; one with nothing to show reports the failure the retry is recovering
652+
// from, so its reader stops loading, and re-runs when the connection settles.
653+
return connectionState.lastFailure !== null &&
654+
Option.isNone(
655+
Option.flatMap(
656+
get.self<AsyncResult.AsyncResult<A, unknown>>(),
657+
AsyncResult.value,
658+
),
659+
)
660+
? Effect.fail(connectionState.lastFailure)
661+
: Effect.never;
651662
case "available":
652663
case "offline":
653664
case "blocked":

0 commit comments

Comments
 (0)