Skip to content
TypeScript

fix(web): pull request checks stay current after a checks refresh (#25008)

Julius Marmingef4e140764fba
diff --git a/apps/web/src/components/RightPanelTabs.tsx b/apps/web/src/components/RightPanelTabs.tsx
index de548692f88..59bc6572428 100644
--- a/apps/web/src/components/RightPanelTabs.tsx
+++ b/apps/web/src/components/RightPanelTabs.tsx
@@ -735,7 +735,7 @@ function PullRequestSurfaceIcon({
repository: surface.repository,
number: surface.number,
};
- const sharedSummary = useSharedPullRequestSummary(resolvedEnvironmentId, reference, null);
+ const sharedSummary = useSharedPullRequestSummary(resolvedEnvironmentId, reference);
const status = linkedSnapshot ?? newestPullRequestSummary(detail, sharedSummary) ?? seed ?? null;
if (status === null) {
return <PullRequestGlyph.pullRequest className="size-3 shrink-0 text-muted-foreground" />;
diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsx
index 840573a2c07..15fe062a960 100644
--- a/apps/web/src/components/ThreadStatusIndicators.tsx
+++ b/apps/web/src/components/ThreadStatusIndicators.tsx
@@ -95,17 +95,13 @@ export function useLinkedThreadPullRequest(
current === null ? ((!supportsLinks ? linkedPullRequest : null) ?? branchPullRequest) : null;
// Preserve reference identity so sidebar renders do not touch the summary cache.
const reference = fallback == null ? null : linkedPullRequestReference(fallback);
- const queried = useEnvironmentQuery(
+ // Mounted for its answers, which land in the shared summary read below.
+ useEnvironmentQuery(
!enabled || environmentId === null || reference === null
? null
: linkedPullRequestDetailAtom({ environmentId, input: reference }),
);
- const detail = useSharedPullRequestSummary(
- environmentId,
- reference,
- queried.data,
- queried.dataUpdatedAt,
- );
+ const detail = useSharedPullRequestSummary(environmentId, reference);
if (current !== null) return currentStatus;
return detail === null
diff --git a/apps/web/src/components/pullRequest/PullRequestsPage.tsx b/apps/web/src/components/pullRequest/PullRequestsPage.tsx
index 57ae57924c9..65765be00d0 100644
--- a/apps/web/src/components/pullRequest/PullRequestsPage.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestsPage.tsx
@@ -205,7 +205,6 @@ export function PullRequestsPage() {
...renderedPullRequestSurface,
projectId: renderedPullRequestSurface.projectId as ProjectId,
},
- null,
);
const updateSearch = (patch: {
[Key in keyof PullRequestsSearch]?: PullRequestsSearch[Key] | undefined;
diff --git a/apps/web/src/components/pullRequest/usePullRequestDetail.ts b/apps/web/src/components/pullRequest/usePullRequestDetail.ts
index a721129fbb0..7451ee38c5f 100644
--- a/apps/web/src/components/pullRequest/usePullRequestDetail.ts
+++ b/apps/web/src/components/pullRequest/usePullRequestDetail.ts
@@ -2,11 +2,10 @@ import { resolvePullRequestDetailChecks } from "@t3tools/client-runtime/pull-req
import {
AuthSourceControlWriteScope,
type EnvironmentId,
- type PullRequestDetail,
type PullRequestListEntry,
type PullRequestRef,
} from "@t3tools/contracts";
-import { useEffect, useEffectEvent, useState } from "react";
+import { useState } from "react";
import { usePullRequestChecksRefresh } from "~/hooks/usePullRequestChecksRefresh";
import { useServerConfigs } from "~/state/entities";
@@ -21,18 +20,12 @@ import {
import {
readPullRequestDetailSnapshot,
resolveDisplayedPullRequestDetail,
- writePullRequestDetailSnapshot,
} from "./pullRequestDetail.logic";
-import { pullRequestChecksState } from "./pullRequestPresentation";
export function pullRequestRefKey(reference: PullRequestRef) {
return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
}
-function pullRequestDetailSummary(data: PullRequestDetail | null) {
- return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
-}
-
/**
* Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
* summary and the list row, into the detail the panel shows.
@@ -95,24 +88,7 @@ export function usePullRequestDetail(
} else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
setCachedDetail(detailQuery.data);
}
- const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
- writePullRequestDetailSnapshot(
- typeof window === "undefined" ? undefined : window.localStorage,
- environmentId,
- reference,
- data,
- );
- });
- useEffect(() => {
- if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
- }, [detailQuery.data]);
- const detailSummary = pullRequestDetailSummary(liveDetail);
- const observedSummary = useSharedPullRequestSummary(
- environmentId,
- reference,
- detailSummary,
- Math.max(detailQuery.dataUpdatedAt, checksQuery.dataUpdatedAt),
- );
+ const observedSummary = useSharedPullRequestSummary(environmentId, reference);
const activity = activityQuery.data;
const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
live: liveDetail,
@@ -122,8 +98,7 @@ export function usePullRequestDetail(
// Every hook runs above this line: a hook between the merge and the panel's plain readers of
// `detail` would cost it its cache scope.
const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
- // The list row is also published to the shared cache, but only after this commit's layout
- // effects run, so it is compared directly rather than trusted to be there already.
+ // The row can come from the page's stored snapshot, which no answer has published yet.
const sharedSummary = newestPullRequestSummary(
resolvedCoreDetail,
newestPullRequestSummary(observedSummary, listSummary),
diff --git a/apps/web/src/state/pullRequests.test.ts b/apps/web/src/state/pullRequests.test.ts
index 9bdd84c96c2..d524501740c 100644
--- a/apps/web/src/state/pullRequests.test.ts
+++ b/apps/web/src/state/pullRequests.test.ts
@@ -4,6 +4,9 @@ import { describe, expect, it } from "vite-plus/test";
import {
newestPullRequestObservation,
+ observedPullRequestSummary,
+ observePullRequestChecks,
+ observePullRequestMetadata,
newestPullRequestSummary,
threadPullRequestLinkToSummary,
} from "./pullRequests";
@@ -114,3 +117,73 @@ describe("pull request summary cache", () => {
).toBeNull();
});
});
+
+describe("pull request observations", () => {
+ const fromSummary = (value: PullRequestSummary, observedAt: number) =>
+ observePullRequestMetadata({ metadata: null, checks: null }, observed(value, observedAt));
+ const checksRead = (
+ checksState: "passing" | "failing" | "pending" | null,
+ readAt: number | undefined,
+ observedAt: number,
+ ) => ({ checksState, readAt, observedAt });
+
+ it("keeps a newer list's mergeability when a checks read lands after it", () => {
+ const detail = summary({
+ mergeability: "conflicting",
+ checksState: "pending",
+ observedAt: 100,
+ });
+ let state = fromSummary(detail, 1);
+ state = observePullRequestMetadata(
+ state,
+ observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 200 }), 2),
+ );
+ state = observePullRequestChecks(state, checksRead("passing", 300, 3));
+ expect(observedPullRequestSummary(state)).toMatchObject({
+ mergeability: "mergeable",
+ checksState: "passing",
+ });
+ });
+
+ it("keeps a newer checks read when an older list read lands after it", () => {
+ let state = fromSummary(
+ summary({ mergeability: "conflicting", checksState: "pending", observedAt: 100 }),
+ 1,
+ );
+ state = observePullRequestChecks(state, checksRead("passing", 300, 2));
+ state = observePullRequestMetadata(
+ state,
+ observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 250 }), 3),
+ );
+ expect(observedPullRequestSummary(state)).toMatchObject({
+ mergeability: "mergeable",
+ checksState: "passing",
+ });
+ });
+
+ it("lets a newer read's checks replace an older checks read", () => {
+ let state = fromSummary(summary({ checksState: "pending", observedAt: 100 }), 1);
+ state = observePullRequestChecks(state, checksRead("passing", 300, 2));
+ state = observePullRequestMetadata(
+ state,
+ observed(summary({ checksState: "failing", observedAt: 400 }), 3),
+ );
+ expect(observedPullRequestSummary(state)?.checksState).toBe("failing");
+ });
+
+ it("orders checks by arrival when the server reports no read time", () => {
+ let state = fromSummary(summary({ checksState: "pending" }), 1);
+ state = observePullRequestChecks(state, checksRead("passing", undefined, 2));
+ expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
+ state = observePullRequestChecks(state, checksRead("failing", undefined, 1));
+ expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
+ });
+
+ it("shows nothing until some read has described the pull request", () => {
+ const state = observePullRequestChecks(
+ { metadata: null, checks: null },
+ checksRead("failing", 1, 1),
+ );
+ expect(observedPullRequestSummary(state)).toBeNull();
+ });
+});
diff --git a/apps/web/src/state/pullRequests.ts b/apps/web/src/state/pullRequests.ts
index 7e39460dbc7..e8ec9a7a407 100644
--- a/apps/web/src/state/pullRequests.ts
+++ b/apps/web/src/state/pullRequests.ts
@@ -8,6 +8,7 @@ import {
import type {
EnvironmentId,
ProjectId,
+ PullRequestChecksState,
PullRequestListInput,
PullRequestFilterOptionsInput,
PullRequestListStatsInput,
@@ -20,9 +21,10 @@ import {
threadPullRequestKeyOf,
visibleThreadPullRequests,
} from "@t3tools/client-runtime/thread-pull-requests";
+import * as Clock from "effect/Clock";
+import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
-import { AsyncResult, Atom } from "effect/reactivity";
-import { useLayoutEffect } from "react";
+import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";
import { connectionAtomRuntime } from "../connection/runtime";
import { appAtomRegistry } from "../rpc/atomRegistry";
@@ -31,28 +33,17 @@ import {
type EnvironmentPullRequestStat,
type MergedPullRequestList,
} from "../components/pullRequest/pullRequestList.logic";
+import { writePullRequestDetailSnapshot } from "../components/pullRequest/pullRequestDetail.logic";
+import { pullRequestChecksState } from "../components/pullRequest/pullRequestPresentation";
import { formatEnvironmentQueryError } from "./query";
import { environmentThreadShells } from "./threads";
-export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime);
-export const linkedPullRequestDetailAtom = createLinkedPullRequestSummaryAtomFamily(
- connectionAtomRuntime,
- pullRequestEnvironment.refreshes,
-);
-
export interface ObservedPullRequestSummary {
readonly summary: PullRequestSummary;
/** Client arrival time, the only ordering older servers leave us for same-dated snapshots. */
readonly observedAt: number;
}
-const observedPullRequestSummaryAtom = Atom.family((key: string) =>
- Atom.make<ObservedPullRequestSummary | null>(null).pipe(
- Atom.setIdleTTL(5 * 60_000),
- Atom.withLabel(`web-pull-requests:observed-summary:${key}`),
- ),
-);
-
function compareSummaries(current: PullRequestSummary, incoming: PullRequestSummary): number {
const merged = Number(incoming.state === "merged") - Number(current.state === "merged");
if (merged !== 0) return merged;
@@ -136,18 +127,151 @@ export function newestPullRequestObservation(
};
}
+export interface ObservedPullRequestChecks {
+ readonly checksState: PullRequestChecksState | null;
+ /** Server read time, when the server reports one. */
+ readonly readAt: number | undefined;
+ /** Client arrival time. */
+ readonly observedAt: number;
+}
+
+/**
+ * Checks are reread far more often than the rest of a pull request, so they keep their own
+ * freshness: a checks read must not carry old metadata forward, and a list read must not take
+ * back newer checks.
+ */
+export interface PullRequestObservations {
+ readonly metadata: ObservedPullRequestSummary | null;
+ readonly checks: ObservedPullRequestChecks | null;
+}
+
+const noObservations: PullRequestObservations = { metadata: null, checks: null };
+
+const pullRequestObservationsAtom = Atom.family((key: string) =>
+ Atom.make<PullRequestObservations>(noObservations).pipe(
+ Atom.setIdleTTL(5 * 60_000),
+ Atom.withLabel(`web-pull-requests:observations:${key}`),
+ ),
+);
+
+export function observePullRequestChecks(
+ current: PullRequestObservations,
+ incoming: ObservedPullRequestChecks,
+): PullRequestObservations {
+ const previous = current.checks;
+ const newer =
+ previous === null ||
+ (previous.readAt !== undefined && incoming.readAt !== undefined
+ ? incoming.readAt >= previous.readAt
+ : incoming.observedAt >= previous.observedAt);
+ return newer ? { ...current, checks: incoming } : current;
+}
+
+export function observePullRequestMetadata(
+ current: PullRequestObservations,
+ incoming: ObservedPullRequestSummary,
+): PullRequestObservations {
+ const metadata = newestPullRequestObservation(current.metadata, incoming);
+ const next = metadata === current.metadata ? current : { ...current, metadata };
+ const { checksState } = incoming.summary;
+ return checksState === undefined
+ ? next
+ : observePullRequestChecks(next, {
+ checksState,
+ readAt: incoming.summary.observedAt,
+ observedAt: incoming.observedAt,
+ });
+}
+
+export function observedPullRequestSummary(
+ observations: PullRequestObservations,
+): PullRequestSummary | null {
+ const { metadata, checks } = observations;
+ if (metadata === null) return null;
+ return checks === null
+ ? metadata.summary
+ : { ...metadata.summary, checksState: checks.checksState };
+}
+
+function recordObservation(
+ registry: AtomRegistry.AtomRegistry,
+ key: string,
+ observe: (current: PullRequestObservations) => PullRequestObservations,
+): void {
+ registry.modify(pullRequestObservationsAtom(key), (previous) => {
+ const next = observe(previous);
+ return next === previous ? [false, previous] : [true, next];
+ });
+}
+
function observePullRequestSummary(
+ registry: AtomRegistry.AtomRegistry,
key: string,
summary: PullRequestSummary,
observedAt: number,
): void {
- const atom = observedPullRequestSummaryAtom(key);
- appAtomRegistry.modify(atom, (previous) => {
- const next = newestPullRequestObservation(previous, { summary, observedAt });
- return next === previous ? [false, previous] : [true, next];
- });
+ recordObservation(registry, key, (current) =>
+ observePullRequestMetadata(current, { summary, observedAt }),
+ );
}
+function snapshotStorage(): Storage | undefined {
+ return typeof window === "undefined" ? undefined : window.localStorage;
+}
+
+// Answers are stamped on arrival: older servers leave no other ordering for same-dated reads.
+export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
+ list: ({ environmentId }, answer, registry) =>
+ Effect.map(Clock.currentTimeMillis, (observedAt) => {
+ for (const entry of answer.entries) {
+ observePullRequestSummary(
+ registry,
+ pullRequestSummaryKey(environmentId, entry),
+ pullRequestListEntryToSummary(entry),
+ observedAt,
+ );
+ }
+ }),
+ detail: ({ environmentId, input }, detail, registry) =>
+ Effect.map(Clock.currentTimeMillis, (at) => {
+ // Only fresh reads seed the cold-start snapshot; a stale one could replace a newer record.
+ if (input.allowStale === false) {
+ writePullRequestDetailSnapshot(snapshotStorage(), environmentId, input, detail);
+ }
+ observePullRequestSummary(
+ registry,
+ pullRequestSummaryKey(environmentId, input),
+ { ...detail, checksState: pullRequestChecksState(detail.checks) },
+ at,
+ );
+ }),
+ checks: ({ environmentId, input }, checks, registry) =>
+ Effect.map(Clock.currentTimeMillis, (observedAt) => {
+ if (checks === null) return;
+ recordObservation(registry, pullRequestSummaryKey(environmentId, input), (current) =>
+ observePullRequestChecks(current, {
+ checksState: pullRequestChecksState(checks.checks),
+ readAt: checks.observedAt,
+ observedAt,
+ }),
+ );
+ }),
+});
+
+export const linkedPullRequestDetailAtom = createLinkedPullRequestSummaryAtomFamily(
+ connectionAtomRuntime,
+ pullRequestEnvironment.refreshes,
+ ({ environmentId, input }, summary, registry) =>
+ Effect.map(Clock.currentTimeMillis, (observedAt) =>
+ observePullRequestSummary(
+ registry,
+ pullRequestSummaryKey(environmentId, input),
+ summary,
+ observedAt,
+ ),
+ ),
+);
+
export function threadPullRequestLinkToSummary(
link: ThreadPullRequestLink,
projectId: ProjectId,
@@ -196,18 +320,16 @@ const savedPullRequestSummaryAtom = Atom.family((key: string) =>
),
);
+/** The newest of every list, detail, checks and linked-summary answer and the thread's saved link. */
export function useSharedPullRequestSummary(
environmentId: EnvironmentId | null,
reference: PullRequestRef | null,
- current: PullRequestSummary | null,
- observedAt: number | null = null,
): PullRequestSummary | null {
const key =
environmentId === null || reference === null
? "none"
: pullRequestSummaryKey(environmentId, reference);
- const atom = observedPullRequestSummaryAtom(key);
- const observed = useAtomValue(atom);
+ const observations = useAtomValue(pullRequestObservationsAtom(key));
const saved = useAtomValue(
savedPullRequestSummaryAtom(
environmentId === null || reference === null
@@ -215,16 +337,7 @@ export function useSharedPullRequestSummary(
: savedPullRequestKey(environmentId, reference),
),
);
- useLayoutEffect(() => {
- if (key === "none" || current === null || observedAt === null) return;
- observePullRequestSummary(key, current, observedAt);
- }, [current, key, observedAt]);
- const summary =
- newestPullRequestObservation(
- observed,
- current === null || observedAt === null ? null : { summary: current, observedAt },
- )?.summary ?? current;
- return newestPullRequestSummary(summary, saved);
+ return newestPullRequestSummary(observedPullRequestSummary(observations), saved);
}
export const pullRequestStackAtom = createPullRequestStackAtomFamily(
connectionAtomRuntime,
@@ -240,7 +353,6 @@ interface MergedEnvironmentQueryView<A> {
readonly values: ReadonlyArray<readonly [EnvironmentId, A]>;
readonly error: string | null;
readonly isPending: boolean;
- readonly observations: ReadonlyArray<readonly [EnvironmentId, A, number]>;
}
// Use one derived atom for variable fan-out; unreachable environments must not hide other rows.
@@ -254,7 +366,6 @@ function createMergedEnvironmentQuery<Input, A>(
Atom.make((get): MergedEnvironmentQueryView<A> => {
const targets = JSON.parse(key) as ReadonlyArray<EnvironmentQueryTarget<Input>>;
const values: Array<readonly [EnvironmentId, A]> = [];
- const observations: Array<readonly [EnvironmentId, A, number]> = [];
let error: string | null = null;
let isPending = false;
for (const target of targets) {
@@ -265,16 +376,12 @@ function createMergedEnvironmentQuery<Input, A>(
}
const value = Option.getOrNull(AsyncResult.value(result));
if (value !== null) values.push([target.environmentId, value]);
- if (result._tag === "Success") {
- observations.push([target.environmentId, result.value, result.timestamp]);
- }
}
- return { values, error, isPending, observations };
+ return { values, error, isPending };
}).pipe(Atom.withLabel(`${label}:${key}`)),
);
const empty = Atom.make<MergedEnvironmentQueryView<A>>({
values: [],
- observations: [],
error: null,
isPending: false,
}).pipe(Atom.withLabel(`${label}:empty`));
@@ -337,22 +444,6 @@ export function usePullRequestList(
targets: ReadonlyArray<EnvironmentQueryTarget<PullRequestListInput>>,
): MergedPullRequestListView {
const query = usePullRequestListsQuery(targets);
- useLayoutEffect(() => {
- for (const [environmentId, answer, observedAt] of query.observations) {
- for (const entry of answer.entries) {
- observePullRequestSummary(
- pullRequestSummaryKey(environmentId, {
- projectId: entry.projectId,
- host: entry.host,
- repository: entry.repository,
- number: entry.number,
- }),
- pullRequestListEntryToSummary(entry),
- observedAt,
- );
- }
- }
- }, [query.observations]);
const data = mergePullRequestLists(query.values);
return { data, error: query.error, isPending: query.isPending, refresh: query.refresh };
}
diff --git a/packages/client-runtime/src/state/pullRequests.ts b/packages/client-runtime/src/state/pullRequests.ts
index 14868d7f374..382f0ca47f6 100644
--- a/packages/client-runtime/src/state/pullRequests.ts
+++ b/packages/client-runtime/src/state/pullRequests.ts
@@ -13,6 +13,7 @@ import {
type EnvironmentId,
type PullRequestActor,
type PullRequestActionInput,
+ type PullRequestListInput,
type PullRequestDetail,
type PullRequestDiffInput,
type PullRequestRef,
@@ -36,6 +37,7 @@ import {
createEnvironmentRpcQueryAtomFamily,
createEnvironmentRpcSubscriptionAtomFamily,
createEnvironmentQueryAtomFamily,
+ type QueryAnswerHandler,
} from "./runtime.ts";
import { createPullRequestRouter } from "./pullRequestRouting.ts";
import * as PullRequestDiffLoader from "./pullRequestDiffHttp.ts";
@@ -123,6 +125,7 @@ function createPullRequestRefreshAtomFamily<R, E>(
export function createLinkedPullRequestSummaryAtomFamily<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
refreshes = createPullRequestRefreshAtomFamily(runtime),
+ onAnswer?: QueryAnswerHandler<PullRequestRef, PullRequestSummary>,
) {
const routedRequest = createPullRequestRouter();
return createEnvironmentRpcQueryAtomFamily(runtime, {
@@ -134,9 +137,17 @@ export function createLinkedPullRequestSummaryAtomFamily<R, E>(
idleTtlMs: LINKED_PULL_REQUEST_IDLE_TTL_MS,
refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
isFinal: (summary) => summary.state === "merged",
+ ...(onAnswer === undefined ? {} : { onAnswer }),
});
}
+/** Lets a client record what each answer says where it lands, rather than watching the data. */
+export interface PullRequestAnswerHandlers {
+ readonly list?: QueryAnswerHandler<PullRequestListInput, PullRequestListResult>;
+ readonly detail?: QueryAnswerHandler<PullRequestRef, PullRequestDetail>;
+ readonly checks?: QueryAnswerHandler<PullRequestRef, PullRequestChecks | null>;
+}
+
export function createPullRequestStackAtomFamily<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
refreshes = createPullRequestRefreshAtomFamily(runtime),
@@ -172,6 +183,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
EnvironmentRegistry | PullRequestDiffLoader.PullRequestDiffLoader | R,
E
>,
+ onAnswer: PullRequestAnswerHandlers = {},
) {
const refreshes = createPullRequestRefreshAtomFamily(runtime);
const commandScheduler = createAtomCommandScheduler();
@@ -198,6 +210,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
execute: (input) => routedRequest(WS_METHODS.pullRequestsDetail, input),
staleTimeMs: 60_000,
refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
+ ...(onAnswer.detail === undefined ? {} : { onAnswer: onAnswer.detail }),
}),
);
const preview = writableQueryFamily(
@@ -329,6 +342,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
staleTimeMs: 30_000,
refreshTrigger: ({ environmentId, input }) =>
input.cursors === undefined ? refreshes({ environmentId, input: {} }) : undefined,
+ ...(onAnswer.list === undefined ? {} : { onAnswer: onAnswer.list }),
}),
/**
* Its own query because the listing is quicker without them — measured over twelve
@@ -358,6 +372,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
execute: (input) => routedRequest(WS_METHODS.pullRequestsChecks, input),
staleTimeMs: 45_000,
refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
+ ...(onAnswer.checks === undefined ? {} : { onAnswer: onAnswer.checks }),
}),
),
activity,
diff --git a/packages/client-runtime/src/state/runtime.test.ts b/packages/client-runtime/src/state/runtime.test.ts
index b563bd8a04a..5b348d01202 100644
--- a/packages/client-runtime/src/state/runtime.test.ts
+++ b/packages/client-runtime/src/state/runtime.test.ts
@@ -44,6 +44,7 @@ import {
createRuntimeCommand,
scheduleAtomCommandEffect,
executeAtomQuery,
+ type QueryAnswerHandler,
} from "./runtime.ts";
const QUERY_ENVIRONMENT = new PrimaryConnectionTarget({
@@ -92,8 +93,10 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
initialState,
rpcSession,
isFinal,
+ onAnswer,
}: {
readonly refreshTrigger?: Atom.Atom<unknown>;
+ readonly onAnswer?: QueryAnswerHandler<undefined, A>;
readonly isFinal?: (value: A) => boolean;
readonly idleTtlMs?: number;
readonly persistence?: QueryPersistence<A>;
@@ -142,6 +145,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
...(refreshTrigger === undefined ? {} : { refreshTrigger: () => refreshTrigger }),
...(idleTtlMs === undefined ? {} : { idleTtlMs }),
...(isFinal === undefined ? {} : { isFinal }),
+ ...(onAnswer === undefined ? {} : { onAnswer }),
});
return {
@@ -1152,6 +1156,55 @@ describe("persisted environment queries", () => {
),
);
+ it.effect("hands each server answer to onAnswer before readers see it", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const answers: Array<readonly [string, string | null]> = [];
+ let served = 0;
+ const recorded = Atom.make<string | null>(null).pipe(Atom.keepAlive);
+ const harness = yield* makeEnvironmentQueryHarness(
+ Effect.sync(() => `answer-${++served}`),
+ {
+ onAnswer: (_target, value, registry) =>
+ Effect.sync(() => {
+ answers.push([value, registry.get(recorded)]);
+ registry.set(recorded, value);
+ }),
+ },
+ );
+ const registry = yield* mountEnvironmentQuery(harness.atom);
+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("answer-1");
+ expect(registry.get(recorded)).toBe("answer-1");
+ registry.refresh(harness.atom);
+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("answer-2");
+ expect(answers).toEqual([
+ ["answer-1", null],
+ ["answer-2", "answer-1"],
+ ]);
+ }),
+ ),
+ );
+
+ it.effect("does not hand a value restored from persistence to onAnswer", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const answers: Array<string> = [];
+ const harness = yield* makeEnvironmentQueryHarness(Effect.succeed("server"), {
+ persistence: { load: () => Effect.succeedSome("cached"), save: () => Effect.void },
+ initialState: queryConnectionState({ phase: "offline", network: "offline" }),
+ onAnswer: (_target, value) => Effect.sync(() => answers.push(value)),
+ });
+ const registry = yield* mountEnvironmentQuery(harness.atom);
+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("cached");
+ expect(answers).toEqual([]);
+ yield* SubscriptionRef.set(harness.supervisorState, queryConnectionState());
+ yield* Effect.yieldNow;
+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("server");
+ expect(answers).toEqual(["server"]);
+ }),
+ ),
+ );
+
it.effect("keeps cached content available when revalidation fails", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/packages/client-runtime/src/state/runtime.ts b/packages/client-runtime/src/state/runtime.ts
index 5a408d61e92..79b1ec4ac5e 100644
--- a/packages/client-runtime/src/state/runtime.ts
+++ b/packages/client-runtime/src/state/runtime.ts
@@ -101,8 +101,20 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
readonly input: Input;
}) => Atom.Atom<unknown> | undefined;
readonly isFinal?: (value: A) => boolean;
+ /**
+ * Runs where each server answer lands, before readers see it, so derived records (summaries,
+ * snapshots, watches) come from the producer instead of from components watching the data.
+ * Cached and emitted provisional values are not answers.
+ */
+ readonly onAnswer?: QueryAnswerHandler<Input, A>;
}
+export type QueryAnswerHandler<Input, A> = (
+ target: { readonly environmentId: EnvironmentIdType; readonly input: Input },
+ value: A,
+ registry: AtomRegistry.AtomRegistry,
+) => Effect.Effect<void>;
+
interface EnvironmentSubscriptionAtomOptions<Input, A, E, R> {
readonly sensitiveInput?: boolean;
readonly label: string;
@@ -652,7 +664,15 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
);
}
};
- if (!options.persistence) return execute().pipe(Effect.map(share));
+ const onAnswer = options.onAnswer;
+ const answer = () =>
+ onAnswer === undefined
+ ? execute().pipe(Effect.map(share))
+ : execute().pipe(
+ Effect.map(share),
+ Effect.tap((value) => onAnswer(target, value, get.registry)),
+ );
+ if (!options.persistence) return answer();
const persistence = options.persistence;
const cacheKey = JSON.stringify([options.label, target.input]);
const previous = Option.flatMap(
@@ -670,8 +690,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
return cached.value;
}
if (Option.isNone(previous) && Option.isSome(cached)) yield* emit(cached.value);
- return yield* execute().pipe(
- Effect.map(share),
+ return yield* answer().pipe(
Effect.tap((value) => persistence.save(target.environmentId, cacheKey, value)),
);
});
@@ -776,6 +795,7 @@ export function createEnvironmentRpcQueryAtomFamily<
readonly input: EnvironmentRpcInput<TTag>;
}) => Atom.Atom<unknown> | undefined;
readonly isFinal?: (value: EnvironmentRpcSuccess<TTag>) => boolean;
+ readonly onAnswer?: QueryAnswerHandler<EnvironmentRpcInput<TTag>, EnvironmentRpcSuccess<TTag>>;
},
) {
return createEnvironmentQueryAtomFamily(runtime, {
@@ -788,6 +808,7 @@ export function createEnvironmentRpcQueryAtomFamily<
: { refreshIntervalMs: options.refreshIntervalMs }),
...(options.refreshTrigger === undefined ? {} : { refreshTrigger: options.refreshTrigger }),
...(options.isFinal === undefined ? {} : { isFinal: options.isFinal }),
+ ...(options.onAnswer === undefined ? {} : { onAnswer: options.onAnswer }),
execute: (
input: EnvironmentRpcInput<TTag>,
emit: (value: EnvironmentRpcSuccess<TTag>) => Effect.Effect<void>,