Repository navigation
foreach: apply array changes made from callbacks; honor createChildContextWithAs - #439
Open
Conversation
…ort child context creation with 'as'
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This branch has not been deployed
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 #438.
Stacked on #414's branch (
414-if-binding-evaluating-to-false-delays-invocation-of-kodescendantscomplete).Why
ForEachBindingdropped array changes that were made synchronously from inside its own callbacks (typicallyafterRender, e.g.afterRender: $parent.onRenderpushing or removing items). After that,firstLastNodesListno longer matched the array, the DOM drifted out of sync, and the next delete threwTypeError: Cannot read properties of undefined (reading 'first'). This happened in sync and async mode. Details and a reproduction are in #438.While investigating this,
asturned out to always extend the current context inForEachBinding. Unlike the template-basedforeachandwith, it ignoredoptions.createChildContextWithAs, even though the foreach docs (Note 7) describe that option as the switch.Changes
Re-entrant array changes (
packages/binding.foreach/src/foreach.ts)processQueue()now works on a snapshot ofchangeQueue. Changes registered during a pass go to a fresh queue and are scheduled throughanimateFrameonce the pass completes without throwing. Previouslyfinallywiped them. If a pass throws, the pending changes are kept and flushed with the next array change.beforeQueueFlush/afterQueueFlushreceive the batch that is actually being processed.arrayChangesubscription is set up before the initial render, so changes made during the initialafterRenderare no longer missed.as+createChildContextWithAsforeach: { data, as: 'x' }creates a child context per item:$datais the item,$parentthe outer data, and observable items are unwrapped with the observable as$rawData. The default (false, KO 3.5 behavior) is unchanged.Cleanup
setSync(false)drops the vendor-prefixedrequestAnimationFramefallbacks and keeps thesetTimeoutfallback for runtimes without rAF (Node, Bun).options.globalinstead of a hard-codedwindow..call(...)is kept so that a custom, unboundanimateFramekeeps working.Tests
New specs in
packages/binding.foreach/spec/eachBehavior.ts:afterRender›with $parent:$parentresolution in nested, virtual-element, recycled-nodeset, and asyncforeach.afterRender, including during the initial render, in a nested$parentsetup, and in async mode. All four fail without the fix.aswithout a child context by default, whitespace text nodes innodes, andthisnot being the view model. Thethistest only asserts "not the view model", so the exact value is not locked in.`as` parameter›with options.createChildContextWithAs = true: child context,$parentat root and nested,$rawDataunwrapping,$indexand$list. Three of these four fail without the change; the$index/$listtest is a guard.bun run verifypasses: Biome, tsc, build, ESM check, and 5481 tests (browser + happy-dom).Docs
$parentwithasis not the foreach parent.agents/guide.md: notes onas/$parent, whitespace nodes,this, and array changes made from callbacks.verified-behaviors.jsonand the generated markdown are updated.@tko/binding.foreach(patch).Note:
bun run buildintko.io/currently fails locally on the Starlight sidebar config (autogeneratewithlabel, removed in Starlight v0.39). The same failure occurs on the base branch without this PR's changes.