Skip to content

Commit

Permalink
Stabilize computed that calls set
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Sep 19, 2024
1 parent 96bbf4c commit 7e49d0e
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,24 @@ export class ComputedNode<T> extends SignalNode<T | ComputedSpecialValues> imple
this.computing = true;
let value: T | ComputedSpecialValues;
const prevActiveConsumer = setActiveConsumer(this);
try {
value = this.computeFn.call(this.wrapper);
} catch (error) {
value = COMPUTED_ERRORED;
this.error = error;
let isUpToDate = false;
let iterations = 0;
while (!isUpToDate && iterations < 1000) {
try {
value = this.computeFn.call(this.wrapper);
} catch (error) {
value = COMPUTED_ERRORED;
this.error = error;
}
this.#removeUnusedProducers();
iterations++;
isUpToDate = this.#areProducersUpToDate();
}
this.#removeUnusedProducers();
setActiveConsumer(prevActiveConsumer);
if (!isUpToDate) {
value = COMPUTED_ERRORED;
this.error = new Error('Could not stabilize the computation.');
}
this.computing = false;
this.dirty = false;
this.set(value, false);
Expand Down

0 comments on commit 7e49d0e

Please sign in to comment.