Is an object provision meant to couple its fields? A part's write to its own state waits for the app's action when the provision also reads a prop the action holds

未關閉
#75 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
32/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
javascript, typescript

研究方向

Start with the ReactiveScript compiler’s lowering of provide object literals and the hold behavior described in the issue; the payload does not name compiler files or tests. Reproduce the supplied c.rx sequence and compare the listed provision forms, then check the behavior against ref/ as the report notes it was not tested. Done means deciding whether provision fields should couple and documenting or implementing a consistent behavior for the cases raised.

由索引模型根據 Issue 內容生成。

描述

Found while building a component library on ReactiveScript. Written with Claude Fable 5.2 (in Claude Code), which also ran the reproductions; all of them were rerun before posting.

@reactivescript/compiler 0.3.1 through @solidjs/vite-plugin 3.0.0-next.57 on a Vite dev server, mounted with
const app = createApp({ root }); app.mount(main(app)) from @reactivescript/runtime 0.3.1, in Chromium, on
solid-js 2.0.0-rc.22 and on 2.0.0. A Playwright script clicks the buttons and reads the text of the <p>
elements.

https://github.com/solidjs/reactivescript/issues/20 was closed with:

When a write should land now, for example clearing an input, the guide covers it under "What lands together, and what waits" in guide/async.md, with onFlush (EF-8).

and asked for "a case where plain Solid and the lowered .rx actually behave differently". This is a case onFlush
does not reach, in a component library whose root part provides its state to the other parts, and the Solid
comparison is at the end.

api.js:

export const wait = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));

c.rx:

import { wait } from "./api.js"

context menu: { open: boolean; disabled: boolean }

component Popup() {
  <p>open: {(use menu).open ? "yes" : "no"}</p>
}

component Menu(props: { disabled: boolean }) {
  source open := false
  provide menu = { open: open, disabled: props.disabled }
  <div>
    <button onClick={() => { open := !open }}>toggle</button>
    <Popup />
  </div>
}

export default component App() {
  source step := 1
  action next() {
    step := 2
    try { await wait(300, 0) } catch (e) { }
  }
  <div>
    <button onClick={() => { next() }}>next</button>
    <p>step: {step}</p>
    <Menu disabled={step == 2} />
  </div>
}
step screen
mount step: 1, open: no
click toggle step: 1, open: yes
click toggle step: 1, open: no
click next step: 1, open: no
click toggle step: 1, open: no
100 ms later step: 1, open: no
the wait resolves step: 2, open: yes

The provision is one equation over open and props.disabled, and Popup reads it, so the menu's open := !open
joins the app's hold on step (H-3). Menu and Popup have no await and no action; App does not contain the
provision. In a library they are in different packages. The compiler reports nothing.

Each row is one change to c.rx, then click next, click toggle:

change at the toggle click
In Menu's handler, let o = !open; onFlush(() => { open := o }) open: no until the wait resolves
context menuOpen: boolean and context menuDisabled: boolean, provide menuOpen = open, provide menuDisabled = props.disabled, and Popup reads use menuOpen open: yes
context menu: { open: () => boolean; disabled: () => boolean }, provide menu = { open: () => open, disabled: () => props.disabled }, and Popup reads (use menu).open() open: yes
provide menu = { open: open, disabled: latest props.disabled } open: yes. With <p>disabled: {(use menu).disabled ? "yes" : "no"}</p> added to Popup, that paragraph shows disabled: yes from the click on next, beside step: 1 (this addition ran on rc.22 only)
In App, source step = 1 and guess step := 2 in place of source step := 1 and step := 2 step: 2 at the click on next, then open: yes at the toggle

An app avoids the wait by not holding a plain write, which AC-1c allows on purpose: "A write meant to be held — a
transition […] is written in a declared action".

In Solid a context value is usually an object of getters or accessors, and that shape does not couple. The page
below, on solid-js alone with the same clicks, shows open: no at the toggle click as written (one memo over the
object, which is what provide emits) and open: yes with ?getters in the URL:

<!doctype html> <button>next</button><button>toggle</button>
<p></p>
<p></p>
<script type="module">
  import { createRoot, createSignal, createMemo, createRenderEffect, action } from "solid-js";
  const getters = new URLSearchParams(location.search).has("getters");
  const [next, toggle] = document.querySelectorAll("button");
  const [stepText, openText] = document.querySelectorAll("p");
  createRoot(() => {
    const [step, setStep] = createSignal(1);
    const [open, setOpen] = createSignal(false);
    const disabled = () => step() === 2;
    // the context value: an object of getters, or one memo over the whole object (what `provide` emits)
    const menu = getters
      ? () => ({
          get open() {
            return open();
          },
          get disabled() {
            return disabled();
          },
        })
      : createMemo(() => ({ open: open(), disabled: disabled() }));
    const hold = action(function* () {
      setStep(2);
      yield new Promise((r) => setTimeout(r, 300));
    });
    next.onclick = () => hold();
    toggle.onclick = () => setOpen((o) => !o);
    createRenderEffect(step, (v) => {
      stepText.textContent = "step: " + v;
    });
    createRenderEffect(
      () => menu().open,
      (v) => {
        openText.textContent = "open: " + (v ? "yes" : "no");
      },
    );
  });
</script>

The question: is provide name = { … } meant to couple its fields, so that a write to the source behind one waits
for a hold on another? If yes, which row of the table should a component library use?

If no, one way: an object literal on the right of provide lowers each field to a getter, which is the functions
row written by the compiler. CP-6b says the compiler's facts already record "a read of one field of props or a
store"; that is compile-time knowledge, not an existing lowering. The cost, as far as I can see it:

  • the provision stops being one value (RG-7b: "makes e the context's value"), so a consumer of the whole object
    (an effect (use name), an argument to JS, a spread) needs a rule;
  • each field is evaluated per reader unless it gets its own memo, which adds nodes;
  • the literal form then differs from x = { a: e1, b: e2 } followed by provide name = x, against E-2;
  • which right-hand sides qualify (nested literals, spreads, computed keys, a conditional of two literals) has to
    be decided.

Not tested: any of the .rx programs against ref/.

主要語言
TypeScript
星號
127
分支
7
平均合併
15 小時 42 分鐘
30 天內合併 PR
63

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

solidjs/reactivescript 的其他 Issue

查看 solidjs/reactivescript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。