Skip to content

Commit

Permalink
feat: add a couple of UpdateItemAsync overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Jan 9, 2025
1 parent ed5a927 commit 2983601
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/Uno.Extensions.Reactive/Core/ListState.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public static ValueTask RemoveAllAsync<T>(this IListState<T> state, Predicate<T>
=> state.UpdateDataAsync(itemsOpt => itemsOpt.Map(items => items.RemoveAll(match)), ct);



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


/// <summary>
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
/// </summary>
/// <typeparam name="T">The type of the items in the list.</typeparam>
/// <param name="state">The list state onto which the item should be added.</param>
/// <param name="oldItem">The old value of the item.</param>
/// <param name="newItem">The new value for the item.</param>
/// <param name="ct">A token to abort the async add operation.</param>
/// <returns></returns>
public static ValueTask UpdateItemAsync<T>(this IListState<T?> state, T oldItem, T? newItem, CancellationToken ct = default)
where T : struct, IKeyEquatable<T>
=> 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);


/// <summary>
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
/// </summary>
Expand All @@ -159,7 +186,8 @@ public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem,
/// <param name="updater">How to update items.</param>
/// <param name="ct">A token to abort the async add operation.</param>
/// <returns></returns>
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, Func<T, T> updater, CancellationToken ct = default) where T : IKeyEquatable<T>
public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem, Func<T, T> updater, CancellationToken ct = default)
where T : notnull, IKeyEquatable<T>
=> state.UpdateDataAsync(
itemsOpt => itemsOpt.Map(items =>
{
Expand All @@ -175,6 +203,32 @@ public static ValueTask UpdateItemAsync<T>(this IListState<T> state, T oldItem,
}),
ct);

/// <summary>
/// Updates all items from a list state that match the key of <paramref name="oldItem"/>.
/// </summary>
/// <typeparam name="T">The type of the items in the list.</typeparam>
/// <param name="state">The list state onto which the item should be added.</param>
/// <param name="oldItem">The old value of the item.</param>
/// <param name="updater">How to update items.</param>
/// <param name="ct">A token to abort the async add operation.</param>
/// <returns></returns>
public static ValueTask UpdateItemAsync<T>(this IListState<T?> state, T oldItem, Func<T?, T?> updater, CancellationToken ct = default)
where T : struct, IKeyEquatable<T>
=> 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);


/// <summary>
/// Updates all matching items from a list state.
Expand Down

0 comments on commit 2983601

Please sign in to comment.