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}`);
}
let syncTimer: any = null;
let dirtyModules = new Set<string>();
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`);
}
}