Skip to content

Commit 8e3fced

Browse files
perf(client): equal query refetches keep the previous value
A refetch that returns the same content now keeps the previous value's references, so PR rows and their tooltips skip re-rendering on idle checks polls. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2fbd431 commit 8e3fced

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎apps/web/src/components/chat/ThreadDetailsPrRow.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ export function ThreadDetailsPrRow({
116116
? pullRequestEnvironment.checks({ environmentId, input: reference })
117117
: null,
118118
);
119+
// Every refetch moves the timestamps; comparing them first keeps `detail`
120+
// stable while the data itself is unchanged.
121+
const checksAreNewer = checksQuery.dataUpdatedAt >= detailQuery.dataUpdatedAt;
119122
const detail =
120123
detailQuery.data === null
121124
? null
122-
: checksQuery.data !== null && checksQuery.dataUpdatedAt >= detailQuery.dataUpdatedAt
125+
: checksQuery.data !== null && checksAreNewer
123126
? { ...detailQuery.data, ...checksQuery.data }
124127
: detailQuery.data;
125128
const open = reference !== null && (detail?.state ?? pr?.state) === "open";

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,28 @@ describe("finite environment subscription lifecycle", () => {
373373
});
374374

375375
describe("environment query lifecycle", () => {
376+
it.effect("keeps the previous value when a refetch returns equal content", () =>
377+
Effect.scoped(
378+
Effect.gen(function* () {
379+
let checks = [{ name: "ci", status: "passed" }];
380+
const execute = Effect.sync(() => ({ checks: checks.map((check) => ({ ...check })) }));
381+
const harness = yield* makeEnvironmentQueryHarness(execute);
382+
const registry = yield* mountEnvironmentQuery(harness.atom);
383+
384+
const first = yield* readEnvironmentQuery(registry, harness.atom);
385+
registry.refresh(harness.atom);
386+
const equal = yield* readEnvironmentQuery(registry, harness.atom);
387+
expect(equal).toBe(first);
388+
389+
checks = [{ name: "ci", status: "failed" }];
390+
registry.refresh(harness.atom);
391+
const changed = yield* readEnvironmentQuery(registry, harness.atom);
392+
expect(changed).not.toBe(first);
393+
expect(changed.checks[0]?.status).toBe("failed");
394+
}),
395+
),
396+
);
397+
376398
it.effect(
377399
"retries an interrupted query without exposing a failure during session replacement",
378400
() =>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { QueryPersistence } from "./queryPersistence.ts";
22
import { createCommandPermissions } from "./commandPermissions.ts";
33
import { followStreamInEnvironment } from "./environmentStreams.ts";
4+
import { reuseUnchanged } from "./serverConfigProjection.ts";
45
export { runStreamInEnvironment, followStreamInEnvironment } from "./environmentStreams.ts";
56
import {
67
type ClientGuardedRpcTag,
@@ -560,8 +561,17 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
560561
const connection = Option.getOrNull(
561562
AsyncResult.value(get(connectionAtom(target.environmentId))),
562563
);
564+
// A refetch that returns equal content keeps the previous value's
565+
// references, so readers of the data skip re-rendering on idle polls.
566+
const share = (value: A): A =>
567+
reuseUnchanged(
568+
Option.getOrUndefined(
569+
Option.flatMap(get.self<AsyncResult.AsyncResult<A, unknown>>(), AsyncResult.value),
570+
),
571+
value,
572+
);
563573
const emit = (value: A) =>
564-
Effect.sync(() => get.setSelf(AsyncResult.success(value, { waiting: true }))).pipe(
574+
Effect.sync(() => get.setSelf(AsyncResult.success(share(value), { waiting: true }))).pipe(
565575
// A value set while the atom builds wins over the build's own
566576
// result, so a query that then settled synchronously would stay
567577
// waiting forever. Suspend on a promise (`yieldNow` is drained
@@ -604,7 +614,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
604614
);
605615
}
606616
};
607-
if (!options.persistence) return execute();
617+
if (!options.persistence) return execute().pipe(Effect.map(share));
608618
const persistence = options.persistence;
609619
const cacheKey = JSON.stringify([options.label, target.input]);
610620
const previous = Option.flatMap(
@@ -623,6 +633,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
623633
}
624634
if (Option.isNone(previous) && Option.isSome(cached)) yield* emit(cached.value);
625635
return yield* execute().pipe(
636+
Effect.map(share),
626637
Effect.tap((value) => persistence.save(target.environmentId, cacheKey, value)),
627638
);
628639
});

0 commit comments

Comments
 (0)