-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace R3; | ||
|
||
public static partial class ObservableExtensions | ||
{ | ||
public static Observable<T> Do<T>(this Observable<T> source, Action<T>? onNext = null, Action<Exception>? onErrorResume = null, Action<Result>? onCompleted = null, Action? onDispose = null, Action? onSubscribe = null) | ||
{ | ||
return new Do<T>(source, onNext, onErrorResume, onCompleted, onDispose, onSubscribe); | ||
} | ||
|
||
public static Observable<T> Do<T, TState>(this Observable<T> source, TState state, Action<T, TState>? onNext = null, Action<Exception, TState>? onErrorResume = null, Action<Result, TState>? onCompleted = null, Action<TState>? onDispose = null, Action<TState>? onSubscribe = null) | ||
{ | ||
return new Do<T, TState>(source, state, onNext, onErrorResume, onCompleted, onDispose, onSubscribe); | ||
} | ||
|
||
public static Observable<T> CancelOnCompleted<T>(this Observable<T> source, CancellationTokenSource cancellationTokenSource) | ||
{ | ||
return Do(source, cancellationTokenSource, onCompleted: (_, state) => state.Cancel()); | ||
} | ||
} | ||
|
||
internal sealed class Do<T>(Observable<T> source, Action<T>? onNext, Action<Exception>? onErrorResume, Action<Result>? onCompleted, Action? onDispose, Action? onSubscribe) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
onSubscribe?.Invoke(); | ||
return source.Subscribe(new _Do(observer, onNext, onErrorResume, onCompleted, onDispose)); | ||
} | ||
|
||
internal sealed class _Do(Observer<T> observer, Action<T>? onNext, Action<Exception>? onErrorResume, Action<Result>? onCompleted, Action? onDispose) : Observer<T> | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
onNext?.Invoke(value); | ||
observer.OnNext(value); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
onErrorResume?.Invoke(error); | ||
observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
onCompleted?.Invoke(result); | ||
observer.OnCompleted(); | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
onDispose?.Invoke(); | ||
} | ||
} | ||
} | ||
|
||
internal sealed class Do<T, TState>(Observable<T> source, TState state, Action<T, TState>? onNext, Action<Exception, TState>? onErrorResume, Action<Result, TState>? onCompleted, Action<TState>? onDispose, Action<TState>? onSubscribe) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
onSubscribe?.Invoke(state); | ||
return source.Subscribe(new _Do(observer, state, onNext, onErrorResume, onCompleted, onDispose)); | ||
} | ||
|
||
internal sealed class _Do(Observer<T> observer, TState state, Action<T, TState>? onNext, Action<Exception, TState>? onErrorResume, Action<Result, TState>? onCompleted, Action<TState>? onDispose) : Observer<T> | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
onNext?.Invoke(value, state); | ||
observer.OnNext(value); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
onErrorResume?.Invoke(error, state); | ||
observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
onCompleted?.Invoke(result, state); | ||
observer.OnCompleted(); | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
onDispose?.Invoke(state); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace R3.Tests.OperatorTests; | ||
|
||
public class DoTest | ||
{ | ||
[Fact] | ||
public void Test() | ||
{ | ||
var subject = new Subject<int>(); | ||
|
||
List<int> onNext = new(); | ||
List<Exception> onErrorResume = new(); | ||
List<Result> onCompleted = new(); | ||
bool disposeCalled = false; | ||
bool subscribeCalled = false; | ||
var source = subject.Do(onNext.Add, onErrorResume.Add, onCompleted.Add, () => disposeCalled = true, () => subscribeCalled = true); | ||
|
||
subscribeCalled.Should().BeFalse(); | ||
|
||
var list = source.ToLiveList(); | ||
|
||
subscribeCalled.Should().BeTrue(); | ||
|
||
subject.OnNext(10); | ||
subject.OnNext(20); | ||
subject.OnNext(30); | ||
subject.OnErrorResume(new Exception("a")); | ||
subject.OnErrorResume(new Exception("b")); | ||
subject.OnErrorResume(new Exception("c")); | ||
|
||
onNext.Should().Equal([10, 20, 30]); | ||
onErrorResume.Select(x => x.Message).Should().Equal(["a", "b", "c"]); | ||
|
||
|
||
disposeCalled.Should().BeFalse(); | ||
|
||
subject.OnCompleted(); | ||
onCompleted.Should().ContainSingle(); | ||
|
||
disposeCalled.Should().BeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters