-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Cysharp/hadashiA/operators-concat
Add operator Concat(Observable<Observable<T>>)
- Loading branch information
Showing
2 changed files
with
233 additions
and
2 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,143 @@ | ||
namespace R3; | ||
|
||
public static partial class Observable | ||
{ | ||
public static Observable<T> Concat<T>(this Observable<Observable<T>> sources) | ||
{ | ||
return new ConcatMany<T>(sources); | ||
} | ||
} | ||
|
||
internal sealed class ConcatMany<T>(Observable<Observable<T>> sources) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
return sources.Subscribe(new _ConcatMany(observer)); | ||
} | ||
|
||
sealed class _ConcatMany(Observer<T> observer) : Observer<Observable<T>> | ||
{ | ||
readonly Observer<T> observer = observer; | ||
readonly object gate = new(); | ||
readonly Queue<Observable<T>> q = new(); | ||
|
||
SerialDisposableCore serialDisposable; | ||
bool isStopped; | ||
int activeCount; | ||
|
||
// keep when inner is running | ||
protected override bool AutoDisposeOnCompleted => false; | ||
|
||
protected override void OnNextCore(Observable<T> value) | ||
{ | ||
lock (gate) | ||
{ | ||
if (activeCount < 1) | ||
{ | ||
activeCount++; | ||
serialDisposable.Disposable = value.Subscribe(new ConcatInner(this)); | ||
} | ||
else | ||
{ | ||
q.Enqueue(value); | ||
} | ||
} | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
lock (gate) | ||
{ | ||
observer.OnErrorResume(error); | ||
} | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
PublishCompleted(result); | ||
} | ||
else | ||
{ | ||
lock (gate) | ||
{ | ||
isStopped = true; | ||
if (activeCount == 0) | ||
{ | ||
PublishCompleted(result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
serialDisposable.Dispose(); | ||
} | ||
|
||
void PublishCompleted(Result result) | ||
{ | ||
try | ||
{ | ||
lock (gate) | ||
{ | ||
observer.OnCompleted(result); | ||
} | ||
} | ||
finally | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
|
||
sealed class ConcatInner(_ConcatMany parent) : Observer<T> | ||
{ | ||
// Manual disposing by SerialDisposableCore | ||
protected override bool AutoDisposeOnCompleted => false; | ||
|
||
protected override void OnNextCore(T value) | ||
{ | ||
lock (parent.gate) | ||
{ | ||
parent.observer.OnNext(value); | ||
} | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
lock (parent.gate) | ||
{ | ||
parent.observer.OnErrorResume(error); | ||
} | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
parent.OnCompleted(); | ||
} | ||
else | ||
{ | ||
lock (parent.gate) | ||
{ | ||
if (parent.q.Count > 0) | ||
{ | ||
var nextSource = parent.q.Dequeue(); | ||
parent.serialDisposable.Disposable = nextSource.Subscribe(new ConcatInner(parent)); | ||
} | ||
else | ||
{ | ||
parent.activeCount--; | ||
if (parent is { isStopped: true, activeCount: 0 }) | ||
{ | ||
parent.PublishCompleted(result); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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