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

feat: Add overload of GetFromDeactivatableObservable that uses a Func #92

Merged
merged 1 commit into from
Dec 15, 2023
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
31 changes: 31 additions & 0 deletions src/DynamicMvvm.Tests/Integration/DeactivationIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -61,11 +62,41 @@ bool ReadCountChanged()
}
}

[Fact]
public void GetFromDeactivatableObservable_WithFuncOverload_doesnt_build_observable_more_than_once()
{
var sut = new TestVM2();

// InvocationCount should be 0 because the observable is not built until the first time it is accessed.
sut.InvocationCount.Should().Be(0);
sut.Count.Should().Be(0);

// InvocationCount should be 1 because the observable is built the first time it is accessed.
sut.InvocationCount.Should().Be(1);
sut.Count.Should().Be(0);

// InvocationCount should still be 1 because the property is cached.
sut.InvocationCount.Should().Be(1);
}

public class TestVM : DeactivatableViewModelBase
{
public ReplaySubject<int> CountSubject = new ReplaySubject<int>(bufferSize: 1);

public int Count => this.GetFromDeactivatableObservable<int>(CountSubject, initialValue: 0);
}

public class TestVM2 : DeactivatableViewModelBase
{
public int InvocationCount { get; private set; }

public int Count => this.GetFromDeactivatableObservable<int>(GetObservable, initialValue: 0);

private IObservable<int> GetObservable()
{
++InvocationCount;
return Observable.Never<int>();
}
}
}
}
16 changes: 16 additions & 0 deletions src/DynamicMvvm/Deactivation/IDeactivatableViewModel.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ public static T GetFromDeactivatableObservable<T>(this IDeactivatableViewModel v
{
return viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => new DeactivatableDynamicPropertyFromObservable<T>(name, source, viewModel, initialValue)));
}

/// <summary>
/// Gets or creates a <see cref="IDynamicProperty"/> attached to this <see cref="IViewModel"/>.<br/>
/// The underlying <see cref="IDynamicProperty"/> implements <see cref="IDeactivatable"/> so the observation can be deactivated.
/// This overload uses a <see cref="Func{TResult}"/> to avoid evaluating the observable sequence more than once (which can avoid memory allocations).
/// </summary>
/// <typeparam name="T">The property type.</typeparam>
/// <param name="viewModel">The <see cref="IViewModel"/> owning the property.</param>
/// <param name="sourceProvider">The provider of the observable of values that feeds the property.</param>
/// <param name="initialValue">The property's initial value.</param>
/// <param name="name">The property's name.</param>
/// <returns>The property's value.</returns>
public static T GetFromDeactivatableObservable<T>(this IDeactivatableViewModel viewModel, Func<IObservable<T>> sourceProvider, T initialValue = default, [CallerMemberName] string name = null)
{
return viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => new DeactivatableDynamicPropertyFromObservable<T>(name, sourceProvider(), viewModel, initialValue)));
}
}
}