Skip to content

Commit 4aaa9c1

Browse files
authored
Rewrote the list variant of the OnItemRefreshed() operator, in accordance with #1014. (#1068)
1 parent 1a7d43a commit 4aaa9c1

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

src/DynamicData.Tests/API/ApiApprovalTests.DynamicDataTests.DotNet9_0.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,8 +2389,8 @@ namespace DynamicData
23892389
where T : notnull { }
23902390
public static System.IObservable<DynamicData.IChangeSet<T>> OnItemAdded<T>(this System.IObservable<DynamicData.IChangeSet<T>> source, System.Action<T> addAction)
23912391
where T : notnull { }
2392-
public static System.IObservable<DynamicData.IChangeSet<TObject>> OnItemRefreshed<TObject>(this System.IObservable<DynamicData.IChangeSet<TObject>> source, System.Action<TObject> refreshAction)
2393-
where TObject : notnull { }
2392+
public static System.IObservable<DynamicData.IChangeSet<T>> OnItemRefreshed<T>(this System.IObservable<DynamicData.IChangeSet<T>> source, System.Action<T> refreshAction)
2393+
where T : notnull { }
23942394
public static System.IObservable<DynamicData.IChangeSet<T>> OnItemRemoved<T>(this System.IObservable<DynamicData.IChangeSet<T>> source, System.Action<T> removeAction, bool invokeOnUnsubscribe = true)
23952395
where T : notnull { }
23962396
public static System.IObservable<DynamicData.IChangeSet<T>> Or<T>(this DynamicData.IObservableList<DynamicData.IObservableList<T>> sources)
@@ -3100,4 +3100,4 @@ namespace DynamicData.Tests
31003100
public void Dispose() { }
31013101
protected virtual void Dispose(bool isDisposing) { }
31023102
}
3103-
}
3103+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2011-2025 Roland Pheasant. All rights reserved.
2+
// Roland Pheasant licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for full license information.
4+
5+
using System.Reactive.Linq;
6+
7+
namespace DynamicData.List.Internal;
8+
9+
internal static class OnItemRefreshed<T>
10+
where T : notnull
11+
{
12+
public static IObservable<IChangeSet<T>> Create(
13+
IObservable<IChangeSet<T>> source,
14+
Action<T> refreshAction)
15+
{
16+
source.ThrowArgumentNullExceptionIfNull(nameof(source));
17+
refreshAction.ThrowArgumentNullExceptionIfNull(nameof(refreshAction));
18+
19+
return source.Do(changeSet =>
20+
{
21+
foreach (var change in changeSet)
22+
{
23+
if (change.Reason is ListChangeReason.Refresh)
24+
refreshAction.Invoke(change.Item.Current);
25+
}
26+
});
27+
}
28+
}

src/DynamicData/List/ObservableListEx.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,29 +1259,21 @@ public static IObservable<IChangeSet<T>> OnItemAdded<T>(
12591259
addAction: addAction);
12601260

12611261
/// <summary>
1262-
/// Callback for each item as and when it is being refreshed in the stream.
1262+
/// Invokes a given action for every item refreshed within the source list stream.
12631263
/// </summary>
1264-
/// <typeparam name="TObject">The type of the object.</typeparam>
1265-
/// <param name="source">The source.</param>
1266-
/// <param name="refreshAction">The refresh action.</param>
1267-
/// <returns>An observable which emits a change set with items being added.</returns>
1268-
public static IObservable<IChangeSet<TObject>> OnItemRefreshed<TObject>(this IObservable<IChangeSet<TObject>> source, Action<TObject> refreshAction)
1269-
where TObject : notnull
1270-
{
1271-
var refreshAction2 = refreshAction;
1272-
if (source == null)
1273-
{
1274-
throw new ArgumentNullException(nameof(source));
1275-
}
1276-
1277-
if (refreshAction2 == null)
1278-
{
1279-
throw new ArgumentNullException(nameof(refreshAction));
1280-
}
1281-
1282-
return source.Do((IChangeSet<TObject> changes) =>
1283-
changes.Where((Change<TObject> c) => c.Reason == ListChangeReason.Refresh).ForEach((Change<TObject> c) => refreshAction2(c.Item.Current)));
1284-
}
1264+
/// <typeparam name="T">The type of items in the list.</typeparam>
1265+
/// <param name="source">The list stream whose items are to be passed to <paramref name="refreshAction"/>.</param>
1266+
/// <param name="refreshAction">The action to invoke upon each refreshed item.</param>
1267+
/// <returns>A list stream, containing all items in <paramref name="source"/>, with changes published after <paramref name="refreshAction"/> has been invoked.</returns>
1268+
/// <exception cref="ArgumentNullException">Throws for <paramref name="source"/> and <paramref name="refreshAction"/>.</exception>
1269+
/// <remarks>Note that "refreshed" items refers to items from <see cref="ListChangeReason.Refresh"/> changes.</remarks>
1270+
public static IObservable<IChangeSet<T>> OnItemRefreshed<T>(
1271+
this IObservable<IChangeSet<T>> source,
1272+
Action<T> refreshAction)
1273+
where T : notnull
1274+
=> List.Internal.OnItemRefreshed<T>.Create(
1275+
source: source,
1276+
refreshAction: refreshAction);
12851277

12861278
/// <summary>
12871279
/// Callback for each item as and when it is being removed from the stream.

0 commit comments

Comments
 (0)