mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 14:20:42 +08:00
desktop: Refresh CI status on job updates
Every status chip of a repository shares one list of its jobs, which refreshes when a job changes, at most every few seconds, since listing them scans every job of the repository.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
<script lang="ts">
|
||||
import type { Job } from "@bindings/repo/Job";
|
||||
|
||||
import { keys } from "@radicle-gui/core/keys";
|
||||
import { createQuery } from "@radicle-gui/ui/lib/query.svelte";
|
||||
import { SvelteSet } from "svelte/reactivity";
|
||||
|
||||
import type { RunView, Status } from "@app/lib/ciJobs";
|
||||
@@ -8,7 +12,9 @@
|
||||
statusLabel,
|
||||
totalCounts,
|
||||
} from "@app/lib/ciJobs";
|
||||
import { cachedListJobs } from "@app/lib/invoke";
|
||||
import { nodeEvents } from "@app/lib/events";
|
||||
import { listJobs } from "@app/lib/invoke";
|
||||
import { queryClient } from "@app/lib/query";
|
||||
import { authorForNodeId } from "@app/lib/utils";
|
||||
|
||||
import Button from "@app/components/Button.svelte";
|
||||
@@ -28,9 +34,19 @@
|
||||
const collapsed = new SvelteSet<string>();
|
||||
let popoverExpanded: boolean = $state(false);
|
||||
|
||||
// Held in a `$derived`; see `ReviewCodeThread`. The TTL here means a re-run
|
||||
// would refetch and blank the chip.
|
||||
const jobsPromise = $derived(cachedListJobs(rid, commit).catch(() => []));
|
||||
// Listing a commit's jobs scans every job of the repository, so the cache
|
||||
// keeps one list of them all, shared by every chip of the repository and
|
||||
// kept across navigations. Job updates from the node refresh it; without
|
||||
// them, it is refetched on mount once it is 30 seconds old.
|
||||
const jobsQuery = createQuery<Job[]>(queryClient, () => ({
|
||||
queryKey: keys.jobs(rid),
|
||||
queryFn: () => listJobs(rid),
|
||||
staleTime: $nodeEvents ? Infinity : 30_000,
|
||||
}));
|
||||
const loadedJobs = $derived(
|
||||
jobsQuery.current.data?.filter(job => job.commit === commit) ??
|
||||
(jobsQuery.current.isError ? [] : undefined),
|
||||
);
|
||||
|
||||
function toggleNode(key: string) {
|
||||
if (collapsed.has(key)) {
|
||||
@@ -145,7 +161,8 @@
|
||||
}
|
||||
</style>
|
||||
|
||||
{#await jobsPromise then jobs}
|
||||
{#if loadedJobs}
|
||||
{@const jobs = loadedJobs}
|
||||
{#if jobs.length > 0}
|
||||
{@const groups = groupJobs(jobs)}
|
||||
{@const overallCounts = totalCounts(groups)}
|
||||
@@ -262,4 +279,4 @@
|
||||
{/snippet}
|
||||
</Popover>
|
||||
{/if}
|
||||
{/await}
|
||||
{/if}
|
||||
|
||||
@@ -196,19 +196,11 @@ export const cachedRepoCommitCount = cached(
|
||||
{ max: 5_000 },
|
||||
);
|
||||
|
||||
async function listJobs(rid: string, sha: string): Promise<Job[]> {
|
||||
return withTestBackend(tauri.invoke, "list_jobs", { rid, sha });
|
||||
}
|
||||
|
||||
// Listing jobs scans every job COB in the repo, so it is slow (hundreds of ms
|
||||
// to seconds). It is re-requested on every JobCob mount; caching by (rid, sha)
|
||||
// collapses concurrent identical calls into one in-flight request and reuses
|
||||
// the result across navigations. A short TTL keeps CI status reasonably fresh.
|
||||
export const cachedListJobs = cached(
|
||||
listJobs,
|
||||
(...[rid, sha]) => `list_jobs:${rid}:${sha}`,
|
||||
{ max: 200, ttl: 30_000 },
|
||||
);
|
||||
// to seconds).
|
||||
export async function listJobs(rid: string): Promise<Job[]> {
|
||||
return withTestBackend(tauri.invoke, "list_jobs", { rid });
|
||||
}
|
||||
|
||||
async function alias(nid: string): Promise<string | null> {
|
||||
return withTestBackend(tauri.invoke, "alias", { nid });
|
||||
|
||||
@@ -14,8 +14,9 @@ import { fetchingRepos } from "@app/lib/seedRepo";
|
||||
|
||||
import { artifactNodeRunning, nodeEvents, nodeRunning } from "./events";
|
||||
|
||||
// Collects keys and invalidates them together after `delay`.
|
||||
function batch(delay: number): (affected: Key[]) => void {
|
||||
// Collects keys and invalidates them together after `delay`. Job lists under
|
||||
// a key are left to their own batch unless `jobs` says the keys are theirs.
|
||||
export function batch(delay: number, jobs: boolean): (affected: Key[]) => void {
|
||||
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- never rendered
|
||||
const pending = new Map<string, Key>();
|
||||
let flush: ReturnType<typeof setTimeout> | undefined;
|
||||
@@ -26,18 +27,35 @@ function batch(delay: number): (affected: Key[]) => void {
|
||||
const queryKeys = [...pending.values()];
|
||||
pending.clear();
|
||||
for (const queryKey of queryKeys) {
|
||||
void queryClient.invalidateQueries({ queryKey });
|
||||
void queryClient.invalidateQueries({
|
||||
queryKey,
|
||||
predicate: query => jobs || !isJobs(query.queryKey as Key),
|
||||
});
|
||||
}
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
|
||||
// A fetch often updates many refs in a burst, so invalidations are batched.
|
||||
const invalidate = batch(100);
|
||||
const invalidate = batch(100, false);
|
||||
// Listing CI jobs scans every job in the repository, and a running
|
||||
// job updates often, so job lists refresh at most every few seconds.
|
||||
const invalidateJobs = batch(5_000, true);
|
||||
|
||||
function onRefUpdate(update: RefUpdate) {
|
||||
function isJobs(key: Key): boolean {
|
||||
return key[0] === "repo" && key[2] === "jobs";
|
||||
}
|
||||
|
||||
export function onRefUpdate(update: RefUpdate) {
|
||||
const affected = affectedKeys(update);
|
||||
// A key for the whole repository covers its jobs too.
|
||||
const repoWide = affected.some(key => key.length === 2 && key[0] === "repo");
|
||||
invalidateJobs([
|
||||
...affected.filter(isJobs),
|
||||
...(repoWide ? [keys.jobs(update.rid)] : []),
|
||||
]);
|
||||
invalidate([
|
||||
...affectedKeys(update),
|
||||
...affected.filter(key => !isJobs(key)),
|
||||
// The node writes notifications as it fetches.
|
||||
keys.notificationCount(),
|
||||
// The repository list changes when a seeded repository arrives, or when an
|
||||
@@ -90,10 +108,13 @@ export const createEventEmittersOnce = once(async (): Promise<UnlistenFn> => {
|
||||
]);
|
||||
|
||||
// Without the node's events, refetch what is shown whenever the user comes
|
||||
// back to the window.
|
||||
// back to the window. CI jobs only arrive through the node, so they can't
|
||||
// have changed.
|
||||
const onFocus = () => {
|
||||
if (!get(nodeEvents)) {
|
||||
void queryClient.invalidateQueries();
|
||||
void queryClient.invalidateQueries({
|
||||
predicate: query => !isJobs(query.queryKey as Key),
|
||||
});
|
||||
}
|
||||
};
|
||||
window.addEventListener("focus", onFocus);
|
||||
|
||||
Reference in New Issue
Block a user