Extract common evaluation logic in Workspace

The `Workspace` class previously duplicated the logic around evaluating a document and handling its recursive dependency protection set (`this.resolving`) inside both `evaluateSheet` and `exportsFor`.

This patch abstracts this behavior into a new private method, `doEvaluate`, simplifying the public and internal entry points for sheet evaluation and improving overall code health and maintainability.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-08-09 10:23:25 +00:00
parent c958f30662
commit 1886535ab0

View file

@ -32,8 +32,7 @@ export class Workspace {
constructor(private engine: SumEngine, private sheets: () => SheetSource[]) {}
/** Evaluate a sheet as the user's active/open document — refs resolve live. */
evaluateSheet(sheetId: string, text: string): LineResult[] {
private doEvaluate(sheetId: string, text: string): LineResult[] {
this.resolving.add(sheetId);
try {
return this.engine.evaluateDocument(text, (sheet, key) => this.resolveXRef(sheet, key));
@ -42,6 +41,11 @@ export class Workspace {
}
}
/** Evaluate a sheet as the user's active/open document — refs resolve live. */
evaluateSheet(sheetId: string, text: string): LineResult[] {
return this.doEvaluate(sheetId, text);
}
getCachedResults(sheet: SheetSource): LineResult[] {
return this.exportsFor(sheet).results;
}
@ -145,13 +149,7 @@ export class Workspace {
private exportsFor(sheet: SheetSource): SheetExports {
const cached = this.cache.get(sheet.id);
if (cached) return cached;
this.resolving.add(sheet.id);
let results: LineResult[];
try {
results = this.engine.evaluateDocument(sheet.text, (s, k) => this.resolveXRef(s, k));
} finally {
this.resolving.delete(sheet.id);
}
const results = this.doEvaluate(sheet.id, sheet.text);
const vars = new Map<string, Value>();
for (const r of results) {
if (r.assign && r.value) vars.set(r.assign, r.value);