we have reactivity
parent
accf1fd1dd
commit
84ef946281
@ -0,0 +1,56 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue