From 3e94389c1ea3b41880f888449e0e58e54ff129d9 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 10 Jul 2024 11:32:07 +0200 Subject: [PATCH] Fix missed events --- common/spaces/evented_space_primitives.ts | 28 +++++++++++++++++++++-- server/server_system.ts | 25 +++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/common/spaces/evented_space_primitives.ts b/common/spaces/evented_space_primitives.ts index 59cebbcd..4fb000b2 100644 --- a/common/spaces/evented_space_primitives.ts +++ b/common/spaces/evented_space_primitives.ts @@ -18,12 +18,14 @@ export class EventedSpacePrimitives implements SpacePrimitives { // This is ok, because any event will be picked up in a following iteration. operationInProgress = false; - initialFileListLoad = true; + initialFileListLoad: boolean; + + public enabled = true; - spaceSnapshot: Record = {}; constructor( private wrapped: SpacePrimitives, private eventHook: EventHook, + private spaceSnapshot: Record = {}, ) { // Translate file change events for attachments into attachment:index events this.eventHook.addLocalListener( @@ -37,6 +39,8 @@ export class EventedSpacePrimitives implements SpacePrimitives { } }, ); + this.initialFileListLoad = Object.keys(this.spaceSnapshot).length === 0; + // console.log("Loaded space snapshot", spaceSnapshot); } dispatchEvent(name: string, ...args: any[]): Promise { @@ -44,6 +48,9 @@ export class EventedSpacePrimitives implements SpacePrimitives { } async fetchFileList(): Promise { + if (!this.enabled) { + return this.wrapped.fetchFileList(); + } if (this.operationInProgress) { // Some other operation (read, write, list, meta) is already going on // this will likely trigger events, so let's not worry about any of that and avoid race condition and inconsistent data. @@ -52,6 +59,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { ); return this.wrapped.fetchFileList(); } + // console.log("Fetching file list"); // Fetching mutex this.operationInProgress = true; try { @@ -77,6 +85,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { oldHash !== newHash ) ) { + console.log("Detected file change", meta.name, oldHash, newHash); await this.dispatchEvent( "file:changed", meta.name, @@ -100,6 +109,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { } await this.dispatchEvent("file:listed", newFileList); + await this.dispatchEvent("file:spaceSnapshotted", this.spaceSnapshot); this.initialFileListLoad = false; return newFileList; } finally { @@ -110,6 +120,9 @@ export class EventedSpacePrimitives implements SpacePrimitives { async readFile( name: string, ): Promise<{ data: Uint8Array; meta: FileMeta }> { + if (!this.enabled) { + return this.wrapped.readFile(name); + } try { // Fetching mutex const wasFetching = this.operationInProgress; @@ -133,6 +146,9 @@ export class EventedSpacePrimitives implements SpacePrimitives { selfUpdate?: boolean, meta?: FileMeta, ): Promise { + if (!this.enabled) { + return this.wrapped.writeFile(name, data, selfUpdate, meta); + } try { this.operationInProgress = true; const newMeta = await this.wrapped.writeFile( @@ -163,6 +179,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { text, }); } + await this.dispatchEvent("file:spaceSnapshotted", this.spaceSnapshot); return newMeta; } finally { this.operationInProgress = false; @@ -180,6 +197,9 @@ export class EventedSpacePrimitives implements SpacePrimitives { } async getFileMeta(name: string): Promise { + if (!this.enabled) { + return this.wrapped.getFileMeta(name); + } try { const wasFetching = this.operationInProgress; this.operationInProgress = true; @@ -204,6 +224,9 @@ export class EventedSpacePrimitives implements SpacePrimitives { } async deleteFile(name: string): Promise { + if (!this.enabled) { + return this.wrapped.deleteFile(name); + } try { this.operationInProgress = true; if (name.endsWith(".md")) { @@ -214,6 +237,7 @@ export class EventedSpacePrimitives implements SpacePrimitives { await this.wrapped.deleteFile(name); delete this.spaceSnapshot[name]; await this.dispatchEvent("file:deleted", name); + await this.dispatchEvent("file:spaceSnapshotted", this.spaceSnapshot); } finally { this.operationInProgress = false; } diff --git a/server/server_system.ts b/server/server_system.ts index 1d92a8b8..dd0931ee 100644 --- a/server/server_system.ts +++ b/server/server_system.ts @@ -97,13 +97,31 @@ export class ServerSystem extends CommonSystem { this.system.addHook(codeWidgetHook); - this.spacePrimitives = new EventedSpacePrimitives( + // Look up spaceCache meta data in KV + const spaceCache = (await this.ds.get(["$spaceCache"])) || {}; + + const eventedSpacePrimitives = new EventedSpacePrimitives( new PlugSpacePrimitives( this.spacePrimitives, plugNamespaceHook, ), this.eventHook, + spaceCache, ); + + // Disable events until everything is set up + eventedSpacePrimitives.enabled = false; + + this.spacePrimitives = eventedSpacePrimitives; + + this.eventHook.addLocalListener( + "file:spaceSnapshotted", + (snapshot: Record) => { + console.log("Space snapshot updated"); + return this.ds.set(["$spaceCache"], snapshot); + }, + ); + const space = new Space(this.spacePrimitives, this.eventHook); // Add syscalls @@ -187,6 +205,11 @@ export class ServerSystem extends CommonSystem { }, ); + // All setup, enable eventing + eventedSpacePrimitives.enabled = true; + + space.updatePageList().catch(console.error); + // Ensure a valid index const indexPromise = ensureSpaceIndex(this.ds, this.system); if (awaitIndex) {