Skip to content
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

Rethink props #9826

Merged
merged 28 commits into from
Dec 7, 2023
Merged

Rethink props #9826

merged 28 commits into from
Dec 7, 2023

Conversation

Rich-Harris
Copy link
Member

Alright, this one was a real challenge. Hopefully people think it's worth it.

The goal here was threefold:

  • Make props more efficient, by reducing indirection. In chore: use $$props directly where possible #9813, we replaced function wrappers for the majority of props (those that aren't written to, and that don't have a default value). This PR takes that further — we no longer create a source for a prop unless a) it's written to and b) the prop wasn't declared with bind:. This reduces memory cost, and the overhead associated with get.
  • Eliminate the sync_effect dance. Using effects to keep these sorts of things in sync is best avoided — derived is a better model. @trueadm was particularly keen to get rid of sync_effect (I'm not sure why it's worse than other types of effects, but I believe him)
  • Make the code easier to follow. This was a bit of a stretch goal, and opinions may differ on whether this PR achieves it. But prop_source was a bit of a beast (scary variable names like ignore_next2!) and hopefully by separating out the three different scenarios (default only, bound, synced with local 'forking') it's a little bit less imposing now.

Along the way, I uncovered a few interesting bugs:

  • There's a discrepancy between top-level props (i.e. props on the top-level component) and state/non-top-level props. The top-level props aren't reactive, due (I believe?) to some quirk of the sync_effect dance. This allowed some tests to do things like log.push(...) inside an effect without triggering an infinite loop. In this PR, props and state behave more consistently, so I've rewritten those tests.
  • We had a scoping bug with slots — Svelte 5: scope is borked with slots and static props #9823 — that was masked because reading a slot prop and reading a normal prop looked the same up until this PR
  • We weren't reusing proxies when encountering a previously-proxified object — fixed in fix: reuse proxy between objects #9821
  • Arguably not a bug per se, but because (I think) of the sync_effect, objects were being proxified eagerly. Without that, some tests failed. I've updated them

Having immersed myself in this for the last however many hours, I'm more convinced than ever that accessors needs to die. It makes everything more complicated than it needs to be, and reduces the usefulness of our test suite (by changing how props are compiled, thus reducing fidelity). I'll open an issue with a sketch of an alternative.

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 Dec 7, 2023

🦋 Changeset detected

Latest commit: 9844243

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

Copy link

vercel bot commented Dec 7, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-5-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 7, 2023 10:09am

Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful

@rmunn
Copy link
Contributor

rmunn commented Dec 13, 2023

For what it's worth, I had previously tried a couple times to understand how prop_source worked, and gave up and just treated it as "magic". But now I actually understand how prop works, even the "hard mode" case. So you do seem to have achieved the goal of making the code easier to follow, at least for me.

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.

4 participants