Skip to content

Commit

Permalink
proxy, sheet: addProxyDependencies, implement proxy cycle check
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed Apr 3, 2024
1 parent f6da302 commit b584a28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class SheetProxy {
this.working = new Working(sh.working);
this.errors = new CellErrors(sh.errors);
if (name) this._name = name;
this._id = sh.incrementProxyCount();
this._id = sh.addProxy();
}

// a Sheet has id 0, proxies > 0
Expand Down Expand Up @@ -182,6 +182,7 @@ export class SheetProxy {
// @ts-expect-error conflict with overloaded definitions
const cell = this._sheet.map(dependencies, computeFn, name, this, noFail);
this._list.push(cell);
this._sheet.addProxyDependencies(this._id, dependencies);
return cell as MapCell<V, NF>;
}

Expand All @@ -207,6 +208,7 @@ export class SheetProxy {
noFail
);
this._list.push(cell);
this._sheet.addProxyDependencies(this._id, dependencies);
return cell;
}

Expand Down
21 changes: 19 additions & 2 deletions src/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export class Sheet {
/** Cells that can be garbage collected */
private _gc: Set<number>;

private _proxies: Graph<number>;

/**
* @param equality function comparing a new value with previous value for updates
*/
Expand All @@ -131,16 +133,31 @@ export class Sheet {
this.working = new Working();
this.errors = new CellErrors();
this._gc = new Set();
this._proxies = new Graph();
this._proxies.addNode(0);
}

// a Sheet has id 0, proxies > 0
get id() {
return 0;
}

incrementProxyCount() {
addProxy() {
// keeping 0 as original Sheet
this[proxies]++;
return this[proxies]; // keeping 0 as original Sheet
const id = this[proxies];
this._proxies.addNode(id);
return id;
}

addProxyEdge(from: number, to: number) {
this._proxies.addEdge(from, to);
}
addProxyDependencies(id: number, deps: AnyCellArray<unknown[]>) {
for (const dep of deps)
if (dep._proxy !== id) this.addProxyEdge(dep._proxy, id);
if (this._proxies.topologicalSort() === null)
this.debug(undefined, "proxy cycle", { proxy: id, deps });
}

bless(id: number, name: string) {
Expand Down

0 comments on commit b584a28

Please sign in to comment.