-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
10 changed files
with
454 additions
and
109 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,103 @@ | ||
namespace R3; | ||
|
||
public static partial class Observable | ||
{ | ||
public static Observable<T> Concat<T>(params Observable<T>[] sources) | ||
{ | ||
return new Concat<T>(sources); | ||
} | ||
|
||
public static Observable<T> Concat<T>(IEnumerable<Observable<T>> sources) | ||
{ | ||
return new Concat<T>(sources); | ||
} | ||
} | ||
|
||
public static partial class ObservableExtensions | ||
{ | ||
public static Observable<T> Concat<T>(this Observable<T> source, Observable<T> second) | ||
{ | ||
return new Concat<T>(new[] { source, second }); | ||
} | ||
} | ||
|
||
internal sealed class Concat<T>(IEnumerable<Observable<T>> sources) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
return new _Concat(observer, sources).Run(); | ||
} | ||
|
||
sealed class _Concat : IDisposable | ||
{ | ||
public Observer<T> observer; | ||
public IEnumerator<Observable<T>> enumerator; | ||
public SerialDisposableCore disposable; | ||
|
||
public _Concat(Observer<T> observer, IEnumerable<Observable<T>> sources) | ||
{ | ||
this.observer = observer; | ||
this.enumerator = sources.GetEnumerator(); | ||
} | ||
|
||
public IDisposable Run() | ||
{ | ||
if (!enumerator.MoveNext()) | ||
{ | ||
observer.OnCompleted(); | ||
enumerator.Dispose(); | ||
return Disposable.Empty; | ||
} | ||
else | ||
{ | ||
disposable.Disposable = enumerator.Current.Subscribe(new _ConcatObserver(this)); | ||
return this; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
enumerator.Dispose(); | ||
disposable.Dispose(); | ||
} | ||
} | ||
|
||
sealed class _ConcatObserver(_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) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
try | ||
{ | ||
parent.observer.OnCompleted(result); | ||
} | ||
finally | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
else | ||
{ | ||
if (parent.enumerator.MoveNext()) | ||
{ | ||
parent.disposable.Disposable = parent.enumerator.Current.Subscribe(new _ConcatObserver(parent)); | ||
} | ||
else | ||
{ | ||
parent.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,81 @@ | ||
namespace R3.Internal; | ||
|
||
internal struct ListDisposableCore : IDisposable | ||
{ | ||
IDisposable?[] disposables; | ||
int count; | ||
object gate; | ||
|
||
public ListDisposableCore(int initialCount, object gate) | ||
{ | ||
this.disposables = new IDisposable?[initialCount]; | ||
this.gate = gate; | ||
} | ||
|
||
public void Add(IDisposable disposable) | ||
{ | ||
lock (gate) | ||
{ | ||
if (disposables.Length == count) | ||
{ | ||
Array.Resize(ref disposables, count * 2); | ||
} | ||
|
||
disposables[count++] = disposable; | ||
} | ||
} | ||
|
||
public void RemoveAt(int index) | ||
{ | ||
lock (gate) | ||
{ | ||
if (index < 0 || index >= count) | ||
{ | ||
return; | ||
} | ||
|
||
ref var d = ref disposables[index]; | ||
if (d != null) | ||
{ | ||
d.Dispose(); | ||
} | ||
d = null; | ||
} | ||
} | ||
|
||
public void RemoveAllExceptAt(int index) | ||
{ | ||
lock (gate) | ||
{ | ||
if (index < 0 || index >= count) | ||
{ | ||
return; | ||
} | ||
|
||
for (int i = 0; i < count; i++) | ||
{ | ||
if (i == index) continue; | ||
|
||
ref var d = ref disposables[i]; | ||
if (d != null) | ||
{ | ||
d.Dispose(); | ||
} | ||
d = null; | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (gate) | ||
{ | ||
for (int i = 0; i < count; i++) | ||
{ | ||
disposables[i]?.Dispose(); | ||
disposables[i] = null; | ||
count = 0; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.