Repository navigation
Conversation
Count matching items locally while projecting a range, then update filteredCount only after InsertRange or RemoveRange succeeds. A throwing selector or filter can no longer leave Count ahead of the view contents. Fixes Cysharp#144
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 #144.
A throwing selector or filter during
AddRange/InsertRangeleavesISynchronizedView.Countahead of the view's actual contents. The report in #144 is exact:CreateViewthrows on999,AddRange({2, 999})throws, the source list contains2, 999, and the view is empty whileCountis1.The same increment-during-projection pattern exists on the range
Removepath:Filter.IsMatchcan throw after earlier matches have already decrementedfilteredCount, again leavingCountinconsistent with the items still in the view.Cause
In
ObservableList<T>.View.SourceCollectionChanged, the range Add handler incrementsfilteredCountinside the selector /Filter.IsMatchloop and only then callsInsertRange. If a later item throws, earlier matches have already been counted but nothing has been inserted. Range Remove decrementsfilteredCountin the same way beforeRemoveRange.Fix
Count matches into a local
addedCount/removedCountwhile projecting the range. Mutate the view list, then applyfilteredCountonly afterInsertRange/RemoveRangesucceeds, and only then raise the view notifications.Exceptions still propagate to the caller. The source list has already accepted the mutation when it notifies the view, so the view is intentionally left unchanged in that case — it just no longer reports a
Countthat does not match its contents.A previous attempt at the Add path was opened and then self-closed in #147, unmerged. This change also covers range Remove.
The public API is unchanged.
Validation
New tests in
ObservableListTest:AddRangeof an empty list (the A throwing selector during a range insertion leaves a wrong Count on the view #144 repro):CountandUnfilteredCountstay0, view is empty.AddRangewhen the view already has an item: existing contents and counts stay unchanged.AddRange.RemoveRangeafter a successfulAddRange.AddRangestill updatesCount/ contents.On
master(a78ba19), the first three tests failed as reported (Countwas1or2with no corresponding inserted items).net8.0andnet10.0.Linux, SDK 10.0.404:
AI assistance
AI assistance was used to implement the fix, write tests, and run local validation.