Fix: proper progress status update (#2077)

* Fix: proper progress status update

* Swap type and progress in signatures
This commit is contained in:
Sviatoslav Kokurin
2026-08-05 15:24:14 +02:00
committed by GitHub
parent e5bc7a0e18
commit fc69d951b4
5 changed files with 88 additions and 28 deletions
+51 -11
View File
@@ -111,7 +111,13 @@ export class MainUI {
}
// Progress circle handling
private progressTimeout?: ReturnType<typeof setTimeout>;
private progressMap = new Map<
"index" | "sync",
{
percentage: number;
timeout: ReturnType<typeof setTimeout>;
}
>();
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(
+7 -8
View File
@@ -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": {
+7 -3
View File
@@ -298,10 +298,14 @@ export function focus(): Promise<void> {
}
export function showProgress(
progressPercentage?: number,
progressType?: string,
progressType: "sync" | "index",
progressPercentage: number,
): Promise<void> {
return syscall("editor.showProgress", progressPercentage, progressType);
return syscall("editor.showProgress", progressType, progressPercentage);
}
export function hideProgress(progressType: "sync" | "index"): Promise<void> {
return syscall("editor.showProgress", progressType);
}
/**
+12 -4
View File
@@ -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);
}
+11 -2
View File
@@ -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);
}
}
}