-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
9 changed files
with
219 additions
and
16 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
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,54 @@ | ||
namespace R3; | ||
|
||
public static partial class ObservableExtensions | ||
{ | ||
public static Observable<T> Append<T>(this Observable<T> source, T value) | ||
{ | ||
return new AppendPrepend<T>(source, value, append: true); | ||
} | ||
|
||
public static Observable<T> Prepend<T>(this Observable<T> source, T value) | ||
{ | ||
return new AppendPrepend<T>(source, value, append: false); | ||
} | ||
} | ||
|
||
|
||
internal sealed class AppendPrepend<T>(Observable<T> source, T value, bool append) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
if (!append) // prepend | ||
{ | ||
observer.OnNext(value); | ||
} | ||
|
||
return source.Subscribe(new _Append(observer, value)); | ||
} | ||
|
||
sealed class _Append(Observer<T> observer, T value) : Observer<T> | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
observer.OnNext(value); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
observer.OnCompleted(result); | ||
} | ||
else | ||
{ | ||
observer.OnNext(value); | ||
observer.OnCompleted(); | ||
} | ||
} | ||
} | ||
} |
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,84 @@ | ||
namespace R3; | ||
|
||
public static partial class ObservableExtensions | ||
{ | ||
public static Observable<T> Concat<T>(this Observable<T> source, Observable<T> second) | ||
{ | ||
return new Concat<T>(source, second); | ||
} | ||
} | ||
|
||
|
||
internal sealed class Concat<T>(Observable<T> source, Observable<T> second) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
return source.Subscribe(new _Concat(observer, second)); | ||
} | ||
|
||
sealed class _Concat(Observer<T> observer, Observable<T> second) : Observer<T> | ||
{ | ||
readonly Observer<T> observer = observer; | ||
|
||
SingleAssignmentDisposableCore secondSubscription; | ||
|
||
protected override bool AutoDisposeOnCompleted => false; | ||
|
||
protected override void OnNextCore(T value) | ||
{ | ||
observer.OnNext(value); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
try | ||
{ | ||
observer.OnCompleted(result); | ||
} | ||
finally | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
else | ||
{ | ||
secondSubscription.Disposable = second.Subscribe(new SecondObserver(this)); | ||
} | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
secondSubscription.Dispose(); | ||
} | ||
|
||
internal sealed class SecondObserver(_Concat parent) : Observer<T> | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
parent.observer.OnNext(value); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
parent.observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
parent.observer.OnCompleted(result); | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
parent.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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
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,57 @@ | ||
namespace R3.Tests.OperatorTests; | ||
|
||
public class ConcatAppendPrependTest | ||
{ | ||
[Fact] | ||
public void Prepend() | ||
{ | ||
var subject = new Subject<int>(); | ||
using var list = subject.Prepend(9999).ToLiveList(); | ||
|
||
subject.OnNext(10); | ||
|
||
list.AssertEqual([9999, 10]); | ||
|
||
subject.OnCompleted(); | ||
list.AssertIsCompleted(); | ||
} | ||
|
||
[Fact] | ||
public void Append() | ||
{ | ||
var subject = new Subject<int>(); | ||
using var list = subject.Append(9999).ToLiveList(); | ||
|
||
subject.OnNext(10); | ||
|
||
list.AssertEqual([10]); | ||
|
||
subject.OnCompleted(); | ||
|
||
list.AssertEqual([10, 9999]); | ||
list.AssertIsCompleted(); | ||
} | ||
|
||
[Fact] | ||
public void Concat() | ||
{ | ||
var subject = new Subject<int>(); | ||
var subject2 = new Subject<int>(); | ||
using var list = subject.Concat(subject2).ToLiveList(); | ||
|
||
subject.OnNext(10); | ||
subject2.OnNext(9999); | ||
|
||
list.AssertEqual([10]); | ||
|
||
subject.OnCompleted(); | ||
|
||
subject2.OnNext(11111); | ||
|
||
list.AssertEqual([10, 11111]); | ||
|
||
subject2.OnCompleted(); | ||
|
||
list.AssertIsCompleted(); | ||
} | ||
} |
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