Skip to content

Commit bb9b7db

Browse files
Copilottheoephraim
andauthored
fix(cloudflare): force restart after idle threshold even when env is unchanged
Builds on PR #631 by @shawnsw which added the debounce + cache equality check. Adds a 5-second idle threshold: if no restart has happened in the last 5 seconds, a save event will trigger a restart even when the resolved env graph is identical. This handles the case where a user saves a file again after a long idle period and expects the dev server to reload. Co-authored-by: Shawn <shawn@docnow.au>" Agent-Logs-Url: https://github.com/dmno-dev/varlock/sessions/785c3bab-96c9-480b-bcab-ec417ac2c52e Co-authored-by: theoephraim <1158956+theoephraim@users.noreply.github.com>
1 parent 677f469 commit bb9b7db

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

packages/integrations/cloudflare/src/varlock-wrangler.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,30 +412,38 @@ async function handleDev(args: Array<string>) {
412412
});
413413

414414
// watch env source files for changes and restart wrangler with fresh data
415+
const DEBOUNCE_MS = 300;
416+
// force a restart if the last save event was more than this long after the previous restart,
417+
// even when the resolved env is identical — handles repeated saves of an unchanged file
418+
const FORCE_RESTART_IDLE_MS = 5000;
415419
let restartTimeout: ReturnType<typeof setTimeout> | undefined;
416420
let cachedGraphJson = loaded.json;
421+
let lastRestartAt = 0;
417422
function scheduleRestart() {
418423
// debounce — multiple files may change at once (e.g. editor saves multiple files,
419424
// or macOS fs.watch() emits extra events for unchanged files)
420425
if (restartTimeout) clearTimeout(restartTimeout);
421426
restartTimeout = setTimeout(() => {
427+
restartTimeout = undefined;
422428
try {
423429
const freshLoaded = loadSerializedGraph();
424-
// compare serialized JSON strings — only restart when the resolved env actually changed
425-
if (freshLoaded.json === cachedGraphJson) {
426-
restartTimeout = undefined;
430+
const now = Date.now();
431+
const idleSinceLastRestart = now - lastRestartAt > FORCE_RESTART_IDLE_MS;
432+
// skip restart only when env is unchanged AND a restart happened recently
433+
if (freshLoaded.json === cachedGraphJson && !idleSinceLastRestart) {
427434
return;
428435
}
429436
cachedGraphJson = freshLoaded.json;
430437
loaded = freshLoaded;
438+
lastRestartAt = now;
431439
cachedContent = formatEnvFileContent(freshLoaded);
432440
handle.update(cachedContent);
433441
console.log('[varlock-wrangler] env changed, restarting wrangler...');
434442
wranglerChild?.kill();
435443
} catch (err) {
436444
console.error('[varlock-wrangler] failed to re-resolve env:', (err as Error).message);
437445
}
438-
}, 300);
446+
}, DEBOUNCE_MS);
439447
}
440448

441449
// set up watchers on env source files

0 commit comments

Comments
 (0)