From 4d2299df41d4898514439fa4a3e224e8eb0f5e41 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 29 Mar 2025 15:48:49 +0100 Subject: [PATCH] Enhance page list caching logic in Client class - Introduced checks for initial sync completion before updating the page list cache. - Updated logic to fetch pages based on sync status and index plug availability. - Added tagging for meta pages starting with "Library/" during direct fetch. - Initialized empty tags array for file metadata in fileMetaToPageMeta function. --- common/space.ts | 1 + web/client.ts | 87 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/common/space.ts b/common/space.ts index 121e594a..3b6c1b25 100644 --- a/common/space.ts +++ b/common/space.ts @@ -195,6 +195,7 @@ export function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { ...fileMeta, ref: name, tag: "page", + tags: [], name, created: localDateString(new Date(fileMeta.created)), lastModified: localDateString(new Date(fileMeta.lastModified)), diff --git a/web/client.ts b/web/client.ts index 30b895ab..40e24920 100644 --- a/web/client.ts +++ b/web/client.ts @@ -738,31 +738,76 @@ export class Client { async updatePageListCache() { console.log("Updating page list cache"); - if (!this.clientSystem.system.loadedPlugs.has("index")) { - console.warn("Index plug not loaded, cannot update page list cache"); - return; + // **** Check if the initial sync has been completed **** + const initialSyncCompleted = await this.hasInitialSyncCompleted(); + + let allPages: PageMeta[] = []; + + if ( + initialSyncCompleted && this.clientSystem.system.loadedPlugs.has("index") + ) { + // **** Logic when initial sync IS completed AND index plug is loaded **** + console.log( + "Initial sync complete and index plug loaded, loading full page list via index.", + ); + // Fetch actual indexed pages + allPages = await this.clientSystem.queryLuaObjects( + "page", + {}, + ); + // Fetch aspiring pages only when using the index + const aspiringPageNames = await this.clientSystem.queryLuaObjects< + string + >( + "aspiring-page", + { + select: parseExpressionString("name"), + distinct: true, + }, + ); + // Map and push aspiring pages directly into allPages + allPages.push( + ...aspiringPageNames.map((name): PageMeta => ({ + ref: name, + tag: "page", + _isAspiring: true, + name: name, + created: "", // Aspiring pages don't have timestamps yet + lastModified: "", // Aspiring pages don't have timestamps yet + perm: "rw", + })), + ); + } else { + // **** Logic when initial sync is NOT YET completed OR index plug isn't ready **** + // Use space.fetchPageList() directly + console.log( + "Initial sync not complete or index plug not loaded. Fetching page list directly using space.fetchPageList().", + ); + try { + // Call fetchPageList directly + allPages = await this.space.fetchPageList(); + + // Let's do some heuristic based post processing + for (const page of allPages) { + // These are _mostly_ meta pages, let's add a tag for them + if (page.name.startsWith("Library/")) { + page.tags = ["meta"]; + } + } + } catch (e) { + console.error("Failed to list pages directly from space:", e); + // Handle error, maybe show notification or leave list empty + this.flashNotification( + "Could not fetch page list directly.", + "error", + ); + } + // Note: Aspiring pages are not available through fetchPageList() } - const allPages = await this.clientSystem.queryLuaObjects( - "page", - {}, - ); - const allAspiringPages = - (await this.clientSystem.queryLuaObjects("aspiring-page", { - select: parseExpressionString("name"), - distinct: true, - })).map((name): PageMeta => ({ - ref: name, - tag: "page", - _isAspiring: true, - name: name, - created: "", - lastModified: "", - perm: "rw", - })); this.ui.viewDispatch({ type: "update-page-list", - allPages: allPages.concat(allAspiringPages), + allPages: allPages, }); }