Notify collection association states on membership changes - #2
Open
bemky wants to merge 1 commit into
Open
Conversation
viking mutates a collection association's `target` array in place — setTarget splices it and re-pushes — so passing `target` straight to State.set handed it the same array reference every time. State's `oldValue != newValue` guard then skipped the dispatch, and a binding on a collection state only ever showed what it was first rendered with. A "Comments (n)" header, for instance, kept its original count no matter how many were added or removed. Snapshots the array so identity changes and the dispatch goes through. belongsTo still passes its record as-is, since `target` there is a record rather than an array and already changes identity on assignment. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What
record.state('someCollection')never notified its listeners. viking mutates a collection association'stargetarray in place (setTargetsplices it and re-pushes), so the plugin handedState.setthe same array reference on every change:State.setguards onoldValue != newValue, so with an unchanged reference the dispatch was skipped every time. A binding on a collection state only ever showed what it was first rendered with — aComments (n)header, for example, kept its original count no matter how many comments were added or deleted.Fix
Snapshot the array so its identity changes:
belongsTois unchanged — itstargetis a record rather than an array, and it already changes identity on assignment, so it keeps passing straight through. That's what theArray.isArraycheck preserves.One consequence worth noting: a collection state's value is now a detached copy rather than the live
target, so it reflects membership as of the lastafterAdd/afterRemove. Mutations that dispatch neither (e.g. asetTargetthat only reorders the same members) won't refresh it — previously nothing refreshed it at all after the first render.Test
Added a
hasManycase totest/vikingTest.jsasserting listeners fire and the value tracks membership. It fails onmasterwith0 == 1(listener never called) and passes here.npm test: 37 passing, 0 failing — including the existing belongsTo cases.🤖 Generated with Claude Code