From 502b636aefdffcafe1bc1e47840eb225f5edcae8 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Thu, 5 Feb 2026 11:20:11 +0100 Subject: [PATCH] Fixes #1815: Changes how index status is tracked - New check for full indexing completion - Renamed `hasInitialIndexCompleted` to `hasFullIndexCompleted` for clarity. - Removed file:initial event (no longer required and not documented for public consumption) Hopefully doesn't introduce new regressions. Needs testing time "in the field". --- client/client.ts | 9 ++- client/client_system.ts | 4 +- client/data/mq.datastore.ts | 10 ++- client/data/object_index.ts | 99 +++++++++++------------ client/spaces/evented_space_primitives.ts | 8 -- 5 files changed, 63 insertions(+), 67 deletions(-) diff --git a/client/client.ts b/client/client.ts index 8f5e1b89..081b95e8 100644 --- a/client/client.ts +++ b/client/client.ts @@ -600,7 +600,7 @@ export class Client { console.log("Updating page list cache"); // Check if the initial sync has been completed const initialIndexCompleted = await this.objectIndex - .hasInitialIndexCompleted(); + .hasFullIndexCompleted(); let allPages: PageMeta[] = []; @@ -1118,7 +1118,7 @@ export class Client { // Fetch the meta which includes the possibly indexed stuff, like page // decorations - if (await this.objectIndex.hasInitialIndexCompleted()) { + if (await this.objectIndex.hasFullIndexCompleted()) { try { const enrichedMeta = await this.objectIndex.getObjectByRef( pageName, @@ -1284,7 +1284,10 @@ export class Client { console.warn("Not loading custom styles, since space style is disabled"); return; } - if (!await this.objectIndex.hasInitialIndexCompleted()) { + if (!await this.objectIndex.hasFullIndexCompleted()) { + console.warn( + "Not loading custom styles, since full indexing has not completed yet", + ); return; } diff --git a/client/client_system.ts b/client/client_system.ts index 4e1b875b..cff4f2c7 100644 --- a/client/client_system.ts +++ b/client/client_system.ts @@ -200,9 +200,9 @@ export class ClientSystem { console.info("Space Lua scripts are disabled, skipping loading scripts"); return; } - if (!await this.objectIndex.hasInitialIndexCompleted()) { + if (!await this.objectIndex.hasFullIndexCompleted()) { console.info( - "Not loading space scripts, since initial indexing has not completed yet", + "Not loading space scripts, since full indexing has not completed yet", ); return; } diff --git a/client/data/mq.datastore.ts b/client/data/mq.datastore.ts index fb8e7d6f..22b67330 100644 --- a/client/data/mq.datastore.ts +++ b/client/data/mq.datastore.ts @@ -379,10 +379,14 @@ export class DataStoreMQ { }; } - async awaitEmptyQueue(queue: string): Promise { + public async isQueueEmpty(queue: string): Promise { + const stats = await this.getQueueStats(queue); + return stats.queued === 0 && stats.processing === 0; + } + + public async awaitEmptyQueue(queue: string): Promise { while (true) { - const stats = await this.getQueueStats(queue); - if (stats.queued === 0 && stats.processing === 0) { + if (await this.isQueueEmpty(queue)) { break; } await sleep(200); diff --git a/client/data/object_index.ts b/client/data/object_index.ts index 53fb3eee..a3b89a1f 100644 --- a/client/data/object_index.ts +++ b/client/data/object_index.ts @@ -21,7 +21,6 @@ const indexKey = "idx"; const pageKey = "ridx"; const indexVersionKey = ["$indexVersion"]; -const indexQueuedKey = ["$indexQueued"]; // Bump this one every time a full reindex is needed const desiredIndexVersion = 9; @@ -53,40 +52,47 @@ export class ObjectIndex { private eventHook: EventHook, private mq: DataStoreMQ, ) { - let startTime = -1; - this.eventHook.addLocalListener("file:initial", async () => { - startTime = Date.now(); - await this.setIndexOngoing(true); + // Clear any entries for deleted files + this.eventHook.addLocalListener("file:deleted", (path: string) => { + return this.clearFileIndex(path); }); - this.eventHook.addLocalListener("file:deleted", async (path: string) => { - await this.clearFileIndex(path); + // Tracks if the file:listed event has been triggered, + // which is fired after all file:changed events have been dispatched + // resulting in new index entries (if any) being queued in the index queue + // this is later used to track if the index is complete + let indexStarted = false; + this.eventHook.addLocalListener("file:listed", () => { + indexStarted = true; }); - const emptyQueueHandler = async () => { - await this.setIndexOngoing(false); - if ( - startTime !== -1 && !await this.hasInitialIndexCompleted() - ) { - // Indexing has just finished for the first time - console.info( - "Initial index complete after", - (Date.now() - startTime) / 1000, - "s", - ); - // Unsubscribe myself - this.eventHook.removeLocalListener( + // Handle initial index completion + this.hasFullIndexCompleted().then((hasCompleted) => { + if (!hasCompleted) { + const emptyQueueHandler = async () => { + console.log("Index queue empty, checking if index is complete"); + // Theoretically we could get empty queue notifications before the file:listed event has been triggered, so let's account for this + if (indexStarted) { + // Indexing has just finished for the first time for this client + console.info( + "Initial index complete, reloading editor state", + ); + await this.markFullIndexComplete(); + // Unsubscribe yourself + this.eventHook.removeLocalListener( + "mq:emptyQueue:indexQueue", + emptyQueueHandler, + ); + // Trigger an editor:reloadState event to reload the editor state (render widgets etc.) + this.eventHook.dispatchEvent("editor:reloadState"); + } + }; + this.eventHook.addLocalListener( "mq:emptyQueue:indexQueue", emptyQueueHandler, ); - await this.markInitialIndexComplete(); - this.eventHook.dispatchEvent("editor:reloadState"); } - }; - this.eventHook.addLocalListener( - "mq:emptyQueue:indexQueue", - emptyQueueHandler, - ); + }); } tag(tagName: string): LuaQueryCollection { @@ -128,22 +134,7 @@ export class ObjectIndex { return this.ds.get([indexKey, tag, this.cleanKey(ref, page), page]); } - async isIndexOngoing() { - return !!(await this.ds.get(indexQueuedKey)); - } - - async setIndexOngoing(val: boolean = true) { - await this.ds.set(indexQueuedKey, val); - } - async ensureFullIndex(space: Space) { - // Commenting out this check because I think it always holds when calling this API - // if (!this.client.fullSyncCompleted) { - // console.info( - // "Initial full sync not completed, skipping index check", - // ); - // return; - // } const currentIndexVersion = await this.getCurrentIndexVersion(); if (!currentIndexVersion) { @@ -152,8 +143,10 @@ export class ObjectIndex { } if ( + // If the index version is less than the desired version currentIndexVersion < desiredIndexVersion && - !await this.isIndexOngoing() + // And the index queue is empty (meaning no indexing is ongoing) + await this.mq.isQueueEmpty("indexQueue") ) { console.info( "[index]", @@ -161,12 +154,10 @@ export class ObjectIndex { currentIndexVersion, desiredIndexVersion, ); - await this.setIndexOngoing(true); + await this.reindexSpace(space); - console.info("[index]", "Full space index complete."); - await this.markInitialIndexComplete(); - await this.setIndexOngoing(false); - // Let's load space scripts again, which probably weren't loaded before + + // Dispatch an editor:reloadState event to reload the editor state (render widgets etc.) this.eventHook.dispatchEvent("editor:reloadState"); } } @@ -174,6 +165,7 @@ export class ObjectIndex { async reindexSpace(space: Space) { console.log("Clearing page index..."); await this.clearIndex(); + await this.markFullIndexInComplete(); const files = await space.deduplicatedFileList(); @@ -182,10 +174,11 @@ export class ObjectIndex { const startTime = Date.now(); await this.mq.batchSend("indexQueue", files.map((file) => file.name)); await this.mq.awaitEmptyQueue("indexQueue"); - console.log("Done with full index after", Date.now() - startTime, "ms"); + await this.markFullIndexComplete(); + console.log("Full index completed after", Date.now() - startTime, "ms"); } - public async hasInitialIndexCompleted() { + public async hasFullIndexCompleted() { return (await this.ds.get(indexVersionKey)) >= desiredIndexVersion; } @@ -193,10 +186,14 @@ export class ObjectIndex { return this.ds.get(indexVersionKey); } - async markInitialIndexComplete() { + async markFullIndexComplete() { await this.ds.set(indexVersionKey, desiredIndexVersion); } + async markFullIndexInComplete() { + await this.ds.delete(indexVersionKey); + } + cleanKey(ref: string, page: string) { if (ref.startsWith(`${page}@`)) { return ref.substring(page.length + 1); diff --git a/client/spaces/evented_space_primitives.ts b/client/spaces/evented_space_primitives.ts index 9af5f383..c97c6bd8 100644 --- a/client/spaces/evented_space_primitives.ts +++ b/client/spaces/evented_space_primitives.ts @@ -37,15 +37,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { console.log("Loading snapshot and enabling events"); this.spaceSnapshot = (await this.ds.get(this.snapshotKey)) || {}; this.snapshotChanged = false; - const isFreshSnapshot = this.isSnapshotEmpty(); this.enabled = true; - // trigger loading and eventing - this.fetchFileList().then(async () => { - if (isFreshSnapshot) { - // Trigger event to signal that an intial batch of events has been triggered - await this.dispatchEvent("file:initial"); - } - }); } public isSnapshotEmpty() {