Skip to content

Conversation

@Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Nov 13, 2025

As noted in #17126, each blocks are a little buggy when there's async stuff involved. #16977 fixed a bunch of stuff around async blocks/branches, but excluded each blocks because, well, they're complicated. But the time has come to deal with it.

As the commit messages suggest this is very WIP — a bunch of tests are failing, and while it fixes part of the problem (item effects and the fallback effect both need to be created immediately inside the block effect, not later upon commit), it doesn't yet fix the problems relating to overlapping async batches. But I thought I'd open the PR in this unfinished state anyway. Pleasingly, so far it's a net reduction in code and complexity.

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

@changeset-bot
Copy link

changeset-bot bot commented Nov 13, 2025

🦋 Changeset detected

Latest commit: e41a8ea

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

@github-actions
Copy link
Contributor

Playground

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

@Rich-Harris
Copy link
Member Author

Need to come up with some test cases that test tricky things like out-of-order updates, but this is already way more robust than main. Adapting the repro from #17033:

  • main — goes absolutely haywire when you spam-click the button
  • this PR

(there is still a bug insofar as it begins at pending=2 rather than 0, but that's presumably unrelated to each blocks)

@sillvva
Copy link

sillvva commented Nov 16, 2025

This fixes #17074 as an alternative to #17151

5.43.0 REPL

This PR

…nding (#17151)

Co-authored-by: dylan <dylanbradshaw107@hotmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
@Rich-Harris
Copy link
Member Author

Thanks — have merged the test in that PR into this branch.

There's more that can be done here as we discover test cases, but since it fixes some stuff already (and apparently doesn't regress anywhere) I've marked it ready for review.

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.

This is looking great! I've added another test. The pending count is off as you say (in the test, too), I think it's because of the array entries having multiple variants in later batches - e.g. when you push 2 then 3 the first batch will see [1,2] and the second one [1, undefined, 3], and so the repeated entry will create another pending entry. It doesn't quite line up for later iterations though. I don't know how to best fix it yet, one possibility might be to special-case each blocks and require each change to happen on their own, or somehow merge later iterations into earlier ones.
This also doesn't fix #17050 (hoped it did since it's also about each blocks, but repro still fails), and there's some pending TODOs (especially the one about discard doing nothing concerns me with regards to forking) but that doesn't need to block this PR IMO.

@Rich-Harris
Copy link
Member Author

cool — will go ahead and cut a release with this, then open a PR that unskips that new test. The follow-up work can start there

@Rich-Harris Rich-Harris merged commit c02ad03 into main Nov 16, 2025
18 checks passed
@Rich-Harris Rich-Harris deleted the each-branch-manager branch November 16, 2025 17:35
@github-actions github-actions bot mentioned this pull request Nov 16, 2025

Choose a reason for hiding this comment

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

holy shit indeed


if (item === undefined) {
var pending = offscreen_items.get(key);
item = /** @type {EachItem} */ (items.get(key));

Choose a reason for hiding this comment

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

I am running into a bug in my codebase due to this line making the assumption that item cannot be undefined.
I am not able to make a reproduction, should i open a bug report anyway ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes please, maybe someone else is able to make one, or you find a way, a non-minimal reproduction would also be fine in that case

Choose a reason for hiding this comment

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

I did find a reproduction #17166 (as you said). Thx for your help

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.

Async component rendering bug with #each [bug] Bad rendering using await in component

7 participants