diff --git a/src/components/SidebarRepoList.svelte b/src/components/SidebarRepoList.svelte index 0eee3a680..fb9fa0800 100644 --- a/src/components/SidebarRepoList.svelte +++ b/src/components/SidebarRepoList.svelte @@ -46,7 +46,12 @@ import { nodeRunning } from "@app/lib/events"; import { dynamicInterval, resetDynamicInterval } from "@app/lib/interval"; - import { invoke, writeToClipboard } from "@app/lib/invoke"; + import { + cachedListReposSummary, + invalidateReposSummary, + invoke, + writeToClipboard, + } from "@app/lib/invoke"; import * as router from "@app/lib/router"; import { explorerUrl, formatRepositoryId } from "@app/lib/utils"; @@ -180,8 +185,9 @@ }); async function reloadRepos() { + invalidateReposSummary(); [repos, seededNotReplicated] = await Promise.all([ - invoke("list_repos_summary"), + cachedListReposSummary(), invoke("seeded_not_replicated"), ]); } diff --git a/src/lib/cached.ts b/src/lib/cached.ts index b1950daa9..182f457ba 100644 --- a/src/lib/cached.ts +++ b/src/lib/cached.ts @@ -1,12 +1,18 @@ import { LRUCache } from "lru-cache"; +export interface Cached { + (...args: Args): Promise; + /** Drop all cached entries, forcing the next call to re-fetch. */ + clear(): void; +} + export function cached( f: (...args: Args) => Promise, makeKey: (...args: Args) => string, options?: LRUCache.Options }, unknown>, -): (...args: Args) => Promise { +): Cached { const cache = new LRUCache(options || { max: 500 }); - return function (...args: Args): Promise { + const fn = function (...args: Args): Promise { const key = makeKey(...args); const hit = cache.get(key); if (hit !== undefined) return hit.promise; @@ -18,5 +24,7 @@ export function cached( }); cache.set(key, { promise }); return promise; - }; + } as Cached; + fn.clear = () => cache.clear(); + return fn; } diff --git a/src/lib/invoke.ts b/src/lib/invoke.ts index 82e3287ec..d6f6ca9cc 100644 --- a/src/lib/invoke.ts +++ b/src/lib/invoke.ts @@ -2,6 +2,7 @@ import type { DiffOptions } from "@bindings/cob/DiffOptions"; import type { Diff } from "@bindings/diff/Diff"; import type { Stats } from "@bindings/diff/Stats"; import type { Commit } from "@bindings/repo/Commit"; +import type { RepoSummary } from "@bindings/repo/RepoSummary"; import * as tauri from "@tauri-apps/api/core"; @@ -154,6 +155,22 @@ export const cachedGetCommitDiff = cached( { max: 100 }, ); +/** + * Enumerating repos loads and verifies the identity doc of every repo in + * storage, which takes ~100ms with many repos. The list rarely changes, so it + * is cached with a short TTL; mutations that change it (adding, seeding, + * unseeding) go through invalidateReposSummary(). + */ +export const cachedListReposSummary = cached( + () => invoke("list_repos_summary"), + () => "list_repos_summary", + { max: 1, ttl: 30_000 }, +); + +export function invalidateReposSummary(): void { + cachedListReposSummary.clear(); +} + export async function writeToClipboard( text: string, opts?: { diff --git a/src/lib/router/definitions.ts b/src/lib/router/definitions.ts index 9f9ba74db..4686a3127 100644 --- a/src/lib/router/definitions.ts +++ b/src/lib/router/definitions.ts @@ -13,7 +13,7 @@ import { loadRepoHome, } from "@app/views/repo/router"; -import { invoke } from "@app/lib/invoke"; +import { cachedListReposSummary, invoke } from "@app/lib/invoke"; interface BootingRoute { resource: "booting"; @@ -72,7 +72,7 @@ export async function loadSidebarData(): Promise { const [config, repos, notificationCount, seededNotReplicated] = await Promise.all([ invoke("config"), - invoke("list_repos_summary"), + cachedListReposSummary(), invoke("notification_count"), invoke("seeded_not_replicated"), ]);