From 5c75d30e4427cbff64b763eb11513ea671065c32 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 2 Apr 2026 17:17:33 -0600 Subject: [PATCH] Writing to disk on syncToAst --- src/runtime-js.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/runtime-js.ts b/src/runtime-js.ts index c9ce9f7..11802c3 100644 --- a/src/runtime-js.ts +++ b/src/runtime-js.ts @@ -376,8 +376,10 @@ function valueToAst(value: any): AST { throw new Error(`Cannot convert to AST: ${typeof value}`); } +let syncTimer: any = null; +let dirtyModules = new Set(); + export function syncToAst(name: string) { - // if (definitions.has(name)) { if (name in store) { const existing = definitions.get(name); const newDef: Definition = { @@ -388,9 +390,20 @@ export function syncToAst(name: string) { module: existing?.module, }; definitions.set(name, newDef); // valueToAst(store[name])); - // const source = prettyPrint({ kind: 'definition', name, body: definitions.get(name)! }); const source = prettyPrint(definitions.get(name)!); appendChangeLog(name, source); + + // Debounce writes + dirtyModules.add(name); + if (!syncTimer) { + syncTimer = setTimeout(() => { + for (const n of dirtyModules) { + syncToFilesystem(n); + } + dirtyModules.clear(); + syncTimer = null; + }, 2000); + } } } @@ -444,6 +457,7 @@ function syncToFilesystem(name: string) { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ filename: def.module, content }) }); + console.log(`Wrote ${name} to ${def.module}.cg`); } else { const content = prettyPrint(def) + '\n'; fetch('/api/save', { @@ -451,5 +465,6 @@ function syncToFilesystem(name: string) { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ filename: name, content }) }); + console.log(`Wrote ${name} to ${name}.cg`); } }