Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/memo-untracked-cold-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@retreejs/core": patch
---

A `@memo` or `@fnMemo` read inside `Retree.untracked` no longer subscribes the surrounding selector or effect to its key's reads. Before, a memo whose key had to re-run (on its first read, or after a key input changed) leaked those reads, so whether a selector stayed asleep depended on what had warmed the memo.
5 changes: 5 additions & 0 deletions packages/retree-core/src/internals/dependency-tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,11 @@ export function replayDependencyComparisonAccesses(
comparisons: unknown[],
comparisonValues?: readonly (readonly unknown[])[]
): void {
// Paused by `Retree.untracked` or a memo body: the memo's own frame
// tracked these reads, and the paused caller must not inherit them.
if (!isDependencyTrackingActive()) {
return;
}
const currentFrame =
dependencyAccessStack[dependencyAccessStack.length - 1];
if (currentFrame === undefined) {
Expand Down
2 changes: 0 additions & 2 deletions packages/retree-core/src/internals/memo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
collectDependencyComparisonAccesses,
createNodeVersionRead,
replayDependencyComparisonAccesses,
isDependencyTrackingActive,
runMemoBody,
} from "./dependency-tracking.js";
import { getDependencyComparisonValues } from "./dependencies.js";
Expand Down Expand Up @@ -172,7 +171,6 @@ function validateTrappedMemo(entry: IMemoCacheEntry): boolean {
}

function replayTrappedMemo(entry: IMemoCacheEntry): void {
if (!isDependencyTrackingActive()) return;
if (entry.comparisonAccessors === undefined) return;
replayDependencyComparisonAccesses(
entry.comparisonAccessors,
Expand Down
44 changes: 44 additions & 0 deletions packages/retree-core/src/memo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2366,3 +2366,47 @@ describe("reads through memos with hidden bodies", () => {
expect(root.consumer.value).toBe("Grace");
});
});

describe("memo reads under Retree.untracked", () => {
class Inner extends ReactiveNode {
public a: object | null = null;
get dependencies() {
return [];
}
}
class Outer extends ReactiveNode {
public inner = new Inner();
get dependencies() {
return [];
}
@memo((self: Outer) => [self.inner.a])
get memoized() {
return this.inner.a;
}
}
type TTrack = (run: () => void) => () => void;
const select: TTrack = (run) => Retree.select(run, () => {});
const effect: TTrack = (run) => Retree.effect(run);

it.each([
["cold", "Retree.select", select],
["cold", "Retree.effect", effect],
["stale", "Retree.select", select],
])("keeps a %s keyed memo's key reads out of %s", (state, _, track) => {
const root = trackRoot(Retree.root(new Outer()));
if (state === "stale") {
void root.memoized;
root.inner.a = {};
}
let runs = 0;
const stop = track(() => {
runs++;
Retree.untracked(() => root.memoized);
});

root.inner.a = {};
stop();

expect(runs).toBe(1);
});
});
Loading