mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 14:20:42 +08:00
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.
127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
import type { Key } from "@radicle-gui/core/keys";
|
|
import type { RefUpdate } from "@radicle-gui/core/refUpdates";
|
|
import type { UnlistenFn } from "@tauri-apps/api/event";
|
|
|
|
import { keys } from "@radicle-gui/core/keys";
|
|
import { affectedKeys } from "@radicle-gui/core/refUpdates";
|
|
import { listen } from "@tauri-apps/api/event";
|
|
import once from "lodash/once";
|
|
import { get } from "svelte/store";
|
|
|
|
import { invoke } from "@app/lib/invoke";
|
|
import { queryClient } from "@app/lib/query";
|
|
import { fetchingRepos } from "@app/lib/seedRepo";
|
|
|
|
import { artifactNodeRunning, nodeEvents, nodeRunning } from "./events";
|
|
|
|
// 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;
|
|
return affected => {
|
|
for (const key of affected) pending.set(key.join("\0"), key);
|
|
flush ??= setTimeout(() => {
|
|
flush = undefined;
|
|
const queryKeys = [...pending.values()];
|
|
pending.clear();
|
|
for (const queryKey of queryKeys) {
|
|
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, 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 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([
|
|
...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
|
|
// identity update renames one.
|
|
...(get(fetchingRepos).includes(update.rid) ||
|
|
update.refs?.some(ref => ref.endsWith("refs/rad/id"))
|
|
? [keys.repos()]
|
|
: []),
|
|
]);
|
|
}
|
|
|
|
export let unlistenNodeEvents: UnlistenFn | undefined = undefined;
|
|
|
|
export function setUnlistenNodeEvents(unlisten: UnlistenFn) {
|
|
unlistenNodeEvents = unlisten;
|
|
}
|
|
|
|
// Will be called once in the startup of the app
|
|
export const createEventEmittersOnce = once(async (): Promise<UnlistenFn> => {
|
|
if (!window.__TAURI_INTERNALS__) {
|
|
// The test backend can't emit events, so poll at the backend's pace.
|
|
const pollNodes = () => {
|
|
invoke<boolean>("node_running")
|
|
.then(running => nodeRunning.set(running))
|
|
.catch(console.error);
|
|
invoke<boolean>("artifact_node_running")
|
|
.then(running => artifactNodeRunning.set(running))
|
|
.catch(console.error);
|
|
};
|
|
pollNodes();
|
|
const interval = setInterval(pollNodes, 2_000);
|
|
return () => clearInterval(interval);
|
|
}
|
|
|
|
const unlisteners = await Promise.all([
|
|
listen<boolean>("node_running", event => {
|
|
nodeRunning.set(event.payload);
|
|
}),
|
|
listen<boolean>("artifact_node_running", event => {
|
|
artifactNodeRunning.set(event.payload);
|
|
}),
|
|
listen<boolean>("node_events", event => {
|
|
nodeEvents.set(event.payload);
|
|
}),
|
|
listen<RefUpdate>("ref_update", event => onRefUpdate(event.payload)),
|
|
// The backend may have missed events while it wasn't subscribed.
|
|
listen("ref_resync", () => {
|
|
void queryClient.invalidateQueries();
|
|
}),
|
|
]);
|
|
|
|
// Without the node's events, refetch what is shown whenever the user comes
|
|
// 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({
|
|
predicate: query => !isJobs(query.queryKey as Key),
|
|
});
|
|
}
|
|
};
|
|
window.addEventListener("focus", onFocus);
|
|
|
|
return () => {
|
|
unlisteners.forEach(unlisten => unlisten());
|
|
window.removeEventListener("focus", onFocus);
|
|
};
|
|
});
|