desktop: Keep inbox statuses up to date

This commit is contained in:
Rūdolfs Ošiņš
2026-11-25 15:56:58 +01:00
parent ed294feb62
commit 7241e790ba
4 changed files with 150 additions and 10 deletions
+24
View File
@@ -0,0 +1,24 @@
import type { NotificationsByRepo } from "@bindings/cob/inbox/NotificationsByRepo";
// The issues and patches in the open inbox, by repository. Updates to them
// refresh the statuses the inbox shows. Undefined while the inbox is closed.
let shown: ReadonlyMap<string, ReadonlySet<string>> | undefined;
export function setInboxItems(repos: NotificationsByRepo[] | undefined): void {
shown =
repos &&
new Map(
repos.map(r => [r.rid, new Set(r.notifications.flat().map(n => n.id))]),
);
}
export function inboxOpen(): boolean {
return shown !== undefined;
}
// Whether the open inbox shows the repository, or with `id`, that issue or
// patch of it.
export function inInbox(rid: string, id?: string): boolean {
const ids = shown?.get(rid);
return ids !== undefined && (id === undefined || ids.has(id));
}
+27 -5
View File
@@ -17,6 +17,7 @@ import { get } from "svelte/store";
import { heardOfChange, heardOfChangeWithin } from "@app/lib/cacheActivity";
import type { RepoList } from "@app/lib/cobQueries";
import { inboxOpen, inInbox } from "@app/lib/inboxRepos";
import { clearEntityCaches, invoke } from "@app/lib/invoke";
import { queryClient } from "@app/lib/query";
import { repoListScope } from "@app/lib/repoListScope";
@@ -87,24 +88,45 @@ export function onRefUpdate(update: RefUpdate) {
queryClient.getQueryData<ActorInfo | null>(keys.actor())?.rid === update.rid
? [keys.actor()]
: []),
// The inbox shows issue and patch statuses from the COB cache. The node
// updates the cache after it writes the notification, so the refresh that
// `notifications_changed` triggers can still show the old status. The
// same key as that refresh merges the two when they fall in one batch.
...(inboxAffected(update.rid, affected, repoWide)
? [keys.notifications()]
: []),
]);
}
function inboxAffected(rid: string, affected: Key[], repoWide: boolean) {
if (repoWide) return inInbox(rid);
return affected.some(
key =>
key.length === 4 &&
(key[2] === "issue" || key[2] === "patch") &&
inInbox(rid, key[3]),
);
}
function refreshCobCache(): void {
clearEntityCaches();
void invalidate(queryClient, {
predicate: query => isCobCacheKey(query.queryKey as Key),
});
// The change doesn't say which repository it was in.
if (inboxOpen()) {
void invalidate(queryClient, { queryKey: keys.notificationList() });
}
}
let deferredCobRefresh: ReturnType<typeof setTimeout> | undefined;
// Issues and patches written without announcing them, such as with
// `rad issue comment --no-announce`, only show in the node's COB cache. It
// also changes with every fetch and write the cache already heard of, and
// says nothing about which repository changed. So right after such a change
// it refreshes once things are quiet, rather than with every fetch, and never
// leaves a change out.
// `rad issue comment --no-announce`, only show in the node's COB cache, and so
// do their statuses in the inbox. The cache also changes with every fetch and
// write we already heard of, and doesn't say which repository changed. So
// after such a change, refresh once things are quiet rather than on every
// fetch, without missing a change.
export function onCobsChanged(): void {
if (!heardOfChangeWithin(COB_QUIET_MS)) {
refreshCobCache();
+11 -5
View File
@@ -6,6 +6,7 @@
import { untrack } from "svelte";
import { nodeEvents } from "@app/lib/events";
import { setInboxItems } from "@app/lib/inboxRepos";
import { invoke } from "@app/lib/invoke";
import { latestOnly } from "@app/lib/latestOnly";
import {
@@ -84,12 +85,17 @@
return notificationsByRepo.reduce((acc, r) => acc + r.count, 0);
}
$effect(() => {
setInboxItems(notificationsByRepo);
return () => setInboxItems(undefined);
});
// Follow notifications that arrive or are cleared elsewhere, such as by the
// node or another instance of the app. The backend says when they change;
// new activity on an issue or patch already in the inbox leaves the count
// as it is.
// The count is refreshed along with the list, since the one this view was
// loaded with may be older than the list.
// node or another instance of the app, and status changes of the issues and
// patches they're about. New activity on an issue or patch already in the
// inbox doesn't change the count.
// Refresh the count with the list, since the count this view loaded with may
// be older than the list.
onInvalidate(
queryClient,
() => keys.notificationList(),
+88
View File
@@ -1,3 +1,4 @@
import type { NotificationsByRepo } from "@bindings/cob/inbox/NotificationsByRepo";
import type { RefUpdate as BackendRefUpdate } from "@bindings/repo/RefUpdate";
import type { RefUpdate } from "@radicle-gui/core/refUpdates";
@@ -12,6 +13,7 @@ import {
vi,
} from "vitest";
import { setInboxItems } from "@app/lib/inboxRepos";
import { queryClient } from "@app/lib/query";
import { repoListScope } from "@app/lib/repoListScope";
import { batch, onCobsChanged, onRefUpdate } from "@app/lib/startup.svelte";
@@ -31,6 +33,25 @@ const invalidateQueries = vi.spyOn(queryClient, "invalidateQueries");
const rid = "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5";
const nid = "z6MkkfM3tPXNPrPevKr3uSiQtHPuwnNhu2yUVjgd2jXVsVz5";
const issueId = "d96f425412c9f8ad5d9a9a05c9831d0728e2338d";
const patchId = "a1b5c9e2f3d4071829a3b4c5d6e7f80912a3b4c5";
// Opens the inbox showing the given issues and patches of each repository.
function openInbox(...repos: [string, string[]][]) {
setInboxItems(
repos.map(
([rid, ids]) =>
({
rid,
notifications: [ids.map(id => ({ id }))],
}) as unknown as NotificationsByRepo,
),
);
}
function invalidatedKeys() {
return invalidateQueries.mock.calls.map(call => call[0]?.queryKey);
}
// Whether an invalidation with the given filter refetches a query of `key`.
function covers(call: unknown[], key: readonly unknown[]): boolean {
@@ -56,6 +77,7 @@ afterEach(() => {
vi.useRealTimers();
invalidateQueries.mockClear();
clearEntityCaches.mockReset();
setInboxItems(undefined);
queryClient.clear();
});
@@ -191,4 +213,70 @@ describe("the COB cache", () => {
vi.advanceTimersByTime(5_000);
expect(cobInvalidations()).toHaveLength(1);
});
test("refreshes the open inbox along with it", () => {
openInbox([rid, [issueId]]);
onCobsChanged();
vi.advanceTimersByTime(5_000);
expect(invalidatedKeys()).toContainEqual(keys.notificationList());
});
test("leaves the inbox while it isn't open", () => {
onCobsChanged();
vi.advanceTimersByTime(5_000);
expect(invalidatedKeys()).not.toContainEqual(keys.notificationList());
});
});
describe("the inbox", () => {
const issue = `refs/namespaces/${nid}/refs/cobs/xyz.radicle.issue/${issueId}`;
const patch = `refs/namespaces/${nid}/refs/cobs/xyz.radicle.patch/${patchId}`;
function inboxInvalidated(): boolean {
vi.advanceTimersByTime(100);
return invalidatedKeys().some(
key => key?.join("\0") === keys.notifications().join("\0"),
);
}
test("refreshes for an update of an issue it shows", () => {
openInbox([rid, [issueId]]);
onRefUpdate({ rid, refs: [issue] });
expect(inboxInvalidated()).toBe(true);
});
test("refreshes for an update of a patch it shows", () => {
openInbox([rid, [patchId]]);
onRefUpdate({ rid, refs: [patch] });
expect(inboxInvalidated()).toBe(true);
});
test("refreshes for an update naming no refs of a repository it shows", () => {
openInbox([rid, [issueId]]);
onRefUpdate({ rid });
expect(inboxInvalidated()).toBe(true);
});
test("stays for an update of an issue it doesn't show", () => {
openInbox([rid, [patchId]]);
onRefUpdate({ rid, refs: [issue] });
expect(inboxInvalidated()).toBe(false);
});
test("stays for a branch update of a repository it shows", () => {
openInbox([rid, [issueId]]);
onRefUpdate({ rid, refs: [`refs/namespaces/${nid}/refs/heads/main`] });
expect(inboxInvalidated()).toBe(false);
});
test("stays for an update naming no refs of a repository it doesn't show", () => {
openInbox(["rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5", [issueId]]);
onRefUpdate({ rid });
expect(inboxInvalidated()).toBe(false);
});
test("stays while it isn't open", () => {
onRefUpdate({ rid, refs: [issue] });
expect(inboxInvalidated()).toBe(false);
});
});