Skip to content

fix: better binding interop between runes/non-runes components #12123

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 7 commits into from
Jun 21, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jun 21, 2024

Ensure binding from legacy component passed to runes component updates correctly. This is done by also using the prop(..) variant for a property if it's mutated in runes mode, and then figuring out at runtime whether or not the parent should be notified or not

fixes #12032

Also:

  • fixes an adjacent bug I came across: console.log(foo.bar = true) did not log true
  • fixes an adjacent bug where rest on let { foo, ...rest } = $props() contained private properties like $$slots etc
  • refactored the knowledge of "is this a prop(..) prop" into a common function

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

…s correctly. This is done by also using the `prop(..)` variant for a property if it's mutated in runes mode, and then figuring out at runtime whether or not the parent should be notified or not

fixes #12032
Copy link

changeset-bot bot commented Jun 21, 2024

🦋 Changeset detected

Latest commit: 5415cc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

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

@Rich-Harris
Copy link
Member

IIUC in the mutation case we're always calling the prop function with the value of calling itself:

stuff(stuff().blah += 1, stuff());

Given that the prop already knows its own value, would it be simpler to do this?

stuff(stuff().blah += 1, true);

Then the function returned from prop would be like this:

return function (/** @type {any} */ value, mutation) {
  if (arguments.length > 0) {
    // We don't want to notify if the value was mutated and the parent is in runes mode.
    // In that case the state proxy (if it exists) should take care of the notification.
    // If the parent is not in runes mode, we need to notify on mutation, too, that the prop
    // has changed because the parent will not be able to detect the change otherwise.
    if (!runes || !mutation || legacy_parent) {
      // upon reassignment only the first argument is used
      /** @type {Function} */ (setter)(mutation ? getter() : value);
    }
    return value;
  }
  
  return getter();
};

@dummdidumm
Copy link
Member Author

good catch, simplified

@Rich-Harris Rich-Harris merged commit afe8445 into main Jun 21, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the binding-interop branch June 21, 2024 19:44
FoHoOV pushed a commit to FoHoOV/svelte that referenced this pull request Jun 27, 2024
…ejs#12123)

* Ensure binding from legacy component passed to runes component updates correctly. This is done by also using the `prop(..)` variant for a property if it's mutated in runes mode, and then figuring out at runtime whether or not the parent should be notified or not
fixes sveltejs#12032

* fix adjacent bug around wrong value getting return upon mutation

* deduplicate

* changeset

* simplify

* move comment

* rename

---------

Co-authored-by: Rich Harris <[email protected]>
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.

Svelte 5 reactivity breaks using objects, when parent is not using runes mode
2 participants