Skip to content

Refactor WhenAnyValue usage to generic overloads #4097

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/ReactiveUI.AOTTests/ReactiveUI.AOT.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<TrimMode>full</TrimMode>
<NoWarn>$(NoWarn);IL2026;IL3050;</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/ReactiveUI.AOTTests/StringBasedObservationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System.Reactive.Concurrency;
using System.Reactive.Linq;

Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.AndroidX/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Blazor/ReactiveComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override void OnAfterRender(bool firstRender)
{
// The following subscriptions are here because if they are done in OnInitialized, they conflict with certain JavaScript frameworks.
var viewModelChanged =
this.WhenAnyValue(x => x.ViewModel)
this.WhenAnyValue<ReactiveComponentBase<T>, T?>(nameof(ViewModel))
.WhereNotNull()
.Publish()
.RefCount(2);
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Blazor/ReactiveInjectableComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override void OnAfterRender(bool firstRender)
{
// The following subscriptions are here because if they are done in OnInitialized, they conflict with certain JavaScript frameworks.
var viewModelChanged =
this.WhenAnyValue(x => x.ViewModel)
this.WhenAnyValue<ReactiveInjectableComponentBase<T>, T?>(nameof(ViewModel))
.WhereNotNull()
.Publish()
.RefCount(2);
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Blazor/ReactiveLayoutComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected override void OnAfterRender(bool isFirstRender)
if (isFirstRender)
{
var viewModelChanged =
this.WhenAnyValue(x => x.ViewModel)
this.WhenAnyValue<ReactiveLayoutComponentBase<T>, T?>(nameof(ViewModel))
.WhereNotNull()
.Publish()
.RefCount(2);
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Blazor/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ChatRoomViewModel(IScreen hostScreen, ChatRoom room, string user)
_room = room;
_user = user;

var canSend = this.WhenAnyValue(x => x.MessageText, txt => !string.IsNullOrWhiteSpace(txt));
var canSend = this.WhenAnyValue<ChatRoomViewModel, bool, string>(nameof(MessageText), txt => !string.IsNullOrWhiteSpace(txt));
SendMessage = ReactiveCommand.Create(SendMessageImpl, canSend);

// Observe new incoming messages via MessageBus using the room name as the contract across instances
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Builder.WpfApp/ViewModels/LobbyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LobbyViewModel(IScreen hostScreen)
HostScreen = hostScreen;
UrlPathSegment = "lobby";

var canCreate = this.WhenAnyValue(x => x.RoomName, rn => !string.IsNullOrWhiteSpace(rn));
var canCreate = this.WhenAnyValue<LobbyViewModel, bool, string>(nameof(RoomName), rn => !string.IsNullOrWhiteSpace(rn));
CreateRoom = ReactiveCommand.Create(CreateRoomImpl, canCreate);

DeleteRoom = ReactiveCommand.Create<ChatRoom>(DeleteRoomImpl);
Expand Down
3 changes: 2 additions & 1 deletion src/ReactiveUI.Builder.WpfApp/Views/ChatRoomView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Reactive.Disposables;
using System.Windows;
using ReactiveUI.Builder.WpfApp.ViewModels;

namespace ReactiveUI.Builder.WpfApp.Views;

Expand All @@ -28,7 +29,7 @@ public ChatRoomView()
this.WhenActivated(d =>
{
// Map ViewModel to DataContext for XAML bindings like {Binding RoomName}
this.WhenAnyValue(v => v.ViewModel).BindTo(this, v => v.DataContext).DisposeWith(d);
this.WhenAnyValue<ChatRoomView, ChatRoomViewModel>(nameof(ViewModel)).BindTo(this, v => v.DataContext).DisposeWith(d);

this.Bind(ViewModel, vm => vm.MessageText, v => v.MessageBox.Text).DisposeWith(d);
this.BindCommand(ViewModel, vm => vm.SendMessage, v => v.SendButton).DisposeWith(d);
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Builder.WpfApp/Views/LobbyView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public LobbyView()
}).DisposeWith(d);

// Delete selected room via Delete button only
var selectedRoomStream = this.WhenAnyValue(x => x.RoomsList.SelectedItem)
var selectedRoomStream = this.WhenAnyValue<LobbyView, object>(nameof(RoomsList.SelectedItem))
.Select(x => x as ChatRoom)
.WhereNotNull();
this.BindCommand(ViewModel, vm => vm.DeleteRoom, v => v.DeleteRoomButton, selectedRoomStream)
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Drawing/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/ActivationForViewFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public IObservable<bool> GetActivationForView(IActivatableView view)

return viewLoaded
.Merge(viewUnloaded)
.Select(b => b ? view.WhenAnyValue(x => x.IsHitTestVisible).SkipWhile(x => !x) : Observables.False)
.Select(b => b ? view.WhenAnyValue<FrameworkElement, bool>(nameof(view.IsHitTestVisible)).SkipWhile(x => !x) : Observables.False)
.Switch()
.DistinctUntilChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/Common/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ViewModelViewHost()
.DistinctUntilChanged();

var contractChanged = this.WhenAnyObservable(x => x.ViewContractObservable).Do(x => _viewContract = x).StartWith(ViewContract);
var viewModelChanged = this.WhenAnyValue(x => x.ViewModel).StartWith(ViewModel);
var viewModelChanged = this.WhenAnyValue<ViewModelViewHost, object?>(nameof(ViewModel)).StartWith(ViewModel);
var vmAndContract = contractChanged
.CombineLatest(viewModelChanged, (contract, vm) => (ViewModel: vm, Contract: contract));

Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Maui/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ViewModelViewHost()

ViewContractObservable = Observable<string>.Default;

var vmAndContract = this.WhenAnyValue(x => x.ViewModel).CombineLatest(
var vmAndContract = this.WhenAnyValue<ViewModelViewHost, object?>(nameof(ViewModel)).CombineLatest(
this.WhenAnyObservable(x => x.ViewContractObservable),
(vm, contract) => new { ViewModel = vm, Contract = contract, });

Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Winforms/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
6 changes: 3 additions & 3 deletions src/ReactiveUI.Winforms/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected override void Dispose(bool disposing)
private IEnumerable<IDisposable> SetupBindings()
{
var viewChanges =
this.WhenAnyValue(x => x!.Content)
this.WhenAnyValue<ViewModelControlHost, object?>(nameof(Content))
.WhereNotNull()
.OfType<Control>()
.Subscribe(x =>
Expand All @@ -175,7 +175,7 @@ private IEnumerable<IDisposable> SetupBindings()

yield return viewChanges!;

yield return this.WhenAnyValue(x => x.DefaultContent).Subscribe(x =>
yield return this.WhenAnyValue<ViewModelControlHost, Control?>(nameof(DefaultContent)).Subscribe(x =>
{
if (x is not null)
{
Expand All @@ -186,7 +186,7 @@ private IEnumerable<IDisposable> SetupBindings()
ViewContractObservable = Observable.Return(string.Empty);

var vmAndContract =
this.WhenAnyValue(x => x.ViewModel)
this.WhenAnyValue<ViewModelControlHost, object?>(nameof(ViewModel))
.CombineLatest(
this.WhenAnyObservable(x => x.ViewContractObservable!),
(vm, contract) => new { ViewModel = vm, Contract = contract });
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Wpf/Common/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ViewModelViewHost()
.DistinctUntilChanged();

var contractChanged = this.WhenAnyObservable(x => x.ViewContractObservable).Do(x => _viewContract = x).StartWith(ViewContract);
var viewModelChanged = this.WhenAnyValue(x => x.ViewModel).StartWith(ViewModel);
var viewModelChanged = this.WhenAnyValue<ViewModelViewHost, object?>(nameof(ViewModel)).StartWith(ViewModel);
var vmAndContract = contractChanged
.CombineLatest(viewModelChanged, (contract, vm) => (ViewModel: vm, Contract: contract));

Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Wpf/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Registrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Register uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("Register uses methods that may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI/Platforms/android/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PlatformRegistrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Platform registration uses ComponentModelTypeConverter and RxApp which require dynamic code generation")]
[RequiresUnreferencedCode("Platform registration uses ComponentModelTypeConverter and RxApp which may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction) // TODO: Create Test
{
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI/Platforms/mac/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PlatformRegistrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Platform registration uses ComponentModelTypeConverter and RxApp which require dynamic code generation")]
[RequiresUnreferencedCode("Platform registration uses ComponentModelTypeConverter and RxApp which may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI/Platforms/net/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class PlatformRegistrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Platform registration uses ComponentModelTypeConverter and RxApp which require dynamic code generation")]
[RequiresUnreferencedCode("Platform registration uses ComponentModelTypeConverter and RxApp which may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PlatformRegistrations : IWantsToRegisterStuff
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Platform registration uses ComponentModelTypeConverter and RxApp which require dynamic code generation")]
[RequiresUnreferencedCode("Platform registration uses ComponentModelTypeConverter and RxApp which may require unreferenced code")]
[SuppressMessage("Trimming", "IL2046:'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
[SuppressMessage("AOT", "IL3051:'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.", Justification = "Not all paths use reflection")]
#endif
public void Register(Action<Func<object>, Type> registerFunction)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI/Suspension/SuspensionHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IObservable<T> ObserveAppState<T>(this ISuspensionHost item)
{
item.ArgumentNullExceptionThrowIfNull(nameof(item));

return item.WhenAny(suspensionHost => suspensionHost.AppState, observedChange => observedChange.Value)
return item.WhenAny<ISuspensionHost, object?, object?>(nameof(item.AppState), observedChange => observedChange.Value)
.WhereNotNull()
.Cast<T>();
}
Expand Down
Loading