perf(client-runtime): merged pull requests are no longer re-read every minute (#19374)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
519d64f6ce
commit
21af72f7a5
3 files changed
+58
-6
No files matched your search
@@ -115,7 +115,7 @@ function createPullRequestRefreshAtomFamily<R, E>(
|
||||
});
|
||||
}
|
||||
|
||||
/** Refresh only the live fields a linked thread renders. */
|
||||
/** Refresh only the live fields a linked thread renders. A merged pull request is not re-read. */
|
||||
export function createLinkedPullRequestSummaryAtomFamily<R, E>(
|
||||
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
|
||||
refreshes = createPullRequestRefreshAtomFamily(runtime),
|
||||
@@ -129,6 +129,7 @@ export function createLinkedPullRequestSummaryAtomFamily<R, E>(
|
||||
refreshIntervalMs: 60_000,
|
||||
idleTtlMs: LINKED_PULL_REQUEST_IDLE_TTL_MS,
|
||||
refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
|
||||
isFinal: (summary) => summary.state === "merged",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -88,8 +88,10 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
|
||||
idleTtlMs,
|
||||
persistence,
|
||||
initialState,
|
||||
isFinal,
|
||||
}: {
|
||||
readonly refreshTrigger?: Atom.Atom<unknown>;
|
||||
readonly isFinal?: (value: A) => boolean;
|
||||
readonly idleTtlMs?: number;
|
||||
readonly persistence?: QueryPersistence<A>;
|
||||
readonly initialState?: SupervisorConnectionState;
|
||||
@@ -127,6 +129,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
|
||||
execute: () => execute,
|
||||
...(refreshTrigger === undefined ? {} : { refreshTrigger: () => refreshTrigger }),
|
||||
...(idleTtlMs === undefined ? {} : { idleTtlMs }),
|
||||
...(isFinal === undefined ? {} : { isFinal }),
|
||||
});
|
||||
|
||||
return {
|
||||
@@ -675,6 +678,30 @@ describe("environment query lifecycle", () => {
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("stops refreshing on its trigger once the value is final", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
let executions = 0;
|
||||
const trigger = Atom.make(0).pipe(Atom.keepAlive);
|
||||
const harness = yield* makeEnvironmentQueryHarness(
|
||||
Effect.sync(() => (executions += 1)),
|
||||
{ refreshTrigger: trigger, isFinal: (value) => value >= 2 },
|
||||
);
|
||||
const registry = AtomRegistry.make();
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
|
||||
const read = readEnvironmentQuery(registry, harness.atom);
|
||||
|
||||
registry.mount(harness.atom);
|
||||
expect(yield* read).toBe(1);
|
||||
registry.set(trigger, 1);
|
||||
expect(yield* read).toBe(2);
|
||||
registry.set(trigger, 2);
|
||||
expect(yield* read).toBe(2);
|
||||
expect(executions).toBe(2);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("keeps an idle query when its trigger recomputes to the same value", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
|
||||
@@ -74,6 +74,8 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
|
||||
readonly environmentId: EnvironmentIdType;
|
||||
readonly input: Input;
|
||||
}) => Atom.Atom<unknown> | undefined;
|
||||
/** A value that can no longer change. It stops the interval and `refreshTrigger` re-reads. */
|
||||
readonly isFinal?: (value: A) => boolean;
|
||||
}
|
||||
|
||||
interface EnvironmentSubscriptionAtomOptions<Input, A, E, R> {
|
||||
@@ -485,8 +487,8 @@ function runInEnvironment<A, E, R>(
|
||||
* refreshes on its next read.
|
||||
*/
|
||||
const refreshOnSignalWhileRead =
|
||||
(signal: Atom.Atom<unknown>) =>
|
||||
<A>(self: Atom.Atom<A>): Atom.Atom<A> => {
|
||||
<V>(signal: Atom.Atom<unknown>, isFinal: (value: V) => boolean) =>
|
||||
<A extends V>(self: Atom.Atom<A>): Atom.Atom<A> => {
|
||||
// The signal value each registry's data was read under. Held outside the node because the
|
||||
// registry can sweep this node while `self` keeps the data, and the rebuilt node must still
|
||||
// see a change it missed.
|
||||
@@ -499,13 +501,31 @@ const refreshOnSignalWhileRead =
|
||||
const changed =
|
||||
readUnder.has(get.registry) && !Object.is(readUnder.get(get.registry), current);
|
||||
readUnder.set(get.registry, current);
|
||||
if (changed) get.refresh(self);
|
||||
if (changed && !isFinal(get.once(self))) get.refresh(self);
|
||||
return get.once(self);
|
||||
},
|
||||
{ initialValueTarget: self },
|
||||
);
|
||||
};
|
||||
|
||||
/** `Atom.withRefresh`, except the timer stops once the value is final. */
|
||||
const refreshEveryUntilFinal =
|
||||
<V>(intervalMs: number, isFinal: (value: V) => boolean) =>
|
||||
<A extends V>(self: Atom.Atom<A>): Atom.Atom<A> =>
|
||||
Atom.transform(
|
||||
self,
|
||||
(get) => {
|
||||
const value = get(self);
|
||||
if (!isFinal(value)) {
|
||||
// @effect-diagnostics-next-line globalTimers:off - Mirrors `Atom.withRefresh`, outside an Effect runtime.
|
||||
const handle = setTimeout(() => get.refresh(self), intervalMs);
|
||||
get.addFinalizer(() => clearTimeout(handle));
|
||||
}
|
||||
return value;
|
||||
},
|
||||
{ initialValueTarget: self },
|
||||
);
|
||||
|
||||
export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
|
||||
runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, ER>,
|
||||
options: EnvironmentQueryAtomOptions<
|
||||
@@ -626,15 +646,17 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
|
||||
}),
|
||||
Atom.setIdleTTL(idleTtlMs),
|
||||
);
|
||||
const isFinal = (result: AsyncResult.AsyncResult<A, unknown>) =>
|
||||
options.isFinal !== undefined && Option.exists(AsyncResult.value(result), options.isFinal);
|
||||
const intervalQuery =
|
||||
options.refreshIntervalMs === undefined
|
||||
? queryAtom
|
||||
: queryAtom.pipe(Atom.withRefresh(options.refreshIntervalMs));
|
||||
: queryAtom.pipe(refreshEveryUntilFinal(options.refreshIntervalMs, isFinal));
|
||||
const refreshTrigger = options.refreshTrigger?.(target);
|
||||
return (
|
||||
refreshTrigger === undefined
|
||||
? intervalQuery
|
||||
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger))
|
||||
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger, isFinal))
|
||||
).pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(`${options.label}:${key}`));
|
||||
});
|
||||
return (target) => family(environmentRpcKey(target));
|
||||
@@ -716,6 +738,7 @@ export function createEnvironmentRpcQueryAtomFamily<
|
||||
readonly environmentId: EnvironmentIdType;
|
||||
readonly input: EnvironmentRpcInput<TTag>;
|
||||
}) => Atom.Atom<unknown> | undefined;
|
||||
readonly isFinal?: (value: EnvironmentRpcSuccess<TTag>) => boolean;
|
||||
},
|
||||
) {
|
||||
return createEnvironmentQueryAtomFamily(runtime, {
|
||||
@@ -727,6 +750,7 @@ export function createEnvironmentRpcQueryAtomFamily<
|
||||
? {}
|
||||
: { refreshIntervalMs: options.refreshIntervalMs }),
|
||||
...(options.refreshTrigger === undefined ? {} : { refreshTrigger: options.refreshTrigger }),
|
||||
...(options.isFinal === undefined ? {} : { isFinal: options.isFinal }),
|
||||
execute: (
|
||||
input: EnvironmentRpcInput<TTag>,
|
||||
emit: (value: EnvironmentRpcSuccess<TTag>) => Effect.Effect<void>,
|
||||
|
||||
Reference in new issue
Block a user