You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB
TypeScript

import type { Value } from './types';
import type { AST } from './ast';
export type StoreEntry = {
value: Value,
body: AST,
dependencies: Set<string>;
};
export type Store = Map<string, StoreEntry>;
export function createStore(): Store {
return new Map();
}
let currentlyEvaluating: string | null = null;
let currentDependencies: Set<string> | null = null;
export function startTracking(name: string): Set<string> {
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<string, Set<string>> {
const dependents = new Map<string, Set<string>>();
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;
}