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/cleanup-action-during-teardown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/signals": patch
---

Allow calling an action from a cleanup during teardown, as #4165 allowed writes. Removing a keyed row ran its cleanups inside the list's pass, so an action invoked from an `onCleanup` or an effect's returned cleanup threw `ACTION_CALLED_IN_OWNED_SCOPE` in development, while the same cleanup under a branch switching away ran. The dev-only action guard now stands down while cleanups run, like the owned-scope write guard. Production builds are unchanged.
13 changes: 10 additions & 3 deletions packages/signals/src/core/action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CONFIG_CHILDREN_FORBIDDEN } from "./constants.js";
import { isThenable } from "./async.js";
import { enterCallback, exitCallback, inRefCallback } from "./core.js";
import { disposalDepth, enterCallback, exitCallback, inRefCallback } from "./core.js";
import { getOwner } from "./owner.js";
import type { Owner } from "./types.js";
import { emitDiagnostic } from "./dev.js";
Expand Down Expand Up @@ -136,10 +136,17 @@ export function action<Args extends any[], Y, R>(
// and a computation tracking what its action writes livelocks (every
// write retriggers the compute, which fires a fresh invocation). Same
// scope test as setSignal: leaf imperative scopes (tracked effects,
// onSettled) stay legal.
// onSettled) stay legal, and so do cleanups during teardown — a cleanup
// is not part of the computation disposing its owner (a list pass
// removing a row), so invoking an action there is imperative (#4165).
if (__DEV__) {
const owner = getOwner();
if (owner && !(owner._config & CONFIG_CHILDREN_FORBIDDEN) && !inRefCallback()) {
if (
owner &&
!(owner._config & CONFIG_CHILDREN_FORBIDDEN) &&
!disposalDepth &&
!inRefCallback()
) {
emitDiagnostic({
code: "ACTION_CALLED_IN_OWNED_SCOPE",
kind: "write",
Expand Down
118 changes: 118 additions & 0 deletions packages/signals/tests/for-row-cleanup-action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* #4165 let a cleanup write during teardown: a cleanup is teardown, not part
* of whichever computation is disposing its owner. Invoking an action from a
* cleanup is the same imperative act, so the action guard stands down there
* too. Without it, removing a keyed row whose cleanup calls an action threw
* ACTION_CALLED_IN_OWNED_SCOPE in development, while the same cleanup under a
* branch switching away ran.
*/
import { afterEach, expect, it } from "vitest";
import {
action,
createEffect,
createMemo,
createRenderEffect,
createRoot,
createSignal,
flush,
mapArray,
onCleanup,
untrack
} from "../src/index.js";

afterEach(() => flush());

it("a removed row's onCleanup can call an action", async () => {
const [rows, setRows] = createSignal([1, 2]);
const [count, setCount] = createSignal(0);
const release = action(function* () {
setCount(n => n + 1);
});
const seen: unknown[] = [];
createRoot(() => {
const mapped = mapArray(rows, r => {
onCleanup(() => void release());
return r;
});
createRenderEffect(
() => mapped(),
v => void seen.push([...v])
);
});
flush();
setRows([2]);
flush();
await Promise.resolve();
flush();
setRows([2, 3]);
flush();
expect(seen).toEqual([[1, 2], [2], [2, 3]]);
expect(count()).toBe(1);
});

it("a removed row's effect-returned cleanup can call an action", async () => {
const [rows, setRows] = createSignal([1, 2]);
const [count, setCount] = createSignal(0);
const release = action(function* () {
setCount(n => n + 1);
});
createRoot(() => {
const mapped = mapArray(rows, r => {
createEffect(
() => r,
() => () => void release()
);
return r;
});
createRenderEffect(mapped, () => {});
});
flush();
setRows([2]);
flush();
await Promise.resolve();
flush();
expect(count()).toBe(1);
});

it("a branch's cleanup can call an action when the branch switches away", async () => {
const [on, setOn] = createSignal(true);
const [count, setCount] = createSignal(0);
const release = action(function* () {
setCount(n => n + 1);
});
createRoot(() => {
const branch = createMemo(() => {
if (!on()) return null;
return untrack(() => {
onCleanup(() => void release());
return 1;
});
});
createRenderEffect(branch, () => {});
});
flush();
setOn(false);
flush();
await Promise.resolve();
flush();
expect(count()).toBe(1);
});

it("still rejects an action called in a row's mapping body", () => {
const [rows] = createSignal([1]);
const noop = action(function* () {});
let caught: unknown;
createRoot(() => {
const mapped = mapArray(rows, r => {
try {
noop();
} catch (e) {
caught = e;
}
return r;
});
createRenderEffect(mapped, () => {});
});
flush();
expect(String(caught)).toMatch(/ACTION_CALLED_IN_OWNED_SCOPE/);
});
44 changes: 44 additions & 0 deletions packages/web/test/for-row-cleanup-action.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @jsxImportSource @solidjs/web
* @vitest-environment jsdom
*/
// #4165 let a removed <For> row's cleanup write; invoking an action from the
// same cleanup is the same imperative act and must not be rejected as a call
// in the list's owned scope.
import { afterEach, expect, test } from "vitest";
import { action, createSignal, flush, For, onCleanup, onSettled, resetErrorHalt } from "solid-js";
import { render } from "../src/index.js";

afterEach(() => resetErrorHalt());

for (const [label, register] of [
["onSettled cleanup", (fn: () => void) => onSettled(() => fn)],
["onCleanup", (fn: () => void) => onCleanup(fn)]
] as const)
test(`a row's ${label} can call an action when the row is removed`, async () => {
const [rows, setRows] = createSignal([1, 2]);
const [released, setReleased] = createSignal<number[]>([]);
const release = action(function* (n: number) {
yield Promise.resolve();
setReleased(r => [...r, n]);
});
function Row(props: { n: number }) {
register(() => void release(props.n));
return <i>{props.n}</i>;
}
const div = document.createElement("div");
const dispose = render(() => <For each={rows()}>{n => <Row n={n} />}</For>, div);
flush();
expect(div.textContent).toBe("12");
setRows([2]);
flush();
await Promise.resolve();
await Promise.resolve();
flush();
expect(div.textContent).toBe("2");
expect(released()).toEqual([1]);
setRows([2, 3]);
flush();
expect(div.textContent).toBe("23");
dispose();
});
Loading