diff --git a/desktop/src/lib/invoke.ts b/desktop/src/lib/invoke.ts index 7e590baf5..d14e599d4 100644 --- a/desktop/src/lib/invoke.ts +++ b/desktop/src/lib/invoke.ts @@ -16,6 +16,7 @@ import type { RepoSummary } from "@bindings/repo/RepoSummary"; import type { TreePath } from "@bindings/repo/TreePath"; import { cached } from "@radicle-gui/ui/lib/cached"; +import { invalidate } from "@radicle-gui/ui/lib/query.svelte"; import * as tauri from "@tauri-apps/api/core"; import { labs } from "@app/lib/labs"; @@ -35,8 +36,10 @@ export async function invoke( 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 }); + const written = writtenKeys(cmd, args, result); + if (written.length > 0) clearEntityCaches(); + for (const queryKey of written) { + void invalidate(queryClient, { queryKey }); } } } @@ -310,6 +313,20 @@ export const cachedReleaseById = cached( missTtl, ); +// The caches above hold data that changes: references, hover cards and +// mention candidates. They are dropped whenever data changes, so that they +// show the same as the views around them. +export function clearEntityCaches(): void { + cachedListIssueCandidates.clear(); + cachedListPatchCandidates.clear(); + cachedListReleaseCandidates.clear(); + cachedRepoById.clear(); + cachedIssueById.clear(); + cachedPatchById.clear(); + cachedReleaseById.clear(); + cachedRepoSplitTreePath.clear(); +} + async function repoCommit(rid: string, sha: string): Promise { return withTestBackend(tauri.invoke, "repo_commit", { rid, sha }); } diff --git a/desktop/src/lib/startup.svelte.ts b/desktop/src/lib/startup.svelte.ts index c18e3a4c7..48d9ac81d 100644 --- a/desktop/src/lib/startup.svelte.ts +++ b/desktop/src/lib/startup.svelte.ts @@ -8,7 +8,7 @@ import { listen } from "@tauri-apps/api/event"; import once from "lodash/once"; import { get } from "svelte/store"; -import { invoke } from "@app/lib/invoke"; +import { clearEntityCaches, invoke } from "@app/lib/invoke"; import { queryClient } from "@app/lib/query"; import { fetchingRepos } from "@app/lib/seedRepo"; @@ -26,6 +26,7 @@ export function batch(delay: number, jobs: boolean): (affected: Key[]) => void { flush = undefined; const queryKeys = [...pending.values()]; pending.clear(); + if (!jobs) clearEntityCaches(); for (const queryKey of queryKeys) { void queryClient.invalidateQueries({ queryKey, @@ -105,6 +106,7 @@ export const createEventEmittersOnce = once(async (): Promise => { }), // The backend may have missed events while it wasn't subscribed. listen("ref_resync", () => { + clearEntityCaches(); void queryClient.invalidateQueries(); }), ]); @@ -114,6 +116,7 @@ export const createEventEmittersOnce = once(async (): Promise => { // have changed. const onFocus = () => { if (!get(nodeEvents)) { + clearEntityCaches(); void queryClient.invalidateQueries({ predicate: query => !isJobs(query.queryKey as Key), }); diff --git a/desktop/tests/unit/eventBatch.test.ts b/desktop/tests/unit/eventBatch.test.ts index 21e98cc53..5aefc0d25 100644 --- a/desktop/tests/unit/eventBatch.test.ts +++ b/desktop/tests/unit/eventBatch.test.ts @@ -5,11 +5,12 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; import { batch, onRefUpdate } from "@app/lib/startup.svelte"; -const { invalidateQueries } = vi.hoisted(() => ({ +const { invalidateQueries, clearEntityCaches } = vi.hoisted(() => ({ invalidateQueries: vi.fn(), + clearEntityCaches: vi.fn(), })); vi.mock("@app/lib/query", () => ({ queryClient: { invalidateQueries } })); -vi.mock("@app/lib/invoke", () => ({ invoke: vi.fn() })); +vi.mock("@app/lib/invoke", () => ({ clearEntityCaches, invoke: vi.fn() })); vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() })); vi.mock("@app/lib/seedRepo", async () => { const { writable } = await import("svelte/store"); @@ -41,6 +42,7 @@ beforeEach(() => { afterEach(() => { vi.useRealTimers(); invalidateQueries.mockReset(); + clearEntityCaches.mockReset(); }); describe("batch", () => { @@ -56,6 +58,7 @@ describe("batch", () => { keys.issues(rid), keys.patches(rid), ]); + expect(clearEntityCaches).toHaveBeenCalledTimes(1); }); test("leaves job lists under a repository key to their own batch", () => { @@ -77,6 +80,7 @@ describe("batch", () => { const [call] = invalidateQueries.mock.calls; expect(covers(call, keys.jobs(rid))).toBe(true); + expect(clearEntityCaches).not.toHaveBeenCalled(); }); test("an update naming no refs refreshes job lists in their own batch", () => {