Skip to content
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
6 changes: 6 additions & 0 deletions src/ReactiveUI.Tests/Suspension/DummyAppState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ReactiveUI.Tests.Suspension
{
public class DummyAppState
{
}
}
58 changes: 58 additions & 0 deletions src/ReactiveUI.Tests/Suspension/SuspensionHostExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Shouldly;
using Xunit;

namespace ReactiveUI.Tests.Suspension
{
public class SuspensionHostExtensionsTests
{
[Fact]
public void GetAppStateReturns()
{
var fixture = new SuspensionHost();
fixture.AppState = new DummyAppState();

var result = fixture.GetAppState<DummyAppState>();

result.ShouldBe(fixture.AppState);
}

[Fact]
public void NullSuspensionHostThrowsException()
{
var result = Record.Exception(() => ((SuspensionHost)null!).SetupDefaultSuspendResume());

result.ShouldBeOfType<ArgumentNullException>();
}

[Fact]
public void NullAppStateDoesNotThrowException()
{
var fixture = new SuspensionHost();

var result = Record.Exception(() => fixture.SetupDefaultSuspendResume());

result.ShouldBeNull();
}

[Fact]
public void ObserveAppStateDoesNotThrowException()
{
var fixture = new SuspensionHost();

var result = Record.Exception(() => fixture.ObserveAppState<DummyAppState>().Subscribe());

result.ShouldBeNull();
}

[Fact]
public void ObserveAppStateDoesNotThrowInvalidCastException()
{
var fixture = new SuspensionHost();

var result = Record.Exception(() => fixture.ObserveAppState<DummyAppState>().Subscribe());

result.ShouldNotBeOfType<InvalidCastException>();
}
}
}
19 changes: 5 additions & 14 deletions src/ReactiveUI/Suspension/SuspensionHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public static class SuspensionHostExtensions
public static IObservable<T> ObserveAppState<T>(this ISuspensionHost item)
where T : class
{
return item.WhenAny(x => x.AppState, x => (T)x)
.Where(x => x != null);
return item.WhenAny(suspensionHost => suspensionHost.AppState, observedChange => observedChange.Value)
.Where(x => x != null)
.Cast<T>();
}

/// <summary>
Expand All @@ -41,12 +42,7 @@ public static T GetAppState<T>(this ISuspensionHost item)
throw new ArgumentNullException(nameof(item));
}

if (item.AppState == null)
{
throw new NullReferenceException(nameof(item.AppState));
}

return (T)item.AppState;
return (T)item.AppState!;
}

/// <summary>
Expand All @@ -63,11 +59,6 @@ public static IDisposable SetupDefaultSuspendResume(this ISuspensionHost item, I
throw new ArgumentNullException(nameof(item));
}

if (item.AppState == null)
{
throw new NullReferenceException(nameof(item.AppState));
}

var ret = new CompositeDisposable();
driver ??= Locator.Current.GetService<ISuspensionDriver>();

Expand All @@ -77,7 +68,7 @@ public static IDisposable SetupDefaultSuspendResume(this ISuspensionHost item, I
.Subscribe(_ => item.Log().Info("Invalidated app state")));

ret.Add(item.ShouldPersistState
.SelectMany(x => driver.SaveState(item.AppState).Finally(x.Dispose))
.SelectMany(x => driver.SaveState(item.AppState!).Finally(x.Dispose))
.LoggedCatch(item, Observables.Unit, "Tried to persist app state")
.Subscribe(_ => item.Log().Info("Persisted application state")));

Expand Down