Repository navigation
fix: don't use onCleanup or create primitives inside ref callbacks #1144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,619
−119
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2f75087
fix(form): don't use onCleanup or create primitives inside ref callbacks
davedbase 2fc6337
fix(form): keep per-element state when one bind() callback is attache…
davedbase bde4588
chore(form): shorten comments
davedbase 25cf1bc
fix(form): create the ownerless sync root detached from the ref callb…
davedbase 6457c24
fix: don't use onCleanup or create primitives inside ref callbacks (a…
davedbase c4d64be
fix: address CodeRabbit review on the ref-callback PR
davedbase 59c42d1
fix: stop test globals leaking between files and fix the JSR type check
davedbase aa40140
docs(mediastream): show a waiting state in the createScreen example
davedbase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@solid-primitives/form": patch | ||
| --- | ||
|
|
||
| Fix `form.ref` and `form.bind()` throwing `CLEANUP_IN_FORBIDDEN_SCOPE` (and `PRIMITIVE_IN_EFFECT_CALLBACK`) when used as JSX `ref` callbacks (#1143). | ||
|
|
||
| Solid 2 runs a `ref` callback in a restricted context where `onCleanup` and creating roots, memos or effects are forbidden. `ref` and `bind` registered their listeners with `makeEventListener` (which calls `onCleanup`) and `bind` created its DOM-sync effect inside the callback. In a hydrated app the throw aborted hydration, so the submit handler was never attached and a native GET submission put the fields, including passwords, into the URL. | ||
|
|
||
| Listeners are now plain `addEventListener` calls inside the ref callback, which still returns its cleanup. The field → DOM sync effect is created when `form.bind("name")` is evaluated (the component body), and the listeners are also torn down with the owner of `createForm` / `bind()`, because the runtime ignores a ref callback's return value. The unused `@solid-primitives/event-listener` dependency is removed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { createRoot, flush } from "solid-js"; | ||
| import { createForm } from "../src/index.js"; | ||
|
|
||
| // One bind() callback attached to several elements: each keeps its own state and lifetime. | ||
|
|
||
| const input = (type?: string) => { | ||
| const el = document.createElement("input"); | ||
| if (type) el.type = type; | ||
| return el; | ||
| }; | ||
|
|
||
| describe("one bind() callback attached to several elements", () => { | ||
| it("keeps every attached element in sync with the field", () => { | ||
| createRoot(dispose => { | ||
| const form = createForm({ fields: { email: { initial: "x" } } }); | ||
| const attach = form.bind("email"); | ||
| const a = input(); | ||
| const b = input(); | ||
| attach(a); | ||
| attach(b); | ||
| expect([a.value, b.value]).toEqual(["x", "x"]); | ||
|
|
||
| form.fields.email.setValue("y"); | ||
| flush(); | ||
| expect([a.value, b.value]).toEqual(["y", "y"]); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it("each element writes to the field through its own listener", () => { | ||
| createRoot(dispose => { | ||
| const form = createForm({ fields: { email: { initial: "" } } }); | ||
| const attach = form.bind("email"); | ||
| const a = input(); | ||
| const b = input(); | ||
| attach(a); | ||
| attach(b); | ||
|
|
||
| a.value = "from-a"; | ||
| a.dispatchEvent(new Event("input")); | ||
| flush(); | ||
| expect(form.fields.email.value()).toBe("from-a"); | ||
|
|
||
| b.value = "from-b"; | ||
| b.dispatchEvent(new Event("input")); | ||
| flush(); | ||
| expect(form.fields.email.value()).toBe("from-b"); | ||
| // and the other element follows | ||
| expect(a.value).toBe("from-b"); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it("each listener uses its own element's classification (checkable vs text)", () => { | ||
| createRoot(dispose => { | ||
| const form = createForm({ fields: { v: { initial: "" as any } } }); | ||
| const attach = form.bind("v"); | ||
| const box = input("checkbox"); | ||
| const text = input(); | ||
| attach(box); | ||
| attach(text); | ||
|
|
||
| box.checked = true; | ||
| box.dispatchEvent(new Event("change")); | ||
| flush(); | ||
| expect(form.fields.v.value()).toBe(true); | ||
|
|
||
| text.value = "typed"; | ||
| text.dispatchEvent(new Event("input")); | ||
| flush(); | ||
| expect(form.fields.v.value()).toBe("typed"); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it("cleaning up one element leaves the others attached and in sync", () => { | ||
| createRoot(dispose => { | ||
| const form = createForm({ fields: { email: { initial: "x" } } }); | ||
| const attach = form.bind("email"); | ||
| const a = input(); | ||
| const b = input(); | ||
| const offA = attach(a); | ||
| attach(b); | ||
|
|
||
| offA(); | ||
| form.fields.email.setValue("z"); | ||
| flush(); | ||
| expect(b.value).toBe("z"); | ||
| expect(a.value).toBe("x"); // detached: no longer driven by the field | ||
|
|
||
| a.value = "ignored"; | ||
| a.dispatchEvent(new Event("input")); | ||
| flush(); | ||
| expect(form.fields.email.value()).toBe("z"); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it("disposing the owner removes every attachment", () => { | ||
| let form!: ReturnType<typeof createForm<{ email: { initial: string } }>>; | ||
| const a = input(); | ||
| const b = input(); | ||
| createRoot(dispose => { | ||
| form = createForm({ fields: { email: { initial: "" } } }); | ||
| const attach = form.bind("email"); | ||
| attach(a); | ||
| attach(b); | ||
| dispose(); | ||
| }); | ||
|
|
||
| for (const el of [a, b]) { | ||
| el.value = "after-dispose"; | ||
| el.dispatchEvent(new Event("input")); | ||
| el.dispatchEvent(new Event("blur")); | ||
| } | ||
| flush(); | ||
| expect(form.fields.email.value()).toBe(""); | ||
| expect(form.fields.email.touched()).toBe(false); | ||
| }); | ||
|
|
||
| describe("without an owner", () => { | ||
| // bind() called with no owner: the field -> DOM effect lives in a root tied to the attachments. | ||
| const ownerless = () => { | ||
| let form!: ReturnType<typeof createForm<{ email: { initial: string } }>>; | ||
| const dispose = createRoot(d => { | ||
| form = createForm({ fields: { email: { initial: "x" } } }); | ||
| return d; | ||
| }); | ||
| return { form, dispose, attach: form.bind("email") }; | ||
| }; | ||
|
|
||
| it("cleaning up one element does not stop synchronization for the others", () => { | ||
| const { form, dispose, attach } = ownerless(); | ||
| const a = input(); | ||
| const b = input(); | ||
| const offA = attach(a); | ||
| attach(b); | ||
|
|
||
| offA(); | ||
| form.fields.email.setValue("z"); | ||
| flush(); | ||
| expect(b.value).toBe("z"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("stops synchronizing after the last element is cleaned up, and resumes on re-attach", () => { | ||
| const { form, dispose, attach } = ownerless(); | ||
| const a = input(); | ||
| const offA = attach(a); | ||
| offA(); | ||
|
|
||
| form.fields.email.setValue("while-detached"); | ||
| flush(); | ||
| expect(a.value).toBe("x"); // root disposed: nothing drives the detached element | ||
|
|
||
| const b = input(); | ||
| attach(b); | ||
| expect(b.value).toBe("while-detached"); // initial sync on attach | ||
| form.fields.email.setValue("again"); | ||
| flush(); | ||
| expect(b.value).toBe("again"); // root recreated | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("calling a cleanup twice does not dispose another element's synchronization", () => { | ||
| const { form, dispose, attach } = ownerless(); | ||
| const a = input(); | ||
| const b = input(); | ||
| const offA = attach(a); | ||
| attach(b); | ||
|
|
||
| offA(); | ||
| offA(); | ||
| form.fields.email.setValue("still-synced"); | ||
| flush(); | ||
| expect(b.value).toBe("still-synced"); | ||
| dispose(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.