From 398e4523081c5a1f62c80310b50a45f5a84cc776 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 2 Mar 2024 15:21:36 +0100 Subject: [PATCH] Attempt to address #479 --- plug-api/types.ts | 1 + web/client.ts | 4 ++++ web/sync_service.ts | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/plug-api/types.ts b/plug-api/types.ts index 6e73a0bf..1b79bf90 100644 --- a/plug-api/types.ts +++ b/plug-api/types.ts @@ -186,6 +186,7 @@ export type AppEvent = | "editor:init" | "editor:pageLoaded" // args: pageName, previousPage, isSynced | "editor:pageReloaded" + | "editor:pageSaving" | "editor:pageSaved" | "editor:modeswitch" | "plugs:loaded" diff --git a/web/client.ts b/web/client.ts index dcfdb8e4..748d9ab7 100644 --- a/web/client.ts +++ b/web/client.ts @@ -667,6 +667,10 @@ export class Client { return resolve(); } console.log("Saving page", this.currentPage); + this.dispatchAppEvent( + "editor:pageSaving", + this.currentPage, + ); this.space .writePage( this.currentPage, diff --git a/web/sync_service.ts b/web/sync_service.ts index 5fcc1b49..0d73aa16 100644 --- a/web/sync_service.ts +++ b/web/sync_service.ts @@ -46,6 +46,8 @@ export interface ISyncService { export class SyncService implements ISyncService { spaceSync: SpaceSync; lastReportedSyncStatus = Date.now(); + // If this is set to anything other than undefined, a file is currently saving + savingTimeout: number | undefined; constructor( readonly localSpacePrimitives: SpacePrimitives, @@ -75,7 +77,21 @@ export class SyncService implements ISyncService { }, ); + eventHook.addLocalListener("editor:pageSaving", () => { + console.log("Saving..."); + this.savingTimeout = setTimeout(() => { + this.savingTimeout = undefined; + }, 1000 * 5); + }); + eventHook.addLocalListener("editor:pageSaved", (name) => { + console.log("Done saving..."); + if (this.savingTimeout) { + clearTimeout(this.savingTimeout); + this.savingTimeout = undefined; + } else { + console.warn("This should not happen, savingTimeout was not set"); + } const path = `${name}.md`; this.scheduleFileSync(path).catch(console.error); }); @@ -88,6 +104,12 @@ export class SyncService implements ISyncService { } async isSyncing(): Promise { + if (this.savingTimeout !== undefined) { + console.log( + "Saving a file at the moment, so reporting as isSyncing() = true", + ); + return true; + } const startTime = await this.ds.get(syncStartTimeKey); if (!startTime) { return false;