Skip to content

Conversation

Rich-Harris
Copy link
Member

The way we handle branches is kinda broken in an async world. We assume — quite reasonably unless you're in an async world — that it's safe to simply bail out of a block if the expression is unchanged from before. When it's time to commit a block, we commit whichever branch corresponds to the most recent invocation, which isn't correct. And it's only possible to have one offscreen branch per block. And so on. I'm pretty sure there's some memory leaks in there as well.

This PR adds a new BranchManager class, which each block type can use to control which branches are created when. It ensures that branches are correctly linked to batches, and that they're destroyed at the appropriate time. It handles all the complexities around transitions and as-yet-unresolved batches.

Currently it's used for #if, #await, #key, @render, <svelte:component> and <svelte:element>. It might make sense to use it for <svelte:boundary> as well. It does not currently power #each blocks, which are unique in that their branches aren't mutually exclusive.

I'm hoping that this will enable more work towards #16971 and related issues.

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.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

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

Copy link

changeset-bot bot commented Oct 18, 2025

🦋 Changeset detected

Latest commit: f275aa3

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

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16977

component.promise = promise;
// wait for rendering
await Promise.resolve();
await tick();
Copy link
Member Author

Choose a reason for hiding this comment

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

confession, I don't really understand why this change was necessary. but it was

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Very nice how this deduplicates the logic!

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