Writing to disk on syncToAst
This commit is contained in:
parent
9555f93dd5
commit
5c75d30e44
1 changed files with 17 additions and 2 deletions
|
|
@ -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`);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue