Skip to content

chore: update sequencing inside blocks #10939

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
merged 8 commits into from
Mar 26, 2024
Merged

chore: update sequencing inside blocks #10939

merged 8 commits into from
Mar 26, 2024

Conversation

Rich-Harris
Copy link
Member

Right now, blocks are created after local updates have been applied. Given something like this...

<script>
  let number = $state(0);
</script>

<!-- 1 -->
<input type="number" bind:value={number} />

<!-- 2 -->
{#if number % 2 === 0}
  <p>{number} is even</p>
{:else}
  <p>{number} is odd</p>
{/if}

<!-- 3 -->
<p>{number} * 2 = {number * 2}</p>

...the compiler spits out something like this:

let number = $.source(0);
var fragment = $.open_frag($$anchor, frag);

/* 1 */
var input = $.first_child(fragment);
$.remove_input_attr_defaults(input);

/* 2 */
var node = $.sibling($.sibling(input, true));

/* 3 */
var p_2 = $.sibling($.sibling(node, true));
var text_2 = $.child(p_2);
$.render_effect(() => $.set_text(text_2, `${$.stringify($.get(number))} * 2 = ${$.stringify($.get(number) * 2)}`));

/* 1 */
$.bind_value(input, () => $.get(number), ($$value) => $.set(number, $$value));

/* 2 */
$.if(
  node,
  () => $.get(number) % 2 === 0,
  ($$anchor) => {
    var p = $.open($$anchor, frag_1);
    var text = $.child(p);

    $.render_effect(() => $.set_text(text, `${$.stringify($.get(number))} is even`));
    return $.close($$anchor, p);
  },
  ($$anchor) => {
    var p_1 = $.open($$anchor, frag_2);
    var text_1 = $.child(p_1);

    $.render_effect(() => $.set_text(text_1, `${$.stringify($.get(number))} is odd`));
    return $.close($$anchor, p_1);
  }
);

This is... fine, but it would perhaps be clearer if we got something like this instead:

/* 1 */
var input = $.first_child(fragment);
$.remove_input_attr_defaults(input);
$.bind_value(input, () => $.get(number), ($$value) => $.set(number, $$value));

/* 2 */
var node = $.sibling($.sibling(input, true));

$.if(
  node,
  () => $.get(number) % 2 === 0,
  ($$anchor) => {
    var p = $.open($$anchor, frag_1);
    var text = $.child(p);

    $.render_effect(() => $.set_text(text, `${$.stringify($.get(number))} is even`));
    return $.close($$anchor, p);
  },
  ($$anchor) => {
    var p_1 = $.open($$anchor, frag_2);
    var text_1 = $.child(p_1);

    $.render_effect(() => $.set_text(text_1, `${$.stringify($.get(number))} is odd`));
    return $.close($$anchor, p_1);
  }
);

/* 3 */
var p_2 = $.sibling($.sibling(node, true));
var text_2 = $.child(p_2);
$.render_effect(() => $.set_text(text_2, `${$.stringify($.get(number))} * 2 = ${$.stringify($.get(number) * 2)}`));

This PR doesn't produce exactly that order, since it turns out we need to apply actions/bindings/events after the final render_effect, otherwise things go awry. But it's very close to that ideal.

The reason this is valuable is that it should, if my hunch is correct, unlock a slightly simpler approach to hydration. (It might be worth waiting until we can actually test that theory before merging this, to avoid needless changes.)

Note that I had to compromise in one place: an action inside a block (such as an {#if ...} or {#each ...} will run before an action in an earlier element. In other words b will run before a:

<div use:a>...</div>

{#if foo}
  <div use:b>...</div>
{/if}

I don't consider that a big problem — actions ought to be independent of one another — but it's worth noting.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Mar 26, 2024

⚠️ No Changeset found

Latest commit: 637ec30

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants