Skip to content
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

feature: Adds OnItemRefreshed extension method for SourceList and SourceCache #580

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/DynamicData.Tests/Cache/OnItemFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public void OnItemAddCalled()
Assert.True(called);
}

[Fact]
public void OnItemRefreshedCalled()
{
var called = false;
var source = new SourceCache<Person, int>(x => x.Age);

var person = new Person("A", 1);
source.AddOrUpdate(person);

source.Connect().AutoRefresh(x=>x.Age).OnItemRefreshed(_ => called = true).Subscribe();

person.Age += 1;

Assert.True(called);
}

[Fact]
public void OnItemRemovedCalled()
{
Expand Down
54 changes: 54 additions & 0 deletions src/DynamicData.Tests/List/OnItemFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;

using DynamicData.Tests.Domain;

using Xunit;

namespace DynamicData.Tests.List;

public class OnItemFixture
{
[Fact]
public void OnItemAddCalled()
{
var called = false;
var source = new SourceList<Person>();

source.Connect().OnItemAdded(_ => called = true).Subscribe();

var person = new Person("A", 1);

source.Add(person);
Assert.True(called);
}

[Fact]
public void OnItemRefreshedCalled()
{
var called = false;
var source = new SourceList<Person>();

var person = new Person("A", 1);
source.Add(person);

source.Connect().AutoRefresh(x=>x.Age).OnItemRefreshed(_ => called = true).Subscribe();

person.Age += 1;

Assert.True(called);
}

[Fact]
public void OnItemRemovedCalled()
{
var called = false;
var source = new SourceList<Person>();

source.Connect().OnItemRemoved(_ => called = true).Subscribe();

var person = new Person("A", 1);
source.Add(person);
source.Remove(person);
Assert.True(called);
}
}
36 changes: 31 additions & 5 deletions src/DynamicData/Cache/ObservableCacheEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

#pragma warning disable SA1137

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;

using DynamicData.Binding;
using DynamicData.Cache.Internal;
Expand Down Expand Up @@ -2824,6 +2819,37 @@ public static IObservable<IChangeSet<TObject, TKey>> OnItemAdded<TObject, TKey>(
return source.Do(changes => changes.Where(c => c.Reason == ChangeReason.Add).ForEach(c => addAction(c.Current)));
}

/// <summary>
/// Callback for each item as and when it is being refreshed in the stream.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="refreshAction">The refresh action.</param>
/// <returns>An observable which emits a change set with items being added.</returns>
public static IObservable<IChangeSet<TObject, TKey>> OnItemRefreshed<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source, Action<TObject> refreshAction)
where TKey : notnull
{
Action<TObject> refreshAction2 = refreshAction;
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (refreshAction2 == null)
{
throw new ArgumentNullException(nameof(refreshAction));
}

return source.Do(delegate(IChangeSet<TObject, TKey> changes)
{
changes.Where((Change<TObject, TKey> c) => c.Reason == ChangeReason.Refresh).ForEach(delegate(Change<TObject, TKey> c)
{
refreshAction2(c.Current);
});
});
}

/// <summary>
/// Callback for each item as and when it is being removed from the stream.
/// </summary>
Expand Down
34 changes: 30 additions & 4 deletions src/DynamicData/List/ObservableListEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;

using DynamicData.Binding;
using DynamicData.Cache.Internal;
Expand Down Expand Up @@ -333,6 +329,7 @@ public static IObservable<IChangeSet<T>> Bind<T>(this IObservable<IChangeSet<T>>
}

#if SUPPORTS_BINDINGLIST

/// <summary>
/// Binds a clone of the observable change set to the target observable collection.
/// </summary>
Expand Down Expand Up @@ -1118,6 +1115,35 @@ public static IObservable<IChangeSet<T>> OnItemAdded<T>(this IObservable<IChange
return new OnBeingAdded<T>(source, addAction).Run();
}

/// <summary>
/// Callback for each item as and when it is being refreshed in the stream.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <param name="refreshAction">The refresh action.</param>
/// <returns>An observable which emits a change set with items being added.</returns>
public static IObservable<IChangeSet<TObject>> OnItemRefreshed<TObject>(this IObservable<IChangeSet<TObject>> source, Action<TObject> refreshAction)
{
Action<TObject> refreshAction2 = refreshAction;
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (refreshAction2 == null)
{
throw new ArgumentNullException(nameof(refreshAction));
}

return source.Do(delegate(IChangeSet<TObject> changes)
{
changes.Where((Change<TObject> c) => c.Reason == ListChangeReason.Refresh).ForEach(delegate(Change<TObject> c)
{
refreshAction2(c.Item.Current);
});
});
}

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