Skip to content
Closed
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/fix-lane-derivation-prev-3955.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/signals": patch
---

A `createSignal(fn)` or `createMemo(fn)` derivation that reads `latest()` or `isPending()` of a signal an in-flight async memo is holding receives its previous value as `prev` on every re-run under the hold. When its first pass under the hold was equal to the previous value, the next pass received an empty internal marker instead, so `(prev = []) => [...prev]` threw `prev is not iterable` and halted the reactive system.
6 changes: 3 additions & 3 deletions packages/signals/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
// — here, the finally below, updateIfNecessary — carries it (#3543).
el._flags = REACTIVE_RECOMPUTING_DEPS | (el._flags & REACTIVE_ZOMBIE);
el._time = clock;
// The pass's previous value: its staging, else the lane's value for a
// lane's node (lanes.ts), else the committed one.
// The pass's previous value: its staging, else a lane node's lane value
// when it has one (lanes.ts), else the committed one.
let value =
el._pendingValue !== NOT_PENDING
? el._pendingValue
: el._config & CONFIG_OVERRIDE
: el._config & CONFIG_OVERRIDE && el._x!._lane !== NOT_PENDING
? el._x!._lane
: el._value;
let oldHeight = el._height;
Expand Down
80 changes: 80 additions & 0 deletions packages/signals/tests/held-latest-derivation-prev-3955.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { afterEach, describe, expect, it } from "vitest";
import {
createMemo,
createRenderEffect,
createRoot,
createSignal,
flush,
isPending,
latest,
resetErrorHalt
} from "../src/index.js";

function deferred<T = void>() {
let resolve!: (v: T) => void;
const promise = new Promise<T>(r => (resolve = r));
return { promise, resolve };
}

async function tick(n = 4) {
for (let i = 0; i < n; i++) await Promise.resolve();
flush();
}

describe("a derivation reading a held signal through latest()/isPending() keeps its prev", () => {
afterEach(() => {
resetErrorHalt();
});

for (const kind of ["createSignal", "createMemo"] as const) {
for (const read of ["latest", "isPending"] as const) {
it(`${kind} + ${read}(): an equal pass under the hold does not turn the next pass's prev into NOT_PENDING`, async () => {
const prevs: unknown[] = [];
const gates = { current: deferred() };
let setFilter!: (v: string) => void;
let dispose!: () => void;
createRoot(d => {
dispose = d;
const [filter, set] = createSignal("a");
setFilter = set;
const data = createMemo(async () => {
const f = filter();
await gates.current.promise;
return f;
});
createRenderEffect(data, () => {});
const compute = (prev: string[] = []) => {
read === "latest" ? latest(filter) : isPending(filter);
prevs.push(prev);
return [...prev];
};
const equals = (a: string[], b: string[]) => a.length === b.length;
const names =
kind === "createSignal"
? createSignal<string[]>(compute, { equals })[0]
: createMemo<string[]>(compute, { equals });
createRenderEffect(names, () => {});
});
flush();
const mountGate = gates.current;
gates.current = deferred();
mountGate.resolve();
await tick();
expect(prevs).toEqual([[]]);

setFilter("b");
flush();
setFilter("c");
expect(() => flush()).not.toThrow();
await tick();
expect(prevs).toEqual([[], [], []]);

gates.current.resolve();
for (let i = 0; i < 4; i++) await Promise.resolve();
expect(() => flush()).not.toThrow();
expect(prevs).toEqual(read === "isPending" ? [[], [], [], []] : [[], [], []]);
dispose();
});
}
}
});
Loading