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

TransformOnRefresh option for TransformWithInlineUpdate. #875

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 26 additions & 1 deletion src/DynamicData.Tests/Cache/TransformWithInlineUpdateFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,34 @@ public void Remove()
stub.Results.Data.Count.Should().Be(0, "Should be nothing cached");
}


[Fact]
public void TransformOnRefresh()
{
using var stub = new TransformWithInlineUpdateFixtureStub(true);
var person = new Person("Adult1", 50);
stub.Source.AddOrUpdate(person);

var transformedPerson = stub.Results.Data.Items.First();

person.Age = 51;
stub.Source.Refresh(person);

var updatedTransform = stub.Results.Data.Items.First();

updatedTransform.Age.Should().Be(51, "Age should be updated from 50 to 51.");
stub.Results.Messages.Count.Should().Be(2, "Should be 2 updates");
stub.Results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
transformedPerson.Should().Be(stub.Results.Data.Items.First(), "Should be same transformed person instance.");
}

private class TransformWithInlineUpdateFixtureStub : IDisposable
{
public TransformWithInlineUpdateFixtureStub() => Results = new ChangeSetAggregator<Person, string>(Source.Connect().TransformWithInlineUpdate(TransformFactory, UpdateAction));
public TransformWithInlineUpdateFixtureStub(bool transformOnRefresh = false)
{
Results = new ChangeSetAggregator<Person, string>(Source.Connect()
.TransformWithInlineUpdate(TransformFactory, UpdateAction, transformOnRefresh));
}

public ChangeSetAggregator<Person, string> Results { get; }

Expand Down
13 changes: 11 additions & 2 deletions src/DynamicData/Cache/Internal/TransformWithInlineUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace DynamicData.Cache.Internal;
internal sealed class TransformWithInlineUpdate<TDestination, TSource, TKey>(IObservable<IChangeSet<TSource, TKey>> source,
Func<TSource, TDestination> transformFactory,
Action<TDestination, TSource> updateAction,
Action<Error<TSource, TKey>>? exceptionCallback = null)
Action<Error<TSource, TKey>>? exceptionCallback = null,
bool transformOnRefresh = false)
where TDestination : class
where TSource : notnull
where TKey : notnull
Expand Down Expand Up @@ -41,7 +42,15 @@ private IObservable<IChangeSet<TDestination, TKey>> RunImpl() => source.Scan(
break;

case ChangeReason.Refresh:
cache.Refresh(change.Key);
if (transformOnRefresh)
{
InlineUpdate(cache, change);
}
else
{
cache.Refresh(change.Key);
}

break;

case ChangeReason.Moved:
Expand Down
10 changes: 6 additions & 4 deletions src/DynamicData/Cache/ObservableCacheEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5899,13 +5899,14 @@ public static IObservable<IChangeSet<Node<TObject, TKey>, TKey>> TransformToTree
/// <param name="source">The source.</param>
/// <param name="transformFactory">The transform factory.</param>
/// <param name="updateAction">Apply changes to the original. Example (previousTransformedItem, newOriginalItem) => previousTransformedItem.Value = newOriginalItem.</param>
/// <param name="transformOnRefresh">Should a new transform be applied when a refresh event is received.</param>
/// <returns>
/// A transformed update collection.
/// </returns>
/// <exception cref="ArgumentNullException">source
/// or
/// transformFactory.</exception>
public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpdate<TDestination, TSource, TKey>(this IObservable<IChangeSet<TSource, TKey>> source, Func<TSource, TDestination> transformFactory, Action<TDestination, TSource> updateAction)
public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpdate<TDestination, TSource, TKey>(this IObservable<IChangeSet<TSource, TKey>> source, Func<TSource, TDestination> transformFactory, Action<TDestination, TSource> updateAction, bool transformOnRefresh = false)
JakenVeina marked this conversation as resolved.
Show resolved Hide resolved
where TDestination : class
where TSource : notnull
where TKey : notnull
Expand All @@ -5914,7 +5915,7 @@ public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpd
transformFactory.ThrowArgumentNullExceptionIfNull(nameof(transformFactory));
updateAction.ThrowArgumentNullExceptionIfNull(nameof(updateAction));

return new TransformWithInlineUpdate<TDestination, TSource, TKey>(source, transformFactory, updateAction).Run();
return new TransformWithInlineUpdate<TDestination, TSource, TKey>(source, transformFactory, updateAction, transformOnRefresh: transformOnRefresh).Run();
}

/// <summary>
Expand All @@ -5927,13 +5928,14 @@ public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpd
/// <param name="transformFactory">The transform factory.</param>
/// <param name="updateAction">Apply changes to the original. Example (previousTransformedItem, newOriginalItem) => previousTransformedItem.Value = newOriginalItem.</param>
/// <param name="errorHandler">The error handler.</param>
/// <param name="transformOnRefresh">Should a new transform be applied when a refresh event is received.</param>
/// <returns>
/// A transformed update collection.
/// </returns>
/// <exception cref="ArgumentNullException">source
/// or
/// transformFactory.</exception>
public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpdate<TDestination, TSource, TKey>(this IObservable<IChangeSet<TSource, TKey>> source, Func<TSource, TDestination> transformFactory, Action<TDestination, TSource> updateAction, Action<Error<TSource, TKey>> errorHandler)
public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpdate<TDestination, TSource, TKey>(this IObservable<IChangeSet<TSource, TKey>> source, Func<TSource, TDestination> transformFactory, Action<TDestination, TSource> updateAction, Action<Error<TSource, TKey>> errorHandler, bool transformOnRefresh = false)
where TDestination : class
where TSource : notnull
where TKey : notnull
Expand All @@ -5943,7 +5945,7 @@ public static IObservable<IChangeSet<TDestination, TKey>> TransformWithInlineUpd
updateAction.ThrowArgumentNullExceptionIfNull(nameof(updateAction));
errorHandler.ThrowArgumentNullExceptionIfNull(nameof(errorHandler));

return new TransformWithInlineUpdate<TDestination, TSource, TKey>(source, transformFactory, updateAction, errorHandler).Run();
return new TransformWithInlineUpdate<TDestination, TSource, TKey>(source, transformFactory, updateAction, errorHandler, transformOnRefresh).Run();
}

/// <summary>
Expand Down
Loading