Writing to disk on syncToAst

This commit is contained in:
Dustin Swan 2026-04-02 17:17:33 -06:00
parent 9555f93dd5
commit 5c75d30e44
No known key found for this signature in database
GPG key ID: 30D46587E2100467

View file

@ -376,8 +376,10 @@ function valueToAst(value: any): AST {
throw new Error(`Cannot convert to AST: ${typeof value}`); throw new Error(`Cannot convert to AST: ${typeof value}`);
} }
let syncTimer: any = null;
let dirtyModules = new Set<string>();
export function syncToAst(name: string) { export function syncToAst(name: string) {
// if (definitions.has(name)) {
if (name in store) { if (name in store) {
const existing = definitions.get(name); const existing = definitions.get(name);
const newDef: Definition = { const newDef: Definition = {
@ -388,9 +390,20 @@ export function syncToAst(name: string) {
module: existing?.module, module: existing?.module,
}; };
definitions.set(name, newDef); // valueToAst(store[name])); definitions.set(name, newDef); // valueToAst(store[name]));
// const source = prettyPrint({ kind: 'definition', name, body: definitions.get(name)! });
const source = prettyPrint(definitions.get(name)!); const source = prettyPrint(definitions.get(name)!);
appendChangeLog(name, source); 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' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename: def.module, content }) body: JSON.stringify({ filename: def.module, content })
}); });
console.log(`Wrote ${name} to ${def.module}.cg`);
} else { } else {
const content = prettyPrint(def) + '\n'; const content = prettyPrint(def) + '\n';
fetch('/api/save', { fetch('/api/save', {
@ -451,5 +465,6 @@ function syncToFilesystem(name: string) {
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename: name, content }) body: JSON.stringify({ filename: name, content })
}); });
console.log(`Wrote ${name} to ${name}.cg`);
} }
} }