-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: (mvux) Add new UpdateItemasync Extension Method #2642
base: main
Are you sure you want to change the base?
Changes from all commits
fe948bd
bed6b54
3cc7487
6307d5b
272a010
1b8811f
4fe5eca
d9aa547
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,114 @@ public static ValueTask AddAsync<T>(this IListState<T> state, T item, Cancellati | |
public static ValueTask RemoveAllAsync<T>(this IListState<T> state, Predicate<T> match, CancellationToken ct = default) | ||
=> 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> | ||
/// <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 : notnull, IKeyEquatable<T> | ||
=> state.UpdateDataAsync( | ||
itemsOpt => itemsOpt.Map(items => | ||
{ | ||
var updated = items; | ||
foreach (var item in items) | ||
{ | ||
if (item.KeyEquals(oldItem)) | ||
{ | ||
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> | ||
/// <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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the |
||
/// <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> | ||
/// <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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the |
||
/// <returns></returns> | ||
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 => | ||
{ | ||
var updated = items; | ||
foreach (var item in items) | ||
{ | ||
if (item.KeyEquals(oldItem)) | ||
{ | ||
updated = items.Replace(item, updater(item)); | ||
} | ||
} | ||
return updated; | ||
}), | ||
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the |
||
/// <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. | ||
/// </summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the
ct
param comment mentions it is for aborting the add operation. Probably just a copy-paste thing but this isn't necessarily an add operation. We could be more general and just mention it as anasync operation