Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: centralise branch management #16977

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

Open
Rich-Harris wants to merge 21 commits into main
base: main
Choose a base branch
Loading
from branch-manager
Open

chore: centralise branch management #16977

Rich-Harris wants to merge 21 commits into main from branch-manager

Conversation

Copy link
Member

@Rich-Harris Rich-Harris commented Oct 18, 2025

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
edited
Loading

🦋 Changeset detected

Latest commit: b9208db

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

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

@Rich-Harris Rich-Harris Oct 18, 2025

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!

Comment on lines +82 to +85
this.#batches.delete(batch);

for (const [b, k] of this.#batches) {
if (b === batch) break;
Copy link
Member

@dummdidumm dummdidumm Oct 18, 2025

Choose a reason for hiding this comment

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

The batch is deleted before iteration so the if condition can never be true and it will continue iterating past the current one


// outro/destroy effects
for (const [k, effect] of this.#onscreen) {
if (k === key) continue;
Copy link
Member

@dummdidumm dummdidumm Oct 18, 2025

Choose a reason for hiding this comment

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

Why only continue and not break? Doesn't that mean effects of newer batches that haven't committed yet also run?

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

Reviewers

@dummdidumm dummdidumm dummdidumm left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /