diff --git a/application/state/sftpTransferCenterStore.test.ts b/application/state/sftpTransferCenterStore.test.ts index 730f53cb7..94844d89a 100644 --- a/application/state/sftpTransferCenterStore.test.ts +++ b/application/state/sftpTransferCenterStore.test.ts @@ -4007,3 +4007,47 @@ test("partial cancel failure resets recovered folder children so the re-walk can assert.equal(afterFullCancel.find((row) => row.id === id)?.status, "cancelled"); } }); + +test("lifecycle hierarchy snapshot stays stable on bytes and changes on actual state transitions", () => { + const store = createSftpTransferCenterStore(); + store.publishOwner("panel", [makeTask("hierarchy-live")]); + const hierarchy = store.getLifecycleSnapshot(); + store.patchTask("hierarchy-live", { transferredBytes: 5, speed: 3 }); + assert.equal(store.getLifecycleSnapshot(), hierarchy); + assert.equal(store.getTask("hierarchy-live")?.transferredBytes, 5); + store.getSnapshot(); // Other progress consumers must not invalidate the hierarchy. + assert.equal(store.getLifecycleSnapshot(), hierarchy); + store.patchTask("hierarchy-live", { status: "failed", error: "fixture failure" }); + assert.notEqual(store.getLifecycleSnapshot(), hierarchy); + assert.equal(store.getLifecycleSnapshot().tasks[0].status, "failed"); +}); + +test("progress that admits or changes a task lifecycle also refreshes the hierarchy", () => { + const store = createSftpTransferCenterStore(); + const initial = store.getLifecycleSnapshot(); + store.ingestBackgroundEvent({ type: "progress", transferId: "late-start", transferred: 1 }); + const admitted = store.getLifecycleSnapshot(); + assert.notEqual(admitted, initial); + assert.equal(admitted.tasks[0].id, "late-start"); + store.publishOwner("panel", [makeTask("queued-progress", "queued")]); + const queued = store.getLifecycleSnapshot(); + store.ingestBackgroundEvent({ type: "progress", transferId: "queued-progress", transferred: 3 }); + assert.notEqual(store.getLifecycleSnapshot(), queued); + assert.equal(store.getLifecycleSnapshot().tasks.find((task) => task.id === "queued-progress")?.status, "transferring"); + const live = store.getLifecycleSnapshot(); + store.ingestBackgroundEvent({ type: "progress", transferId: "queued-progress", transferred: 4 }); + assert.equal(store.getLifecycleSnapshot(), live); +}); + +test("admission updates hierarchy state without forcing full owner lifecycle work", () => { + const store = createSftpTransferCenterStore(); + const task = makeTask("admitted-hierarchy", "queued"); + store.publishOwner("panel", [task]); + const queued = store.getLifecycleSnapshot(); + let lifecycleNotifications = 0; + store.subscribe(() => { lifecycleNotifications += 1; }); + assert.equal(store.admitTaskRun(task), "ready"); + assert.notEqual(store.getLifecycleSnapshot(), queued); + assert.equal(store.getLifecycleSnapshot().tasks[0].status, "transferring"); + assert.equal(lifecycleNotifications, 0, "dispatch admission keeps its existing lightweight fanout"); +}); diff --git a/application/state/sftpTransferCenterStore.ts b/application/state/sftpTransferCenterStore.ts index 0ab378a1a..b021db2fd 100644 --- a/application/state/sftpTransferCenterStore.ts +++ b/application/state/sftpTransferCenterStore.ts @@ -109,6 +109,8 @@ export interface SftpTransferCenterStore { */ subscribeProgress(listener: Listener): () => void; getSnapshot(): SftpTransferCenterSnapshot; + /** Stable task hierarchy between lifecycle changes; live rows read getTask. */ + getLifecycleSnapshot(): SftpTransferCenterSnapshot; /** Stable identity when badge counts are unchanged (avoids TopTabs thrash). */ getBadgeSnapshot(): { count: number; hasAttention: boolean }; /** Single task row — same object identity until that row is patched. */ @@ -420,6 +422,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S event: BackgroundTransferEvent; }>>(); let snapshot = tasks.length > 0 ? buildSnapshot(tasks) : EMPTY_SNAPSHOT; + let lifecycleSnapshot = snapshot; let badgeSnapshot = buildBadgeSnapshot(tasks); let snapshotDirty = false; const listeners = new Set(); @@ -695,6 +698,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S } snapshotDirty = false; snapshot = buildSnapshot(tasks); + lifecycleSnapshot = snapshot; refreshBadgeSnapshot(); persist(shouldPersistImmediately); notifyDedicatedResumeWaiters(); @@ -707,19 +711,24 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S }; /** Progress-only: mutate tasks, defer snapshot rebuild, wake progress UI only. */ - const emitProgress = (persistImmediately = false) => { + const emitProgress = (persistImmediately = false, lifecycleChanged = false) => { if (deferRestoreReplayEmits) { snapshotDirty = true; persistenceDirty = persistenceDirty || persistImmediately; return; } snapshotDirty = true; + // Admission/late progress may change a row's lifecycle without the costly + // prune/persist path. Keep the list hierarchy current, coalescing its UI + // notification with progress instead of waking owner lifecycle listeners. + if (lifecycleChanged) lifecycleSnapshot = buildSnapshot(tasks); // Never JSON.stringify/localStorage on the pure-progress path. Sync storage // on large directory histories freezes CSS spinners every few seconds. // Pause / complete / fingerprint / lifecycle still force persist below. if (persistImmediately) { snapshotDirty = false; snapshot = buildSnapshot(tasks); + lifecycleSnapshot = snapshot; refreshBadgeSnapshot(); persist(true); notifyDedicatedResumeWaiters(); @@ -1401,6 +1410,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S return () => progressListeners.delete(listener); }, getSnapshot: () => ensureSnapshot(), + getLifecycleSnapshot: () => lifecycleSnapshot, getBadgeSnapshot: () => badgeSnapshot, admitTaskRun(incoming, pausedAtResume, completedAtRestart) { const rootId = incoming.parentTaskId ?? incoming.id; @@ -1454,7 +1464,8 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S }; tasks = next; captureObservedTask(next[index]); - emitProgress(false); + emitProgress(false, existing.status !== next[index].status + || existing.reconnectRequired !== next[index].reconnectRequired); return "ready"; }, observeTaskSettlement(expected, ignoredCompletion, onIdentityConflict) { @@ -2173,6 +2184,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S } let existingIndex = tasks.findIndex((task) => task.id === event.transferId); let existing = tasks[existingIndex]; + const beforeEvent = existing; const eventParent = event.parentTaskId ? tasks.find((task) => task.id === event.parentTaskId) : undefined; @@ -2508,8 +2520,14 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S if (existingIndex >= 0) captureObservedTask(tasks[existingIndex]); // Progress-only ticks skip prune + coalesce listener paints; lifecycle // events still take the full emit path so UI never lags on pause/cancel. + const currentTask = tasks[existingIndex]; + const hierarchyOrLifecycleChanged = !beforeEvent + || currentTask?.status !== beforeEvent.status + || currentTask?.parentTaskId !== beforeEvent.parentTaskId + || currentTask?.background !== beforeEvent.background + || currentTask?.reconnectRequired !== beforeEvent.reconnectRequired; if (event.type === "progress" && !persistImmediately) { - emitProgress(false); + emitProgress(false, hierarchyOrLifecycleChanged); } else { emit(persistImmediately); } diff --git a/components/GlobalSftpTransferCenter.children-performance.test.tsx b/components/GlobalSftpTransferCenter.children-performance.test.tsx new file mode 100644 index 000000000..3b81f9c3e --- /dev/null +++ b/components/GlobalSftpTransferCenter.children-performance.test.tsx @@ -0,0 +1,94 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { createDomRenderer, dispatchDomEvent, flushEffects, installDomEnvironment, runWithAct } from "./test-support/renderReactDom.tsx"; + +test("expanded folder history bounds mounted files, isolates progress, and reaches the final child", async (t) => { + const env = installDomEnvironment(); + const previous = new Map(); + for (const [key, value] of Object.entries({ + MutationObserver: env.window.MutationObserver, + NodeFilter: env.window.NodeFilter, + HTMLInputElement: env.window.HTMLInputElement, + Element: env.window.Element, + requestAnimationFrame: (callback: FrameRequestCallback) => setTimeout(() => callback(Date.now()), 0), + cancelAnimationFrame: clearTimeout, + })) { + previous.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); + Object.defineProperty(globalThis, key, { configurable: true, writable: true, value }); + } + Object.defineProperties(env.window.HTMLElement.prototype, { + offsetWidth: { configurable: true, get: () => 460 }, + offsetHeight: { configurable: true, get() { return this.dataset.section === "global-sftp-transfer-child-list" ? 192 : 460; } }, + }); + const { I18nProvider } = await import("../application/i18n/I18nProvider.tsx"); + const { sftpTransferCenterStore: store } = await import("../application/state/sftpTransferCenterStore.ts"); + const { GlobalSftpTransferCenter } = await import("./GlobalSftpTransferCenter.tsx"); + const { TooltipProvider } = await import("./ui/tooltip.tsx"); + const renderer = await createDomRenderer(env.document); + t.after(async () => { + await renderer.unmount(); + for (const [key, descriptor] of previous) { + if (descriptor) Object.defineProperty(globalThis, key, descriptor); + else Reflect.deleteProperty(globalThis, key); + } + env.cleanup(); + }); + const childCount = 1_000; + const base = { + fileName: "folder", sourcePath: "/source", targetPath: "/target", + sourceConnectionId: "source", targetConnectionId: "local", direction: "download" as const, + totalBytes: 1_000, transferredBytes: 0, speed: 0, startTime: 1, + }; + store.publishOwner("children-performance", [ + { ...base, id: "folder", status: "transferring", isDirectory: true, progressMode: "files" }, + ...Array.from({ length: childCount }, (_, index) => ({ + ...base, id: `child-${index}`, fileName: `child-${index}.bin`, sourcePath: `/source/${index}`, targetPath: `/target/${index}`, + status: index === 0 ? "transferring" as const : "queued" as const, + startTime: index + 2, isDirectory: false, parentTaskId: "folder", + })), + ]); + await renderer.render(); + const click = async (selector: string) => { + const element = env.document.querySelector(selector); + assert.ok(element, selector); + await dispatchDomEvent(element, new env.window.MouseEvent("click", { bubbles: true })); + await flushEffects(); + }; + await click("[data-section=global-sftp-transfer-toggle]"); + await click('button[aria-label="Show files"]'); + const childList = () => env.document.querySelector("[data-section=global-sftp-transfer-child-list]"); + const childRows = () => childList()?.querySelectorAll('[data-transfer-status]') ?? []; + assert.ok(childRows().length > 0 && childRows().length < 40, `mounted ${childRows().length} child rows`); + assert.match(childList()?.textContent ?? "", /child-0.bin/); + + let orderReads = 0; + for (const task of store.getSnapshot().tasks) { + if (!task.parentTaskId) continue; + const startTime = task.startTime; + Object.defineProperty(task, "startTime", { configurable: true, enumerable: true, get: () => { orderReads += 1; return startTime; } }); + } + await runWithAct(async () => { + store.patchTask("child-0", { transferredBytes: 500, speed: 100 }); + await new Promise((resolve) => setTimeout(resolve, 20)); + }); + assert.ok(orderReads < 20, `one byte update re-sorted the directory (${orderReads} reads)`); + assert.match(childList()?.textContent ?? "", /500 B/); + + const list = childList()!; + list.scrollTop = 24 * childCount - 192; + await dispatchDomEvent(list, new env.window.Event("scroll")); + await flushEffects(); + assert.ok(childRows().length > 0 && childRows().length < 40); + assert.match(childList()?.textContent ?? "", /child-999.bin/); + await runWithAct(() => store.patchTask("child-999", { status: "completed", transferredBytes: 1_000 })); + assert.equal(store.getTask("folder")?.directoryResumeCheckpoint?.completedEntries, 1, "completed files remain counted in the existing compact folder checkpoint"); + assert.equal(store.getTask("child-999"), undefined, "the existing completion compaction still owns history retention"); + assert.equal(store.getTask("child-500")?.status, "queued"); + await click('button[aria-label="Hide files"]'); + assert.equal(childList(), null); + await click('button[aria-label="Show files"]'); + assert.ok(childRows().length > 0 && childRows().length < 40); + assert.equal(store.getSnapshot().tasks.filter((task) => task.parentTaskId === "folder").length, childCount - 1, "virtualization never drops queued files"); + await click("[data-section=global-sftp-transfer-toggle]"); +}); diff --git a/components/GlobalSftpTransferCenter.tsx b/components/GlobalSftpTransferCenter.tsx index 9f0f4d1a3..87c0e2735 100644 --- a/components/GlobalSftpTransferCenter.tsx +++ b/components/GlobalSftpTransferCenter.tsx @@ -24,6 +24,7 @@ import { sftpTransferCenterStore, useSftpTransferCenterBadge, useSftpTransferResuming, + useSftpTransferTask, type SftpTransferCenterSnapshot, } from "../application/state/sftpTransferCenterStore"; import { transferRuntime } from "../application/state/sftp/transferRuntime"; @@ -47,28 +48,22 @@ const CLOSED_TRANSFER_CENTER_SNAPSHOT: SftpTransferCenterSnapshot = { }; /** - * Full transfer-center list only while the popover is open. When closed, the - * getter always returns the same CLOSED snapshot so progress store notifies do - * not re-render this TopTabs child (badge uses a separate stable subscription). + * Read the folder hierarchy only while open and only on lifecycle changes. + * Mounted rows subscribe to their own bytes; progress must not regroup/sort + * every folder or re-render the center and its TopTabs ancestor. */ function useSftpTransferCenterWhenOpen(open: boolean): SftpTransferCenterSnapshot { const openRef = useRef(open); openRef.current = open; return useSyncExternalStore( (onStoreChange) => { - // Lifecycle + progress: when the popover is open the list must move with - // bytes; when closed getSnapshot returns a stable CLOSED constant so - // progress ticks are cheap no-ops for this subscriber. - const unsubLifecycle = sftpTransferCenterStore.subscribe(onStoreChange); - const unsubProgress = sftpTransferCenterStore.subscribeProgress(onStoreChange); - return () => { - unsubLifecycle(); - unsubProgress(); - }; + const unsubscribeLifecycle = sftpTransferCenterStore.subscribe(onStoreChange); + const unsubscribeProgress = sftpTransferCenterStore.subscribeProgress(onStoreChange); + return () => { unsubscribeLifecycle(); unsubscribeProgress(); }; }, () => ( openRef.current - ? sftpTransferCenterStore.getSnapshot() + ? sftpTransferCenterStore.getLifecycleSnapshot() : CLOSED_TRANSFER_CENTER_SNAPSHOT ), () => CLOSED_TRANSFER_CENTER_SNAPSHOT, @@ -340,8 +335,74 @@ function formatTransferPathLine(task: Pick 0 ? `${percent.toFixed(0)}%` : t(statusLabelKey(task.status))); + if (!compact) { + return ( +
+ + {detail} + {task.status === "transferring" && } +
+ ); + } + const childFrozen = frozen || task.status === "pausing" || task.status === "paused"; + return ( +
+
+ + {detail} +
+ {task.totalBytes > 0 && ( +
+
+
+ )} +
+ ); +}); + +const TransferChildrenList = React.memo(function TransferChildrenList({ tasks }: { tasks: readonly TransferTask[] }) { + const scrollRef = useRef(null); + const virtual = tasks.length > 20; + const virtualizer = useVirtualizer({ + count: tasks.length, + getScrollElement: () => scrollRef.current, + estimateSize: () => 24, + getItemKey: (index) => tasks[index].id, + initialRect: { width: 0, height: 192 }, + overscan: 4, + enabled: virtual, + }); + return ( +
+
+ {!virtual ? tasks.map((task) => ) : ( +
+ {virtualizer.getVirtualItems().map((row) => ( +
+ +
+ ))} +
+ )} +
+
+ ); +}); + function TransferRow({ - task, + task: snapshotTask, childTasks = [], expanded, onToggleExpanded, @@ -353,6 +414,7 @@ function TransferRow({ onToggleExpanded: () => void; isLast: boolean; }) { + const task = useSftpTransferTask(snapshotTask.id, snapshotTask); const { t } = useI18n(); const folderReplaceWarningId = React.useId(); // Optimistic spinner from click until store status moves off paused/interrupted. @@ -605,72 +667,14 @@ function TransferRow({ )} data-section="global-sftp-transfer-active-children" > - {displayChildren.map((child) => { - const childPercent = getGlobalTransferProgressPercent(child); - const childDetail = formatChildByteProgress(child); - const childFrozen = isPausing - || task.status === "paused" - || child.status === "pausing" - || child.status === "paused"; - return ( -
-
- - - {childDetail || (child.totalBytes > 0 ? `${childPercent.toFixed(0)}%` : t(statusLabelKey(child.status)))} - -
- {child.totalBytes > 0 && ( -
-
-
- )} -
- ); - })} + {displayChildren.map((child) => ( + + ))}
)} {isDirParent && expanded && childTasks.length > 0 && ( -
- {childTasks.map((child) => { - const childPercent = getGlobalTransferProgressPercent(child); - const childDetail = formatChildByteProgress(child); - return ( -
- - - {childDetail - || (child.totalBytes > 0 ? `${childPercent.toFixed(0)}%` : t(statusLabelKey(child.status)))} - - {child.status === "transferring" && ( - - )} -
- ); - })} -
+ )} {task.status === "attention" && task.conflict && canControl && (() => { diff --git a/components/GlobalSftpTransferCenter.virtualization.test.tsx b/components/GlobalSftpTransferCenter.virtualization.test.tsx index 423916dc4..0680ee310 100644 --- a/components/GlobalSftpTransferCenter.virtualization.test.tsx +++ b/components/GlobalSftpTransferCenter.virtualization.test.tsx @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import React from "react"; -import { createDomRenderer, dispatchDomEvent, flushEffects, installDomEnvironment } from "./test-support/renderReactDom.tsx"; +import { createDomRenderer, dispatchDomEvent, flushEffects, installDomEnvironment, runWithAct } from "./test-support/renderReactDom.tsx"; test("large transfer histories mount only visible rows and can scroll to the last file", async (t) => { const env = installDomEnvironment(); @@ -64,6 +64,33 @@ test("large transfer histories mount only visible rows and can scroll to the las const lastRow = env.document.querySelector('[role=progressbar][aria-label="file-0.bin"]')?.closest('[data-transfer-status]'); assert.ok(lastRow?.classList.contains("border-b-0"), "only the actual final row omits its divider"); assert.equal(store.getSnapshot().tasks.length, 1_000, "offscreen files must remain queued"); + await runWithAct(() => { + store.patchTask("virtual-file-999", { isDirectory: true, progressMode: "files" }); + store.upsertTasks(Array.from({ length: 100 }, (_, index) => ({ + id: `nested-file-${index}`, fileName: `nested-${index}.bin`, + sourcePath: `/source/999/${index}`, targetPath: `/target/999/${index}`, + sourceConnectionId: "source", targetConnectionId: "local", direction: "download" as const, + status: "queued" as const, totalBytes: 1_000, transferredBytes: 0, speed: 0, + startTime: index + 2_000, isDirectory: false, parentTaskId: "virtual-file-999", + }))); + }); + scroll.scrollTop = 0; + await dispatchDomEvent(scroll, new env.window.Event("scroll")); + await flushEffects(); + const expand = env.document.querySelector('button[aria-label="Show files"]'); + assert.ok(expand); + await dispatchDomEvent(expand, new env.window.MouseEvent("click", { bubbles: true })); + await flushEffects(); + assert.ok(env.document.querySelector('[data-section="global-sftp-transfer-child-list"]')); + scroll.scrollTop = 112 * 1_000 - 460; + await dispatchDomEvent(scroll, new env.window.Event("scroll")); + await flushEffects(); + scroll.scrollTop = 0; + await dispatchDomEvent(scroll, new env.window.Event("scroll")); + await flushEffects(); + assert.ok(env.document.querySelector('button[aria-label="Hide files"]'), "folder expansion survives top-level row recycling"); + assert.ok(env.document.querySelector('[data-section="global-sftp-transfer-child-list"]')); + await dispatchDomEvent(toggle, new env.window.MouseEvent("click", { bubbles: true })); await flushEffects(); }); diff --git a/docs/research/sftp-folder-list-performance.md b/docs/research/sftp-folder-list-performance.md new file mode 100644 index 000000000..4102b0d72 --- /dev/null +++ b/docs/research/sftp-folder-list-performance.md @@ -0,0 +1,47 @@ +# Expanded SFTP folder-list responsiveness + +The global transfer center already limited mounted top-level rows, but expanding +a folder rendered every retained child file. Each progress update also rebuilt +all folder groups and sorted their children. A folder with thousands of queued +files therefore stalled the renderer even when only one file's bytes changed. + +The child list now mounts its visible range with a small overscan. Its existing +scroll area still reaches every retained file. The transfer center reads a stable +hierarchy snapshot between lifecycle changes; mounted parent/child rows subscribe +to their own live progress. Progress that introduces a row or changes its status +still refreshes the hierarchy. Completion retention and compact checkpoints are +unchanged. + +## Controlled measurements + +A separate Electron window mounted the actual before/after transfer center with +two synthetic directory transfers. No SSH connection, real file or user transfer +was involved. The same expand action, ten updates to one child and a scroll to +the final file were exercised in each version. + +| Children | Work | Before | After | +| ---: | --- | ---: | ---: | +| 1,000 | Expand to next rendered frame | 298.8 ms | 10.94 ms | +| 1,000 | One child's progress to next frame | 112.6–125.6 ms | 1.18–18.18 ms | +| 5,000 | Expand to next rendered frame | 1,216.1 ms | 14.36 ms | +| 5,000 | One child's progress to next frame | 500.5–538.4 ms | 1.31–6.03 ms | +| 1,000 / 5,000 | Mounted child rows | 1,000 / 5,000 | 12 / 12 | + +Both repaired lists reached their final child and retained all 1,000 / 5,000 +queued records. Native scrolling, collapse/reopen and live byte display were +also checked. These are development-fixture measurements on one machine, not a +cross-device frame-rate guarantee. + +The actual-component regression fails on the original implementation because +it mounts 1,000 children. It verifies bounded mounted rows, live progress without +re-sorting the folder, final-child access, completion accounting and collapse / +reopen. The existing top-level recycling test also expands a folder, scrolls it +out of view and returns to its retained expansion state. + +## Issue relationship + +Related to #3155. Its Windows report and subsequent comments cover multiple +possible large-transfer failures; they do not establish that this exact expanded +folder-list workload caused the reporter's freeze. This repair is independently +reproduced UI work and must not close that issue. Open #3694 and #3696 modify +file metadata / transfer connection acquisition, not this list behavior.