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
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ public void OAPHDeferSubscriptionParameterIsSubscribedIsNotTrueInitially()
Assert.True(fixture.IsSubscribed);
}

[Fact]
public void OAPHDeferSubscriptionShouldNotThrowIfDisposed()
{
var observable = Observable.Create<int>(o =>
{
o.OnNext(42);
o.OnCompleted();

return Disposable.Empty;
});

var fixture = new ObservableAsPropertyHelper<int>(observable, _ => { }, 0, true);

Assert.False(fixture.IsSubscribed);
fixture.Dispose();
var ex = Record.Exception(() => Assert.Equal(0, fixture.Value));
Assert.Null(ex);
}

[Fact]
public void OAPHShouldRethrowErrors()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public sealed class ObservableAsPropertyHelper<T> : IHandleObservableErrors, IDi
/// A value indicating whether the <see cref="ObservableAsPropertyHelper{T}"/>
/// should defer the subscription to the <paramref name="observable"/> source
/// until the first call to <see cref="Value"/>, or if it should immediately
/// subscribe to the the <paramref name="observable"/> source.
/// subscribe to the <paramref name="observable"/> source.
/// </param>
/// <param name="scheduler">
/// The scheduler that the notifications will be provided on -
Expand Down Expand Up @@ -85,7 +85,7 @@ public ObservableAsPropertyHelper(
/// A value indicating whether the <see cref="ObservableAsPropertyHelper{T}"/>
/// should defer the subscription to the <paramref name="observable"/> source
/// until the first call to <see cref="Value"/>, or if it should immediately
/// subscribe to the the <paramref name="observable"/> source.
/// subscribe to the <paramref name="observable"/> source.
/// </param>
/// <param name="scheduler">
/// The scheduler that the notifications will provided on - this
Expand Down Expand Up @@ -136,7 +136,12 @@ public T Value
{
if (Interlocked.CompareExchange(ref _activated, 1, 0) == 0)
{
_source.Subscribe(_subject).DisposeWith(_disposable);
// Do not subscribe if disposed
var localReferenceInCaseDisposeIsCalled = _disposable;
if (localReferenceInCaseDisposeIsCalled != null)
{
_source.Subscribe(_subject).DisposeWith(localReferenceInCaseDisposeIsCalled);
}
}

return _lastValue;
Expand Down