mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 14:20:42 +08:00
Add shared core package and query cache
Core holds the keys of the data both apps read and maps a node's ref updates to the keys they affect. The query cache wraps TanStack Query for runes, as decided in ADRs 4 and 5.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Keys of the data that shared code reads, by domain identity. Every key of a
|
||||
// repository starts with `["repo", rid]`, so that prefix covers all of it.
|
||||
export type Key = readonly string[];
|
||||
|
||||
export const keys = {
|
||||
repo: (rid: string) => ["repo", rid] as const,
|
||||
repoInfo: (rid: string) => ["repo", rid, "info"] as const,
|
||||
code: (rid: string) => ["repo", rid, "code"] as const,
|
||||
issues: (rid: string) => ["repo", rid, "issues"] as const,
|
||||
issue: (rid: string, id: string) => ["repo", rid, "issue", id] as const,
|
||||
patches: (rid: string) => ["repo", rid, "patches"] as const,
|
||||
patch: (rid: string, id: string) => ["repo", rid, "patch", id] as const,
|
||||
};
|
||||
|
||||
// Lists of CI jobs, which scanning every job COB of a repository builds.
|
||||
export function isJobsKey(key: Key): boolean {
|
||||
return key[0] === "repo" && key[2] === "jobs";
|
||||
}
|
||||
|
||||
export function isRepoInfoKey(key: Key): boolean {
|
||||
return key[0] === "repo" && key[2] === "info";
|
||||
}
|
||||
|
||||
// Data the node's COB cache holds: issues, patches and the counts in the
|
||||
// repository info.
|
||||
export function isCobCacheKey(key: Key): boolean {
|
||||
return (
|
||||
key[0] === "repo" &&
|
||||
["info", "issues", "issue", "patches", "patch"].includes(key[2])
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { Key } from "./keys";
|
||||
|
||||
import { keys } from "./keys";
|
||||
|
||||
// Refs that changed in a repository. Without `refs`, the backend doesn't know
|
||||
// which refs changed, such as when the local user pushed.
|
||||
export interface RefUpdate {
|
||||
rid: string;
|
||||
refs?: string[];
|
||||
}
|
||||
|
||||
const ISSUE = "xyz.radicle.issue";
|
||||
const PATCH = "xyz.radicle.patch";
|
||||
|
||||
export function affectedKeys({ rid, refs }: RefUpdate): Key[] {
|
||||
if (refs === undefined) {
|
||||
return [keys.repo(rid)];
|
||||
}
|
||||
|
||||
const affected = new Map<string, Key>();
|
||||
const add = (key: Key) => affected.set(key.join("\0"), key);
|
||||
|
||||
for (const ref of refs) {
|
||||
const name = withoutNamespace(ref);
|
||||
const cob = name.match(/^refs\/cobs\/([^/]+)\/([^/]+)$/);
|
||||
|
||||
if (cob && cob[1] === ISSUE) {
|
||||
add(keys.issue(rid, cob[2]));
|
||||
add(keys.issues(rid));
|
||||
add(keys.repoInfo(rid));
|
||||
} else if (cob && cob[1] === PATCH) {
|
||||
add(keys.patch(rid, cob[2]));
|
||||
add(keys.patches(rid));
|
||||
add(keys.repoInfo(rid));
|
||||
} else if (/^refs\/(heads|tags)\//.test(name)) {
|
||||
add(keys.code(rid));
|
||||
add(keys.repoInfo(rid));
|
||||
} else if (name === "refs/rad/sigrefs") {
|
||||
// Signed refs change along with the refs they sign.
|
||||
} else {
|
||||
// Identity documents and other COB types can affect anything shown.
|
||||
return [keys.repo(rid)];
|
||||
}
|
||||
}
|
||||
|
||||
return [...affected.values()];
|
||||
}
|
||||
|
||||
function withoutNamespace(ref: string): string {
|
||||
return ref.replace(/^refs\/namespaces\/[^/]+\//, "");
|
||||
}
|
||||
Reference in New Issue
Block a user