import type { Value } from './types'; import type { AST } from './ast'; export type StoreEntry = { value: Value, body: AST, dependencies: Set; }; export type Store = Map; export function createStore(): Store { return new Map(); } let currentlyEvaluating: string | null = null; let currentDependencies: Set | null = null; export function startTracking(name: string): Set { currentlyEvaluating = name; currentDependencies = new Set(); return currentDependencies; } export function stopTracking() { currentlyEvaluating = null; currentDependencies = null; } export function recordDependency(name: string) { if (currentDependencies && name !== currentlyEvaluating) { currentDependencies.add(name); } } export function isTracking(): boolean { return currentlyEvaluating !== null; } export function buildDependents(store: Store): Map> { const dependents = new Map>(); for (const name of store.keys()) { dependents.set(name, new Set()); } for (const [name, entry] of store) { for (const dep of entry.dependencies) { if (dependents.has(dep)) { dependents.get(dep)!.add(name); } } } return dependents; }