Skip to content

Commit

Permalink
Fix unsubscribe not properly unlinking
Browse files Browse the repository at this point in the history
  • Loading branch information
pjeby committed May 27, 2024
1 parent bf4667b commit b763327
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions specs/cells.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ describe("Consistent updates", () => {
runRules(); see("43, 45");
common();
})
it("handles conditional indirect resubscribes", () => {
// Given two rules conditionally subscribed indirectly to the same value
const condition = value(false), commonVal = value(42);
const indirect = cached(() => void log(commonVal()));
rule(() => { if (condition()) commonVal(); });
rule(() => { if (condition()) indirect()});
runRules(); see();
// When the condition is turned on...
condition.set(true); runRules(); see("42");
commonVal.set(66); runRules(); see("66");
commonVal.set(17); runRules(); see("17");
// off...
condition.set(false); runRules(); see();
commonVal.set(42); runRules(); see();
// and on again
condition.set(true); runRules(); see("42");
// Then the indirect calculation should be subscribed again
commonVal.set(66); runRules(); see("66");
});
it("with different-length paths to common element", () => {
const start = value(22);
const indirect = cached(() => start() * 1.5);
Expand Down
5 changes: 3 additions & 2 deletions src/cells.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, current, makeCtx, swapCtx } from "./ambient.ts";
import { type RuleQueue, currentRule, ruleQueue, defaultQ } from "./scheduling.ts";
import { Job, OptionalCleanup, RecalcSource } from "./types.ts"
import { detached, getJob, makeJob } from "./tracking.ts";
import { getJob, makeJob } from "./tracking.ts";
import { Connection, Inlet, IsStream, Sink, Source, backpressure } from "./streams.ts";
import { setMap } from "./utils.ts";
import { isError, isValue, markHandled } from "./results.ts";
Expand All @@ -11,7 +11,7 @@ import { nullCtx } from "./internals.ts";
* Error indicating a rule has attempted to write a value it indirectly
* depends on, or which has already been read by another rule in the current
* batch. (Also thrown when a cached function attempts to write a value at all,
* directly or inidirectly.)
* directly or indirectly.)
*
* @category Errors
*/
Expand Down Expand Up @@ -373,6 +373,7 @@ export class Cell {
if (sub.nT) sub.nT.pT = sub.pT;
if (sub.pT) sub.pT.nT = sub.nT;
if (this.subscribers === sub) this.subscribers = sub.nT;
sub.nT = sub.pT = undefined;
if (!this.subscribers) {
if (this.flags & Is.Calc) {
for(let s=this.sources; s; s = s.nS) s.src.unsubscribe(s);
Expand Down

0 comments on commit b763327

Please sign in to comment.