fix: prevent stale re-renders of #each items during sequence update (#468)#501
Open
dupontbertrand wants to merge 3 commits intometeor:release-3.1.0from
Open
fix: prevent stale re-renders of #each items during sequence update (#468)#501dupontbertrand wants to merge 3 commits intometeor:release-3.1.0from
dupontbertrand wants to merge 3 commits intometeor:release-3.1.0from
Conversation
When the data source of a parent template changes, Tracker can re-run helpers inside #each item views before ObserveSequence has had a chance to diff and remove stale items. This causes items to briefly render with inconsistent data (e.g., item shows "foo" while parent data says "bar"). Fix by freezing item views during sequence transitions: - observe_sequence.js: add onInvalidate, beforeDiff, and afterDiff callbacks so callers can hook into the sequence update lifecycle - builtins.js: use onInvalidate to mark item views with _eachItemPendingUpdate immediately when the sequence is invalidated (before Tracker flush), and afterDiff to clear the flag on survivors - view.js: skip doRender re-runs when the view or an ancestor has _eachItemPendingUpdate set Tested with 5 scenarios: different IDs, same ID with changed content, multiple items removed, new-style #each (each-in), and nested #each. Fixes meteor#468
Add two Tinytest cases that verify #each item views don't re-render with stale data when the parent data context changes: - Different IDs (removedAt + addedAt) — the original bug report - New-style #each item in items syntax
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.
Summary
Fixes #468
When the data source of a parent template changes, Tracker can re-run helpers inside
#eachitem views beforeObserveSequencehas had a chance to diff and remove stale items. This causes items to briefly render with inconsistent data — e.g., an item'smsgshows"foo"while the parent data context already says"bar".Root cause
During
Tracker.flush(), autoruns are re-run in invalidation order. When a parent data context changes:#eachitem views are invalidated (viabindDataContext→Blaze.getData()→dataVar.get())ObserveSequenceautorun is also invalidated (it depends on the sequence function)Fix
Freeze item views during sequence transitions using 3 hooks:
onInvalidate(new callback inObserveSequence.observe): called immediately when the sequence source is invalidated, BEFORETracker.flush()re-runs other autoruns. Marks all current item views with_eachItemPendingUpdate = true.afterDiff(new callback): clears the flag on surviving item views after the diff is applied.doRenderguard (in_materializeView): skips re-render if the view or any ancestor has_eachItemPendingUpdateset.This ensures stale item views never re-render — they are either destroyed by
removedAtor updated bychangedAt, and the flag is cleared afterward.Files changed
packages/observe-sequence/observe_sequence.jsonInvalidate,beforeDiff,afterDiffcallback hookspackages/blaze/builtins.jsBlaze.Eachto freeze/unfreeze item viewspackages/blaze/view.jsdoRenderto skip re-render when pending updateTest plan
#each item in items#each(outer + inner)