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

Add Subscribe overload with riseOnSubscription parameter #260

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 36 additions & 0 deletions src/R3/ObservableSubscribeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public static IDisposable Subscribe<T>(this Observable<T> source, Action<T> onNe
return source.Subscribe(new AnonymousObserver<T>(onNext, onErrorResume, onCompleted));
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T>(this Observable<T> source, Action<T> onNext, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T>(onNext, ObservableSystem.GetUnhandledExceptionHandler(), Stubs.HandleResult), riseOnSubscription);
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T>(this Observable<T> source, Action<T> onNext, Action<Result> onCompleted, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T>(onNext, ObservableSystem.GetUnhandledExceptionHandler(), onCompleted), riseOnSubscription);
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T>(this Observable<T> source, Action<T> onNext, Action<Exception> onErrorResume, Action<Result> onCompleted, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T>(onNext, onErrorResume, onCompleted), riseOnSubscription);
}

// with state

[DebuggerStepThrough]
Expand All @@ -47,6 +65,24 @@ public static IDisposable Subscribe<T, TState>(this Observable<T> source, TState
{
return source.Subscribe(new AnonymousObserver<T, TState>(onNext, onErrorResume, onCompleted, state));
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T, TState>(this Observable<T> source, TState state, Action<T, TState> onNext, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T, TState>(onNext, Stubs<TState>.HandleException, Stubs<TState>.HandleResult, state), riseOnSubscription);
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T, TState>(this Observable<T> source, TState state, Action<T, TState> onNext, Action<Result, TState> onCompleted, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T, TState>(onNext, Stubs<TState>.HandleException, onCompleted, state), riseOnSubscription);
}

[DebuggerStepThrough]
public static IDisposable Subscribe<T, TState>(this Observable<T> source, TState state, Action<T, TState> onNext, Action<Exception, TState> onErrorResume, Action<Result, TState> onCompleted, bool riseOnSubscription)
{
return source.Subscribe(new AnonymousObserver<T, TState>(onNext, onErrorResume, onCompleted, state), riseOnSubscription);
}
}

[DebuggerStepThrough]
Expand Down
15 changes: 14 additions & 1 deletion src/R3/ReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public void OnCompleted(Result result)
}

protected override IDisposable SubscribeCore(Observer<T> observer)
{
return SubscribeCore(observer, true);
}

protected IDisposable SubscribeCore(Observer<T> observer, bool riseOnSubscription)
{
Result? completedResult;
lock (this)
Expand All @@ -210,7 +215,10 @@ protected override IDisposable SubscribeCore(Observer<T> observer)
}

// raise latest value on subscribe(before add observer to list)
observer.OnNext(currentValue);
if (riseOnSubscription)
{
observer.OnNext(currentValue);
}

lock (this)
{
Expand Down Expand Up @@ -245,6 +253,11 @@ protected override IDisposable SubscribeCore(Observer<T> observer)
return Disposable.Empty;
}

public IDisposable Subscribe(Observer<T> observer, bool riseOnSubscription = true)
{
return SubscribeCore(observer, riseOnSubscription);
}

void ThrowIfDisposed()
{
if (IsDisposed) throw new ObjectDisposedException("");
Expand Down
28 changes: 27 additions & 1 deletion tests/R3.Tests/ReactivePropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void NodeDisposeCheck()

list1.AssertEqual([1, 10, 20]);
list2.AssertEqual([1, 10, 20]);
list3.AssertEqual([1, 10, 20]);
list3.AssertEqual([1, 10]);
list4.AssertEqual([1]);
list5.AssertEqual([1, 10, 20]);
}
Expand Down Expand Up @@ -415,4 +415,30 @@ public void RemoveLast()
[P2]2
""");
}

[Fact]
public void SubscribeWithRiseOnSubscription()
{
var rp = new ReactiveProperty<int>(100);
var log = new List<int>();

rp.Subscribe(new AnonymousObserver<int>(log.Add, _ => { }, _ => { }), riseOnSubscription: true);
log.Should().Equal(100);

rp.Value = 200;
log.Should().Equal(100, 200);
}

[Fact]
public void SubscribeWithoutRiseOnSubscription()
{
var rp = new ReactiveProperty<int>(100);
var log = new List<int>();

rp.Subscribe(new AnonymousObserver<int>(log.Add, _ => { }, _ => { }), riseOnSubscription: false);
log.Should().BeEmpty();

rp.Value = 200;
log.Should().Equal(200);
}
}