feat(journal): seed declared variables on initialization
Implement `computeInitialValues` to calculate the starting state of declared variables and seed them into the journal stream store during the completions initialization process.
This commit is contained in:
@@ -174,6 +174,49 @@ export function computeCascade(
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute initial values for all declared variables.
|
||||
* Called once after initReactivity() to seed the store.
|
||||
* Unset dependencies default to 0.
|
||||
*/
|
||||
export function computeInitialValues(
|
||||
currentVars: VariableStore,
|
||||
): Array<{ key: string; value: string }> {
|
||||
if (!declExprs) return [];
|
||||
|
||||
const allKeys = [...declExprs.keys()];
|
||||
const sorted = topoSortAffected(new Set(allKeys));
|
||||
|
||||
const results: Array<{ key: string; value: string }> = [];
|
||||
const localVars = { ...currentVars };
|
||||
|
||||
for (const key of sorted) {
|
||||
const expr = declExprs.get(key);
|
||||
if (!expr) continue;
|
||||
|
||||
try {
|
||||
const result = evaluateExpression(expr, {
|
||||
lookup: (name: string) => {
|
||||
const k = "$" + name;
|
||||
return localVars[k] ?? undefined;
|
||||
},
|
||||
});
|
||||
|
||||
const newValue = String(result.value);
|
||||
const finalValue = applyTagModifiersTo(key, newValue, localVars);
|
||||
|
||||
if (finalValue !== (localVars[key] ?? "")) {
|
||||
localVars[key] = finalValue;
|
||||
results.push({ key, value: finalValue });
|
||||
}
|
||||
} catch {
|
||||
// skip failed evaluations at init time
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute which tags are active given the current variable store.
|
||||
* A tag is active if any variable has that tag as its value.
|
||||
|
||||
Reference in New Issue
Block a user