Skip to content

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

Open

Description

@cgaaf

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.

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions