From 33102f6a3e748a511dd6795be549ffef19db73e1 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Thu, 10 Jul 2025 16:27:35 +0200 Subject: [PATCH] fix(sync-md): fix window focus immediate sync trigger Store pendingMdToSpSync config in local variable before calling handleMdToSpSync to prevent race conditions when window gains focus during debounce period. --- .../src/background/sync/sync-manager.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/plugin-dev/sync-md/src/background/sync/sync-manager.ts b/packages/plugin-dev/sync-md/src/background/sync/sync-manager.ts index fdad730ede..4aedec1d8f 100644 --- a/packages/plugin-dev/sync-md/src/background/sync/sync-manager.ts +++ b/packages/plugin-dev/sync-md/src/background/sync/sync-manager.ts @@ -74,6 +74,7 @@ const performInitialSync = async (config: LocalUserCfg): Promise => { const handleFileChange = (config: LocalUserCfg): void => { if (mdToSpDebounceTimer) { + console.log('[sync-md] Clearing existing MD to SP debounce timer'); clearTimeout(mdToSpDebounceTimer); } @@ -82,16 +83,22 @@ const handleFileChange = (config: LocalUserCfg): void => { // Always use 10 second debounce for MD to SP sync console.log( - `[sync-md] File change detected, debouncing for ${SYNC_DEBOUNCE_MS_MD_TO_SP}ms (10 seconds)`, + `[sync-md] File change detected, debouncing for ${SYNC_DEBOUNCE_MS_MD_TO_SP}ms (10 seconds), window focused: ${isWindowFocused}`, ); mdToSpDebounceTimer = setTimeout(() => { + console.log('[sync-md] MD to SP debounce timer fired, executing sync'); handleMdToSpSync(config); }, SYNC_DEBOUNCE_MS_MD_TO_SP); }; const handleMdToSpSync = async (config: LocalUserCfg): Promise => { - if (syncInProgress) return; + if (syncInProgress) { + console.log('[sync-md] MD to SP sync skipped - sync already in progress'); + return; + } + + console.log('[sync-md] Starting MD to SP sync'); syncInProgress = true; pendingMdToSpSync = null; mdToSpDebounceTimer = null; @@ -101,6 +108,7 @@ const handleMdToSpSync = async (config: LocalUserCfg): Promise => { if (content) { // Use the project ID from config, fallback to default const projectId = config.projectId; + console.log(`[sync-md] Executing mdToSp for project: ${projectId}`); await mdToSp(content, projectId); lastSyncTime = new Date(); @@ -198,8 +206,13 @@ const setupWindowFocusTracking = (): void => { clearTimeout(mdToSpDebounceTimer); mdToSpDebounceTimer = null; - // Trigger the sync immediately - handleMdToSpSync(pendingMdToSpSync); + // Trigger the sync immediately with the stored config + const configToSync = pendingMdToSpSync; + handleMdToSpSync(configToSync); + } else { + console.log( + `[sync-md] Window focus conditions: focused=${isFocused}, wasFocused=${wasFocused}, pendingSync=${!!pendingMdToSpSync}, timer=${!!mdToSpDebounceTimer}`, + ); } }); };