Repository navigation
Run structural effects before dependent effects - #4878
Merged
Merged
Conversation
calebporzio
marked this pull request as ready for review
August 13, 2026 16:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4843.
Problem
A keyed
x-forcan reuse a DOM node while its injected iteration scope still represents the node's previous loop position. If a child effect is notified beforex-for, it combines new outer state with that old scope. Alpine's whole-flush dedupe then suppresses the corrective run requested whenx-forrefreshes the scope.In the reported virtual-list case, the sequence
1–25→1–15→13–30can therefore render25, 26, 27where13, 14, 15belong.Approach
Before Alpine runs an ordinary effect, this change runs every pending structural effect parent-before-child. It keeps the existing invariant that a job runs at most once per flush.
x-for,x-if, andx-htmlopt their subtree-owning effects into structural scheduling.x-htmldestroys the Alpine child tree it replaces so obsolete descendant effects are cleaned up and dequeued.This is an alternative to the completed-job requeueing proposed in #4851. Requeueing fixes the reported symptom, but changes Alpine globally into a convergence scheduler and reopens recursive-effect behavior. Structural ordering prevents the invalid intermediate observation without changing whole-flush dedupe.
Why the complete rule is needed
Each smaller version has a demonstrated counterexample:
x-forlets a nested loop run before an invalidatedx-iforx-htmlancestor removes it.Validation
x-ifandx-htmlancestors, including a moved initialized subtree.x-htmllifecycle destruction exactly once.x-if,x-html, teleports, randomized assignment order, and transaction/non-transaction batching.0x4851immediately; replacing logical depth with creation order fails scheduler seed 1.Performance
Average medians from three alternating
main/PR browser runs:mainx-ifdirectivesx-htmldirectivesOrdinary and structural workloads are within run-to-run noise. The repeatable
x-htmlimprovement comes from destroying replaced descendant effects instead of leaving detached effects subscribed.The minified CDN bundle grows by 860 bytes raw / 262 bytes gzip (47,465 → 48,325 raw; 17,034 → 17,296 gzip).
Intentional boundary
This guarantees ordering for work invalidated before a flush and structural work added while draining it. It does not make arbitrary user-effect graphs converge, nor does it change scheduler exception recovery. Those are separate scheduler policies and intentionally remain out of scope.