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

Aberta
#75 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
32/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
javascript, typescript

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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/.

Linguagem predominante
TypeScript
Estrelas
127
Forks
7
Merge médio
15h 42min
PRs com merge (30d)
63

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de solidjs/reactivescript

Todas as issues de solidjs/reactivescript

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.