Compare commits

...
Author SHA1 Message Date
bincxz 8cb18d5430 perf(sync): coalesce pending indicator payload collection 2026-10-18 12:43:55 +08:00
3 changed files with 114 additions and 2 deletions

No files matched your search

@@ -195,3 +195,82 @@ test("normal manual and debounced auto-sync upload panel creation, edits and del
Object.assign(manager, originals);
}
});
test("pending-sync indication coalesces edit bursts without repeatedly collecting the vault", async () => {
const manager = getCloudSyncManager();
const snapshot = {
...manager.getState(),
securityState: "UNLOCKED" as const,
syncState: "IDLE" as const,
autoSyncEnabled: true,
providers: {
...manager.getState().providers,
webdav: { provider: "webdav" as const, status: "connected" as const },
},
};
const originals = {
getState: manager.getState,
loadSyncBase: manager.loadSyncBase,
inspectProviderRemote: manager.inspectProviderRemote,
setPendingLocalSync: manager.setPendingLocalSync,
setAvailablePluginSyncProviders: manager.setAvailablePluginSyncProviders,
};
manager.getState = () => snapshot;
manager.loadSyncBase = async () => null;
manager.inspectProviderRemote = async () => ({ remoteFile: null, payload: null, remoteChanged: false });
const pending: boolean[] = [];
manager.setPendingLocalSync = (value) => { pending.push(value); };
manager.setAvailablePluginSyncProviders = () => {};
let collects = 0;
let collectBarrier: Promise<void> | undefined;
Object.defineProperty(window, "netcatty", { configurable: true, value: {
collectPluginSyncSidecars: async () => { collects++; await collectBarrier; return { version: 1, entries: [] }; },
} });
const apply = async () => {};
const config = { hosts: [], keys: [], snippets: [], customGroups: ["fixture"], startupReady: true,
onApplyPayload: apply, onApplyConvergentPayload: apply };
function Probe({ enabled = true }: { enabled?: boolean }) { useAutoSync({ ...config, enabled }); return null; }
let renderer: ReactTestRenderer | undefined;
const wait = async (ms: number) => { await act(async () => { await new Promise((resolve) => setTimeout(resolve, ms)); }); };
try {
store.clear();
writeMonitorPanels([{ id: "burst", name: "Before", description: "", hostIds: ["h"], view: "list", interval: 5, range: 15, sort: "manual" }]);
await act(async () => { renderer = create(React.createElement(Probe)); });
await wait(500);
const initial = collects;
assert.ok(initial > 0, "startup still collects a real sync baseline");
for (let i = 0; i < 10; i++) {
await act(async () => {
writeMonitorPanels([{ id: "burst", name: `Edit ${i}`, description: "", hostIds: ["h"], view: "list", interval: 5, range: 15, sort: "manual" }]);
await new Promise((resolve) => setTimeout(resolve, 5));
});
}
assert.equal(collects, initial, "editing must not repeatedly collect all sync data");
await wait(350);
assert.equal(collects, initial + 1, "the final edit updates the pending indicator once");
assert.equal(pending.at(-1), true);
writeMonitorPanels([]);
await wait(10);
await act(async () => { renderer!.update(React.createElement(Probe, { enabled: false })); });
const disabled = collects;
await wait(350);
assert.equal(collects, disabled, "disabling sync cancels the pending collection");
assert.equal(pending.at(-1), false);
await act(async () => { renderer!.update(React.createElement(Probe)); });
await wait(350);
let releaseCollection!: () => void;
collectBarrier = new Promise<void>((resolve) => { releaseCollection = resolve; });
const beforeInflight = collects;
writeMonitorPanels([{ id: "burst", name: "New edit", description: "", hostIds: ["h"], view: "list", interval: 5, range: 15, sort: "manual" }]);
await wait(10);
await wait(350);
assert.equal(collects, beforeInflight + 1, "the pending collection has started");
await act(async () => { renderer!.update(React.createElement(Probe, { enabled: false })); });
await act(async () => { releaseCollection(); });
assert.equal(pending.at(-1), false, "an old collection cannot re-enable the indicator after disabling sync");
} finally {
await act(async () => renderer?.unmount());
Reflect.deleteProperty(window, "netcatty");
Object.assign(manager, originals);
}
});
+17 -2
View File
@@ -603,7 +603,7 @@ export const useAutoSync = (config: AutoSyncConfig) => {
getDataHashRef.current = getDataHash;
}, [getDataHash]);
const refreshPendingLocalSync = useCallback(async () => {
const refreshPendingLocalSync = useCallback(async (isCurrent = () => true) => {
if (
!enabled
|| convergentSyncPaused
@@ -618,6 +618,7 @@ export const useAutoSync = (config: AutoSyncConfig) => {
}
try {
const currentHash = await getDataHashRef.current();
if (!isCurrent()) return;
const hashDecision = resolveAutoSyncHashDecision({
currentHash,
lastSyncedHash: lastSyncedDataRef.current,
@@ -1148,13 +1149,27 @@ export const useAutoSync = (config: AutoSyncConfig) => {
// Reflect unsynced local edits in the top-bar cloud indicator.
useEffect(() => {
void refreshPendingLocalSync();
if (!enabled || convergentSyncPaused || !sync.hasAnyConnectedProvider || !sync.isUnlocked) {
manager.setPendingLocalSync(false);
return;
}
// Collecting the pending hash builds the same full payload as an upload.
// Let rapid edits settle before doing that work, even with auto-sync off.
let cancelled = false;
const timer = setTimeout(() => {
void refreshPendingLocalSync(() => !cancelled);
}, 200);
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [
refreshPendingLocalSync,
getDataHash,
sync.hasAnyConnectedProvider,
sync.isUnlocked,
sync.isSyncing,
convergentSyncPaused,
config.settingsVersion,
enabled,
bookmarksVersion,
@@ -0,0 +1,18 @@
# Pending sync indicator performance
The pending-local-sync indicator independently collected and serialized the full sync payload after every local edit. Upload debounce did not cover this path. New monitor panel and portable chat changes also trigger it. No competing open repair was found during the 2026-10-18 audit.
A production React hook in an isolated Electron renderer, with 2,000 synthetic hosts and mocked cloud network edges, received 50 monitor-name edits spaced 5 ms apart:
| Checkpoint | Before | After |
| --- | --- | --- |
| Collections during the edit burst | 50 | 0 |
| Collections for the settled indicator | 0 additional | 1 |
| Uploads after the existing upload debounce | 1 | 1 |
| Pending indicator after edits | true | true |
The indicator now waits 200 ms after the latest change before collecting. Disabling, locking, losing the provider, pausing, or unmounting cancels pending work; stale in-flight indicator results are ignored. Startup baseline collection and explicit/manual sync keep their existing timing. The change does not alter cloud payloads or conflict/merge decisions.
`application/state/useAutoSync.monitorPanels.test.ts` exercises the actual hook, adapter notifications, and sidecar collection boundary. It failed before the fix (13 collections vs 3 startup collections after ten edits), and passes after the fix. It also checks settled pending state, cancellation before collection, and cancellation while collection is in flight. Existing panel create/edit/delete upload and sync baseline tests continue to pass.
Validation: 47 related tests passed, full lint has zero errors (eight existing warnings), and production build passed. Full-platform CI must be checked at the final PR head. The fixture does not claim to reproduce the Windows long-running terminal report or to benchmark a real cloud provider.