desktop: Refresh the cache after the user's own writes

Reviews and writes with announcing off or the node stopped never reach the
node's events, so each write command now invalidates the data it changes,
also when it fails partway. Views that pin data join that refetch instead
of starting another.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-20 19:32:17 +01:00
parent 94ac124c62
commit 748f9e9da7
13 changed files with 559 additions and 46 deletions
+10 -21
View File
@@ -15,7 +15,6 @@ import type { RepoInfo } from "@bindings/repo/RepoInfo";
import type { RepoSummary } from "@bindings/repo/RepoSummary";
import type { TreePath } from "@bindings/repo/TreePath";
import { keys } from "@radicle-gui/core/keys";
import { cached } from "@radicle-gui/ui/lib/cached";
import * as tauri from "@tauri-apps/api/core";
@@ -23,33 +22,23 @@ import { labs } from "@app/lib/labs";
import { projectsOf } from "@app/lib/projects";
import { queryClient } from "@app/lib/query";
import type { RepoListScope } from "@app/lib/repoListScope";
// Commands that change a repository's info: its issue and patch counts, and
// whether it is seeded. The node doesn't announce the user's own writes when
// announcing is off, so the cache learns of them here.
const countWrites = new Set([
"create_issue",
"edit_issue",
"delete_issue",
"edit_patch",
"delete_patch",
"rebuild_issue_cache",
"rebuild_patch_cache",
"seed",
"unseed",
]);
import { writtenKeys } from "@app/lib/writes";
export async function invoke<T = null>(
cmd: string,
args?: tauri.InvokeArgs,
options?: tauri.InvokeOptions,
): Promise<T> {
const result = await invokeCommand<T>(cmd, args, options);
const rid = (args as { rid?: unknown } | undefined)?.rid;
if (countWrites.has(cmd) && typeof rid === "string") {
void queryClient.invalidateQueries({ queryKey: keys.repoInfo(rid) });
let result: T | undefined;
try {
result = await invokeCommand<T>(cmd, args, options);
return result;
} finally {
// Also after a failure: a command can fail after writing part of a change.
for (const queryKey of writtenKeys(cmd, args, result)) {
void queryClient.invalidateQueries({ queryKey });
}
}
return result;
}
async function invokeCommand<T>(
+86
View File
@@ -0,0 +1,86 @@
import type { Key } from "@radicle-gui/core/keys";
import { keys } from "@radicle-gui/core/keys";
import { affectedKeys, cobTypes } from "@radicle-gui/core/refUpdates";
const { issue: ISSUE, patch: PATCH } = cobTypes;
interface WriteArgs {
rid?: unknown;
cobId?: unknown;
new?: { id?: unknown };
args?: { rid?: unknown };
}
// Write commands whose effect on the cache is mapped below. Other writes change
// data the cache doesn't hold.
export const mappedWrites = [
"create_issue",
"edit_issue",
"delete_issue",
"create_issue_comment",
"edit_patch",
"delete_patch",
"create_patch_comment",
"create_patch_review",
"rebuild_issue_cache",
"rebuild_patch_cache",
"seed",
"unseed",
"clean",
];
// The keys a command's write affects, so that the user's own changes show
// whether or not the node announces them: it doesn't with announcing off, for
// reviews, or while it is stopped. Views that apply pinned state still wait
// for their own refetch.
export function writtenKeys(
cmd: string,
args: unknown,
result: unknown,
): Key[] {
const { rid, cobId, new: created, args: nested } = (args ?? {}) as WriteArgs;
const cob = (type: string, id: unknown) =>
typeof rid === "string" && typeof id === "string"
? affectedKeys({ rid, refs: [`refs/cobs/${type}/${id}`] })
: [];
switch (cmd) {
case "create_issue":
return cob(ISSUE, (result as { id?: unknown } | undefined)?.id);
case "edit_issue":
case "delete_issue":
return cob(ISSUE, cobId);
case "create_issue_comment":
return cob(ISSUE, created?.id);
case "edit_patch":
case "delete_patch":
return cob(PATCH, cobId);
case "create_patch_comment":
return cob(PATCH, created?.id);
// A review names its revision, not its patch, so every patch of the
// repository is refetched while shown.
case "create_patch_review":
return typeof nested?.rid === "string"
? [keys.patchDetails(nested.rid), keys.patches(nested.rid)]
: [];
case "rebuild_issue_cache":
return typeof rid === "string"
? [keys.issues(rid), keys.repoInfo(rid)]
: [];
case "rebuild_patch_cache":
return typeof rid === "string"
? [keys.patches(rid), keys.repoInfo(rid)]
: [];
// The repository info says whether it is seeded, and the repository list
// shows the seeded ones.
case "seed":
case "unseed":
return typeof rid === "string" ? [keys.repoInfo(rid), keys.repos()] : [];
// Cleaning removes other peers' refs, which can change anything shown.
case "clean":
return typeof rid === "string" ? [keys.repo(rid), keys.repos()] : [];
default:
return [];
}
}
+15 -9
View File
@@ -109,13 +109,18 @@
const activityItems = $derived(issueTimeline(issue, activity));
// After the user's own changes, refetch and show them right away.
async function reload() {
await queryClient.invalidateQueries({
queryKey: query.queryKey,
refetchType: "all",
});
description.apply();
// After the user's own changes, refetch and show them right away. Only a
// change to the description itself shows its latest edit, so that acting on
// the rest of the issue doesn't swap the text the user is reading.
async function reload(commentId?: string) {
// Joins the refetch `invoke()` started after the write, if any.
await queryClient.invalidateQueries(
{ queryKey: query.queryKey, refetchType: "all" },
{ cancelRefetch: false },
);
if (commentId !== undefined && commentId === issue.body?.id) {
description.apply();
}
}
async function createComment(
@@ -152,7 +157,7 @@
} catch (error) {
console.error("Issue comment editing failed: ", error);
} finally {
await reload();
await reload(id);
}
}
@@ -218,11 +223,12 @@
} catch (error) {
console.error("Editing reactions failed", error);
} finally {
await reload();
await reload(commentId);
}
}
async function deleteIssue() {
deleting = true;
try {
await invoke("delete_issue", {
rid: repo.rid,
+13 -7
View File
@@ -113,9 +113,11 @@
: loadedRevisions,
);
// Deleted elsewhere while shown, so there is nothing left to show.
// Deleted elsewhere while shown, so there is nothing left to show. Deleting
// it here navigates by itself.
let deleting = false;
$effect(() => {
if (detail.current.data?.patch === null) {
if (detail.current.data && !detail.current.data.patch && !deleting) {
void router.push({ resource: "repo.patches", rid: repo.rid, status });
}
});
@@ -528,10 +530,11 @@
// After the user's own changes, refetch and show them right away.
async function loadPatch() {
await queryClient.invalidateQueries({
queryKey: query.queryKey,
refetchType: "all",
});
// Joins the refetch `invoke()` started after the write, if any.
await queryClient.invalidateQueries(
{ queryKey: query.queryKey, refetchType: "all" },
{ cancelRefetch: false },
);
}
// A draft review belongs to a single revision, mirroring the protocol's
@@ -607,19 +610,22 @@
});
async function deletePatch() {
deleting = true;
try {
await invoke("delete_patch", {
rid: repo.rid,
cobId: patch.id,
opts: { announce: $nodeRunning && $announce },
});
void router.push({
await router.push({
resource: "repo.patches",
rid: repo.rid,
status: patch.state.status,
});
} catch (error) {
console.error("Deleting patch failed", error);
} finally {
deleting = false;
}
}
</script>