From eee7af494a6bf8f28a98275779f4d2fa4c352d8b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 5 Aug 2025 12:58:35 -0400 Subject: [PATCH] fix (fills ui): Preserve sort order during incremental update Binary search is used to add, remove, and change rows in response to incremental updates. Needless to say, if the rows become unsorted, everything breaks. Bugs (fixed): * The fills window crashes sometimes, possibly related to client reconnecting to server. Cause: This was a 2 part bug. 1) Changes to existing rows are applied BEFORE the new sort index is determined via binary search. If the change affects a property in the current sorting criteria, the rows could become unsorted and thus invalidate the binary search. 2) When an existing row is changed so that its new sort index is greater than its current one, the new index is not decremented by one (it must be since the row is removed before it is re-inserted). --- src/client/spark/view/Utility/RXCollection.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs index a639a2bd4..5d5ac1acf 100644 --- a/src/client/spark/view/Utility/RXCollection.cs +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -240,16 +240,18 @@ namespace mbts.ui.utility { wrapped_models.InsertSorted(table.state.Values, comp); return; } else { - var view_item = update(target[pos], ch.New); int new_pos = CollectionHelper.binary_search(wrapped_models, ch.New, comp); - var view_item_state = update(target[pos], ch.New); if (new_pos < 0) new_pos = ~new_pos; + var view_item = update(target[pos], ch.New); if (Math.Abs(new_pos - pos) > 1) { // If the abs diff between old & new positions is 1 (or 0), // the item does not need to be moved because its position wrt the other elements is unchanged. var state = on_moving == null ? default : on_moving(view_item); target.RemoveAt(pos); + if (new_pos > pos) { + new_pos--; + } target.Insert(new_pos, view_item); on_moved?.Invoke(view_item, state); }