Skip to content

Commit 2983601

Browse files
committed
feat: add a couple of UpdateItemAsync overloads
1 parent ed5a927 commit 2983601

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ public static ValueTask RemoveAllAsync<T>(this IListState<T> state, Predicate<T>
123123
=> state.UpdateDataAsync(itemsOpt => itemsOpt.Map(items => items.RemoveAll(match)), ct);
124124

125125

126-
127126
/// <summary>
128127
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
129128
/// </summary>
@@ -133,7 +132,8 @@ public static ValueTask RemoveAllAsync<T>(this IListState<T> state, Predicate<T>
133132
/// <param name="newItem">The new value for the item.</param>
134133
/// <param name="ct">A token to abort the async add operation.</param>
135134
/// <returns></returns>
136-
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, T newItem, CancellationToken ct = default) where T : IKeyEquatable<T>
135+
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, T newItem, CancellationToken ct = default)
136+
where T : notnull, IKeyEquatable<T>
137137
=> state.UpdateDataAsync(
138138
itemsOpt => itemsOpt.Map(items =>
139139
{
@@ -150,6 +150,33 @@ public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem,
150150
ct);
151151

152152

153+
/// <summary>
154+
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
155+
/// </summary>
156+
/// <typeparam name="T">The type of the items in the list.</typeparam>
157+
/// <param name="state">The list state onto which the item should be added.</param>
158+
/// <param name="oldItem">The old value of the item.</param>
159+
/// <param name="newItem">The new value for the item.</param>
160+
/// <param name="ct">A token to abort the async add operation.</param>
161+
/// <returns></returns>
162+
public static ValueTask UpdateItemAsync<T>(this IListState<T?> state, T oldItem, T? newItem, CancellationToken ct = default)
163+
where T : struct, IKeyEquatable<T>
164+
=> state.UpdateDataAsync(
165+
itemsOpt => itemsOpt.Map(items =>
166+
{
167+
var updated = items;
168+
foreach (var item in items)
169+
{
170+
if (item?.KeyEquals(oldItem) == true)
171+
{
172+
updated = items.Replace(item, newItem);
173+
}
174+
}
175+
return updated;
176+
}),
177+
ct);
178+
179+
153180
/// <summary>
154181
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
155182
/// </summary>
@@ -159,7 +186,8 @@ public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem,
159186
/// <param name="updater">How to update items.</param>
160187
/// <param name="ct">A token to abort the async add operation.</param>
161188
/// <returns></returns>
162-
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, Func<T, T> updater, CancellationToken ct = default) where T : IKeyEquatable<T>
189+
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, Func<T, T> updater, CancellationToken ct = default)
190+
where T : notnull, IKeyEquatable<T>
163191
=> state.UpdateDataAsync(
164192
itemsOpt => itemsOpt.Map(items =>
165193
{
@@ -175,6 +203,32 @@ public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem,
175203
}),
176204
ct);
177205

206+
/// <summary>
207+
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
208+
/// </summary>
209+
/// <typeparam name="T">The type of the items in the list.</typeparam>
210+
/// <param name="state">The list state onto which the item should be added.</param>
211+
/// <param name="oldItem">The old value of the item.</param>
212+
/// <param name="updater">How to update items.</param>
213+
/// <param name="ct">A token to abort the async add operation.</param>
214+
/// <returns></returns>
215+
public static ValueTask UpdateItemAsync<T>(this IListState<T?> state, T oldItem, Func<T?, T?> updater, CancellationToken ct = default)
216+
where T : struct, IKeyEquatable<T>
217+
=> state.UpdateDataAsync(
218+
itemsOpt => itemsOpt.Map(items =>
219+
{
220+
var updated = items;
221+
foreach (var item in items)
222+
{
223+
if (item?.KeyEquals(oldItem) == true)
224+
{
225+
updated = items.Replace(item, updater(item));
226+
}
227+
}
228+
return updated;
229+
}),
230+
ct);
231+
178232

179233
/// <summary>
180234
/// Updates all matching items from a list state.

0 commit comments

Comments
 (0)