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:
Rūdolfs Ošiņš
2026-11-20 19:32:16 +01:00
parent 5937288817
commit ee80f7d6f4
19 changed files with 915 additions and 3 deletions
+4
View File
@@ -0,0 +1,4 @@
{
"arrowParens": "avoid",
"proseWrap": "never"
}
+31
View File
@@ -0,0 +1,31 @@
# @radicle-gui/core
Domain logic shared by explorer, desktop and `@radicle-gui/ui`, as plain
TypeScript: types and pure functions, such as the query keys in
`src/keys.ts` and the keys a ref update affects in `src/refUpdates.ts`. See
`docs/adr/0004-shared-core.md` and `docs/adr/0005-reactivity.md` at the
repository root.
The package lives in `shared/core/` of the radicle-gui monorepo, in the root
npm workspace: install dependencies at the repository root with `npm ci`, and
run every other command here from `shared/core/`. Conventions shared with
both apps are in the root `AGENTS.md`.
## Rules
- No Svelte, DOM, Tauri or network dependencies, and no imports from either
app or from `@radicle-gui/ui`. The tsconfig leaves out DOM and Node types,
and eslint rejects the imports
- `@radicle-gui/ui` and the apps depend on core, never the other way round
- The apps import it as `@radicle-gui/core/<module>`
## Checks and tests
```sh
npm run check # tsc, eslint, prettier
npm run format # auto-fix prettier and eslint issues
npm run test:unit
```
CI runs these in `.github/workflows/shared-core.yml`. A change here affects
`shared/ui` and both apps, so also run their checks.
+38
View File
@@ -0,0 +1,38 @@
import js from "@eslint/js";
import ts from "typescript-eslint";
// Core is plain domain logic that both apps and shared/ui build on. Its
// tsconfig leaves out DOM and Node types; this keeps out the packages that
// would tie it to a framework, an app or a transport.
export default [
js.configs.recommended,
...ts.configs.recommended,
{
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["svelte", "svelte/*"],
message: "Core has no Svelte dependencies.",
},
{
group: [
"@tauri-apps/*",
"@bindings/*",
"@app/*",
"@http-client/*",
],
message: "Core doesn't depend on either app.",
},
{
group: ["@radicle-gui/ui", "@radicle-gui/ui/*"],
message: "shared/ui depends on core, never the other way round.",
},
],
},
],
},
},
];
+22
View File
@@ -0,0 +1,22 @@
{
"name": "@radicle-gui/core",
"private": true,
"version": "0.0.0",
"type": "module",
"exports": {
"./*": "./src/*.ts"
},
"scripts": {
"check": "tsc --noEmit && eslint --max-warnings 0 . && prettier '**/*.@(ts|js|json)' --check",
"format": "prettier '**/*.@(ts|js|json)' --write && eslint --fix .",
"test:unit": "TZ='UTC' vitest run"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"eslint": "^10.7.0",
"prettier": "^3.9.5",
"typescript": "^6.0.3",
"typescript-eslint": "^8.64.0",
"vitest": "^4.1.12"
}
}
+31
View File
@@ -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])
);
}
+51
View File
@@ -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\/[^/]+\//, "");
}
+50
View File
@@ -0,0 +1,50 @@
import { describe, expect, test } from "vitest";
import { keys } from "../src/keys";
import { affectedKeys } from "../src/refUpdates";
const rid = "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5";
const nid = "z6MkkfM3tPXNPrPevKr3uSiQtHPuwnNhu2yUVjgd2jXVsVz5";
const issue = "d87dcfe8c2b3200e78b128d9b959cfdf7063fefe";
describe("affectedKeys", () => {
test("an update without refs affects the whole repository", () => {
expect(affectedKeys({ rid })).toEqual([keys.repo(rid)]);
});
test("an issue affects itself, the issue list and the repository info", () => {
expect(
affectedKeys({
rid,
refs: [
`refs/namespaces/${nid}/refs/cobs/xyz.radicle.issue/${issue}`,
`refs/namespaces/${nid}/refs/rad/sigrefs`,
],
}),
).toEqual([keys.issue(rid, issue), keys.issues(rid), keys.repoInfo(rid)]);
});
test("branches affect the code and the repository info once", () => {
expect(
affectedKeys({
rid,
refs: [
`refs/namespaces/${nid}/refs/heads/main`,
`refs/namespaces/${nid}/refs/tags/v1.0.0`,
],
}),
).toEqual([keys.code(rid), keys.repoInfo(rid)]);
});
test("an identity update affects the whole repository", () => {
expect(
affectedKeys({
rid,
refs: [
`refs/namespaces/${nid}/refs/cobs/xyz.radicle.patch/${issue}`,
`refs/namespaces/${nid}/refs/rad/id`,
],
}),
).toEqual([keys.repo(rid)]);
});
});
+16
View File
@@ -0,0 +1,16 @@
{
"include": ["src", "tests"],
"compilerOptions": {
"noEmit": true,
"target": "es2024",
"lib": ["es2024"],
"types": [],
"module": "es2022",
"moduleResolution": "bundler",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
}
}
+8
View File
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
include: ["tests/**/*.test.ts"],
reporters: "verbose",
},
});