From 4fe5eca982e57a37a2702c38ce15cd15a5d44a2e Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Thu, 9 Jan 2025 00:08:56 -0500 Subject: [PATCH] feat: add a couple of UpdateItemAsync overloads --- .../Core/ListState.Extensions.cs | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs b/src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs index 61ef8b1af..04bea8016 100644 --- a/src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs +++ b/src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs @@ -123,7 +123,6 @@ public static ValueTask RemoveAllAsync(this IListState state, Predicate => state.UpdateDataAsync(itemsOpt => itemsOpt.Map(items => items.RemoveAll(match)), ct); - /// /// Updates all items from a list state that match the key of . /// @@ -133,7 +132,8 @@ public static ValueTask RemoveAllAsync(this IListState state, Predicate /// The new value for the item. /// A token to abort the async add operation. /// - public static ValueTask UpdateItemAsync(this IListState state, T oldItem, T newItem, CancellationToken ct = default) where T : IKeyEquatable + public static ValueTask UpdateItemAsync(this IListState state, T oldItem, T newItem, CancellationToken ct = default) + where T : notnull, IKeyEquatable => state.UpdateDataAsync( itemsOpt => itemsOpt.Map(items => { @@ -150,6 +150,33 @@ public static ValueTask UpdateItemAsync(this IListState state, T oldItem, ct); + /// + /// Updates all items from a list state that match the key of . + /// + /// The type of the items in the list. + /// The list state onto which the item should be added. + /// The old value of the item. + /// The new value for the item. + /// A token to abort the async add operation. + /// + public static ValueTask UpdateItemAsync(this IListState state, T oldItem, T? newItem, CancellationToken ct = default) + where T : struct, IKeyEquatable + => state.UpdateDataAsync( + itemsOpt => itemsOpt.Map(items => + { + var updated = items; + foreach (var item in items) + { + if (item?.KeyEquals(oldItem) == true) + { + updated = items.Replace(item, newItem); + } + } + return updated; + }), + ct); + + /// /// Updates all items from a list state that match the key of . /// @@ -159,7 +186,8 @@ public static ValueTask UpdateItemAsync(this IListState state, T oldItem, /// How to update items. /// A token to abort the async add operation. /// - public static ValueTask UpdateItemAsync(this IListState state, T oldItem, Func updater, CancellationToken ct = default) where T : IKeyEquatable + public static ValueTask UpdateItemAsync(this IListState state, T oldItem, Func updater, CancellationToken ct = default) + where T : notnull, IKeyEquatable => state.UpdateDataAsync( itemsOpt => itemsOpt.Map(items => { @@ -175,6 +203,32 @@ public static ValueTask UpdateItemAsync(this IListState state, T oldItem, }), ct); + /// + /// Updates all items from a list state that match the key of . + /// + /// The type of the items in the list. + /// The list state onto which the item should be added. + /// The old value of the item. + /// How to update items. + /// A token to abort the async add operation. + /// + public static ValueTask UpdateItemAsync(this IListState state, T oldItem, Func updater, CancellationToken ct = default) + where T : struct, IKeyEquatable + => state.UpdateDataAsync( + itemsOpt => itemsOpt.Map(items => + { + var updated = items; + foreach (var item in items) + { + if (item?.KeyEquals(oldItem) == true) + { + updated = items.Replace(item, updater(item)); + } + } + return updated; + }), + ct); + /// /// Updates all matching items from a list state.