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

#111 - OnItemAdded and OnItemRemoved for SourceCache #135

Merged
merged 1 commit into from
Jul 16, 2018
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
58 changes: 58 additions & 0 deletions DynamicData.Tests/Cache/OnItemFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using DynamicData.Tests.Domain;
using Xunit;

namespace DynamicData.Tests.Cache
{
public class OnItemFixture
{
[Fact]
public void OnItemAddCalled()
{
var called = false;
var source = new SourceCache<Person, int>(x => x.Age);

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

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

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

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

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

var person = new Person("A", 1);
source.AddOrUpdate(person);
source.Remove(person);
Assert.True(called);
}

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

source.Connect()
.OnItemUpdated((x,y) => called = true)
.Subscribe();

var person = new Person("A", 1);
source.AddOrUpdate(person);
var update = new Person("B", 1);
source.AddOrUpdate(update);
Assert.True(called);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace DynamicData.Cache.Internal
{
internal class OnItemRemoved<TObject, TKey>
internal class DisposeMany<TObject, TKey>
{
private readonly IObservable<IChangeSet<TObject, TKey>> _source;
private readonly Action<TObject> _removeAction;

public OnItemRemoved([NotNull] IObservable<IChangeSet<TObject, TKey>> source, Action<TObject> removeAction)
public DisposeMany([NotNull] IObservable<IChangeSet<TObject, TKey>> source, Action<TObject> removeAction)
{
_source = source ?? throw new ArgumentNullException(nameof(source));
_removeAction = removeAction ?? throw new ArgumentNullException(nameof(removeAction));
Expand Down
38 changes: 28 additions & 10 deletions DynamicData/Cache/ObservableCacheEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ public static IObservable<IChangeSet<TObject, TKey>> SubscribeMany<TObject, TKey
return new SubscribeMany<TObject, TKey>(source, subscriptionFactory).Run();
}


/// <summary>
/// Callback for each item as and when it is being added to 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="addAction">The add action.</param>
/// <returns></returns>
public static IObservable<IChangeSet<TObject, TKey>> OnItemAdded<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source, [NotNull] Action<TObject> addAction)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (addAction == null) throw new ArgumentNullException(nameof(addAction));

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 removed from the stream
/// </summary>
Expand All @@ -412,7 +430,8 @@ public static IObservable<IChangeSet<TObject, TKey>> OnItemRemoved<TObject, TKey
if (source == null) throw new ArgumentNullException(nameof(source));
if (removeAction == null) throw new ArgumentNullException(nameof(removeAction));

return new OnItemRemoved<TObject, TKey>(source, removeAction).Run();
return source.Do(changes => changes.Where(c => c.Reason == ChangeReason.Remove)
.ForEach(c => removeAction(c.Current)));
}

/// <summary>
Expand All @@ -431,10 +450,7 @@ public static IObservable<IChangeSet<TObject, TKey>> OnItemUpdated<TObject, TKey
if (updateAction == null) throw new ArgumentNullException(nameof(updateAction));

return source.Do(changes => changes.Where(c => c.Reason == ChangeReason.Update)
.ForEach(c =>
{
updateAction(c.Current, c.Previous.Value);
}));
.ForEach(c => updateAction(c.Current, c.Previous.Value)));
}

/// <summary>
Expand All @@ -452,11 +468,13 @@ public static IObservable<IChangeSet<TObject, TKey>> OnItemUpdated<TObject, TKey
/// <exception cref="System.ArgumentNullException">source</exception>
public static IObservable<IChangeSet<TObject, TKey>> DisposeMany<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source)
{
return source.OnItemRemoved(t =>
{
var d = t as IDisposable;
d?.Dispose();
});
if (source == null) throw new ArgumentNullException(nameof(source));
return new DisposeMany<TObject, TKey>(source, t =>
{
var d = t as IDisposable;
d?.Dispose();
})
.Run();
}

/// <summary>
Expand Down