From fc69d951b429668b4bab6fbd7faaf857b82928cd Mon Sep 17 00:00:00 2001 From: Sviatoslav Kokurin Date: Wed, 5 Aug 2026 16:24:14 +0300 Subject: [PATCH] Fix: proper progress status update (#2077) * Fix: proper progress status update * Swap type and progress in signatures --- client/editor_ui.tsx | 62 ++++++++++++++++++++++++++------ client/plugos/syscalls/editor.ts | 15 ++++---- plug-api/syscalls/editor.ts | 10 ++++-- plugs/index/queue.ts | 16 ++++++--- plugs/sync/sync.ts | 13 +++++-- 5 files changed, 88 insertions(+), 28 deletions(-) diff --git a/client/editor_ui.tsx b/client/editor_ui.tsx index ae6253e9..85a3feaa 100644 --- a/client/editor_ui.tsx +++ b/client/editor_ui.tsx @@ -111,7 +111,13 @@ export class MainUI { } // Progress circle handling - private progressTimeout?: ReturnType; + private progressMap = new Map< + "index" | "sync", + { + percentage: number; + timeout: ReturnType; + } + >(); viewDispatch: (action: Action) => void = () => {}; @@ -152,20 +158,54 @@ export class MainUI { } } - showProgress(progressPercentage?: number, progressType?: "sync" | "index") { - this.viewDispatch({ - type: "set-progress", - progressPercentage, - progressType, - }); - if (this.progressTimeout) { - clearTimeout(this.progressTimeout); + private dispatchProgressState() { + // Hide when nothing is active + if (this.progressMap.size === 0) { + this.viewDispatch({ type: "set-progress" }); + return; } - this.progressTimeout = setTimeout(() => { + + // Sync takes precedence over index so the indicator + // doesn't flip between the two when both streams are firing. + const progressType: "sync" | "index" = this.progressMap.has("sync") + ? "sync" + : "index"; + const entry = this.progressMap.get(progressType); + if (entry) { this.viewDispatch({ type: "set-progress", + progressPercentage: entry.percentage, + progressType, }); - }, 5000); + } + } + + private removeProgressType(progressType: "index" | "sync") { + const entry = this.progressMap.get(progressType); + if (entry) { + clearTimeout(entry.timeout); + this.progressMap.delete(progressType); + } + } + + showProgress( + progressType: "sync" | "index", + progressPercentage?: number, + ) { + this.removeProgressType(progressType); + + if (progressPercentage !== undefined) { + const timeout = setTimeout(() => { + this.removeProgressType(progressType); + this.dispatchProgressState(); + }, 5000); + this.progressMap.set(progressType, { + percentage: progressPercentage, + timeout, + }); + } + + this.dispatchProgressState(); } filterBox( diff --git a/client/plugos/syscalls/editor.ts b/client/plugos/syscalls/editor.ts index 27c01b58..3c7d25f5 100644 --- a/client/plugos/syscalls/editor.ts +++ b/client/plugos/syscalls/editor.ts @@ -664,14 +664,19 @@ export function editorSyscalls(client: Client): SysCallMapping { "editor.showProgress": { callback: ( _ctx, + progressType: "sync" | "index", progressPercentage?: number, - progressType?: "sync" | "index", ) => { - client.ui.showProgress(progressPercentage, progressType); + client.ui.showProgress(progressType, progressPercentage); }, description: "Shows, updates, or hides a sync or indexing progress indicator.", parameters: [ + { + name: "progressType", + type: "sync | index", + description: "The operation represented by the indicator.", + }, { name: "progressPercentage", type: "number", @@ -679,12 +684,6 @@ export function editorSyscalls(client: Client): SysCallMapping { "Completion percentage, or undefined to hide the indicator.", optional: true, }, - { - name: "progressType", - type: "sync | index", - description: "The operation represented by the indicator.", - optional: true, - }, ], }, "editor.insertAtPos": { diff --git a/plug-api/syscalls/editor.ts b/plug-api/syscalls/editor.ts index 5a4dd148..b4a304a0 100644 --- a/plug-api/syscalls/editor.ts +++ b/plug-api/syscalls/editor.ts @@ -298,10 +298,14 @@ export function focus(): Promise { } export function showProgress( - progressPercentage?: number, - progressType?: string, + progressType: "sync" | "index", + progressPercentage: number, ): Promise { - return syscall("editor.showProgress", progressPercentage, progressType); + return syscall("editor.showProgress", progressType, progressPercentage); +} + +export function hideProgress(progressType: "sync" | "index"): Promise { + return syscall("editor.showProgress", progressType); } /** diff --git a/plugs/index/queue.ts b/plugs/index/queue.ts index 2e880e6e..aee9a6dd 100644 --- a/plugs/index/queue.ts +++ b/plugs/index/queue.ts @@ -65,20 +65,28 @@ async function totalItemsQueued() { async function updateIndexProgressInUI() { // Let's see if there's anything in the index queue let totalQueued = await totalItemsQueued(); + let lastProgress = -1; + let lastProgressAt = 0; while (totalQueued > 0) { const percentage = Math.round( ((maximumObservedQueueSize - totalQueued) / maximumObservedQueueSize) * 100, ); if (percentage > 0 && percentage <= 99) { - await editor.showProgress(percentage, "index"); + const now = Date.now(); + if (lastProgress !== percentage || now - lastProgressAt >= 1000) { + lastProgress = percentage; + lastProgressAt = now; + await editor.showProgress("index", percentage); + } } else { - // Hide progress circle - await editor.showProgress(); + await editor.hideProgress("index"); + lastProgressAt = 0; } - await sleep(1000); + await sleep(40); totalQueued = await totalItemsQueued(); } + await editor.hideProgress("index"); // Schedule again setTimeout(updateIndexProgressInUI, uiUpdateInterval); } diff --git a/plugs/sync/sync.ts b/plugs/sync/sync.ts index 3edbb5f5..8e7c0aa0 100644 --- a/plugs/sync/sync.ts +++ b/plugs/sync/sync.ts @@ -5,6 +5,9 @@ import { sync, } from "@silverbulletmd/silverbullet/syscalls"; +let lastProgress = -1; +let lastProgressAt = 0; + export async function syncSpaceCommand() { await editor.flashNotification("Syncing space..."); await sync.performSpaceSync(); @@ -48,9 +51,15 @@ export async function updateSyncStatus(event: { ); if (percentage >= 99) { // Just hide it - await editor.showProgress(); + await editor.hideProgress("sync"); + lastProgressAt = 0; } else { - await editor.showProgress(percentage, "sync"); + const now = Date.now(); + if (lastProgress !== percentage || now - lastProgressAt >= 1000) { + lastProgress = percentage; + lastProgressAt = now; + await editor.showProgress("sync", percentage); + } } }