Skip to content
Permalink
Browse files

fix: Blazor ViewModel nullability during rendering (#2336)

* fix: Blazor ViewModel nullability during rendering

* formatting for readability
  • Loading branch information
RLittlesII committed Jan 28, 2020
1 parent a9f4585 commit 46d4a603fbbdec29757ebde3be3e730311dcb02f
@@ -31,27 +31,6 @@ public class ReactiveComponentBase<T> : ComponentBase, IViewFor<T>, INotifyPrope

private bool _disposedValue; // To detect redundant calls

/// <summary>
/// Initializes a new instance of the <see cref="ReactiveComponentBase{T}"/> class.
/// </summary>
public ReactiveComponentBase()
{
var viewModelsPropertyChanged = this.WhenAnyValue(x => x.ViewModel)
.Where(x => x != null)
.Select(x => Observable.FromEvent<PropertyChangedEventHandler, Unit>(
eventHandler =>
{
void Handler(object sender, PropertyChangedEventArgs e) => eventHandler(Unit.Default);

return Handler;
},
eh => x.PropertyChanged += eh,
eh => x.PropertyChanged -= eh))
.Switch();

viewModelsPropertyChanged.Do(_ => InvokeAsync(StateHasChanged)).Subscribe();
}

/// <inheritdoc />
public event PropertyChangedEventHandler PropertyChanged;

@@ -105,8 +84,25 @@ protected override void OnAfterRender(bool isFirstRender)
{
if (isFirstRender)
{
this.WhenAnyValue(x => x.ViewModel).Where(x => x != null).Subscribe(_ => InvokeAsync(StateHasChanged));
this.WhenAnyValue(x => x.ViewModel)
.Skip(1)
.Where(x => x != null)
.Subscribe(_ => InvokeAsync(StateHasChanged));
}

this.WhenAnyValue(x => x.ViewModel)
.Where(x => x != null)
.Select(x => Observable.FromEvent<PropertyChangedEventHandler, Unit>(
eventHandler =>
{
void Handler(object sender, PropertyChangedEventArgs e) => eventHandler(Unit.Default);
return Handler;
},
eh => x.PropertyChanged += eh,
eh => x.PropertyChanged -= eh))
.Switch()
.Do(_ => InvokeAsync(StateHasChanged))
.Subscribe();
}

/// <summary>
@@ -29,27 +29,6 @@ public class ReactiveLayoutComponentBase<T> : LayoutComponentBase, IViewFor<T>,

private bool _disposedValue; // To detect redundant calls

/// <summary>
/// Initializes a new instance of the <see cref="ReactiveLayoutComponentBase{T}"/> class.
/// </summary>
public ReactiveLayoutComponentBase()
{
var viewModelsPropertyChanged = this.WhenAnyValue(x => x.ViewModel)
.Where(x => x != null)
.Select(x => Observable.FromEvent<PropertyChangedEventHandler, Unit>(
eventHandler =>
{
void Handler(object sender, PropertyChangedEventArgs e) => eventHandler(Unit.Default);

return Handler;
},
eh => x.PropertyChanged += eh,
eh => x.PropertyChanged -= eh))
.Switch();

viewModelsPropertyChanged.Do(_ => InvokeAsync(StateHasChanged)).Subscribe();
}

/// <inheritdoc />
public event PropertyChangedEventHandler PropertyChanged;

@@ -102,8 +81,25 @@ protected override void OnAfterRender(bool isFirstRender)
{
if (isFirstRender)
{
this.WhenAnyValue(x => x.ViewModel).Where(x => x != null).Subscribe(_ => InvokeAsync(StateHasChanged));
this.WhenAnyValue(x => x.ViewModel)
.Skip(1)
.Where(x => x != null)
.Subscribe(_ => InvokeAsync(StateHasChanged));
}

this.WhenAnyValue(x => x.ViewModel)
.Where(x => x != null)
.Select(x => Observable.FromEvent<PropertyChangedEventHandler, Unit>(
eventHandler =>
{
void Handler(object sender, PropertyChangedEventArgs e) => eventHandler(Unit.Default);
return Handler;
},
eh => x.PropertyChanged += eh,
eh => x.PropertyChanged -= eh))
.Switch()
.Do(_ => InvokeAsync(StateHasChanged))
.Subscribe();
}

/// <summary>
@@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Testing.Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.AndroidX", "ReactiveUI.AndroidX\ReactiveUI.AndroidX.csproj", "{294D8C67-0181-41B4-AB28-0114CE3D6390}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Blazor", "ReactiveUI.Blazor\ReactiveUI.Blazor.csproj", "{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -584,6 +586,62 @@ Global
{294D8C67-0181-41B4-AB28-0114CE3D6390}.Release|x64.Build.0 = Release|Any CPU
{294D8C67-0181-41B4-AB28-0114CE3D6390}.Release|x86.ActiveCfg = Release|Any CPU
{294D8C67-0181-41B4-AB28-0114CE3D6390}.Release|x86.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|Mixed Platforms.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|ARM.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|ARM.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|iPhone.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|Mixed Platforms.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|x64.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|x64.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|x86.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.AppStore|x86.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|ARM.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|ARM.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|iPhone.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|x64.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|x64.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|x86.ActiveCfg = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Debug|x86.Build.0 = Debug|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|Any CPU.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|ARM.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|ARM.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|iPhone.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|iPhone.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|x64.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|x64.Build.0 = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|x86.ActiveCfg = Release|Any CPU
{8D4DDE4A-56E2-4FB2-802C-92C8992E5CB1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

0 comments on commit 46d4a60

Please sign in to comment.
You can’t perform that action at this time.