CombineLatest<TFirst, TSecond, TResult>
sealed class CombineLatest<TFirst, TSecond, TResult> : Producer<TResult, _<TFirst, TSecond, TResult>>
using System.Reactive.Disposables;
using System.Runtime.CompilerServices;
namespace System.Reactive.Linq.ObservableImpl
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1
})]
internal sealed class CombineLatest<[System.Runtime.CompilerServices.Nullable(2)] TFirst, [System.Runtime.CompilerServices.Nullable(2)] TSecond, [System.Runtime.CompilerServices.Nullable(2)] TResult> : Producer<TResult, CombineLatest<TFirst, TSecond, TResult>._>
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
internal sealed class _ : IdentitySink<TResult>
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class FirstObserver : IObserver<TFirst>
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})]
private readonly _ _parent;
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})]
private SecondObserver _other;
public bool HasValue { get; set; }
public TFirst Value { get; set; }
public bool Done { get; set; }
public FirstObserver([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})] _ parent)
{
_parent = parent;
_other = null;
}
public void SetOther([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})] SecondObserver other)
{
_other = other;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public void OnNext(TFirst value)
{
lock (_parent._gate) {
HasValue = true;
Value = value;
if (_other.HasValue) {
TResult value2;
try {
value2 = _parent._resultSelector(value, _other.Value);
} catch (Exception error) {
_parent.ForwardOnError(error);
return;
}
_parent.ForwardOnNext(value2);
} else if (_other.Done) {
_parent.ForwardOnCompleted();
}
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
public void OnError(Exception error)
{
lock (_parent._gate) {
_parent.ForwardOnError(error);
}
}
public void OnCompleted()
{
lock (_parent._gate) {
Done = true;
if (_other.Done)
_parent.ForwardOnCompleted();
else
_parent._firstDisposable.Dispose();
}
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class SecondObserver : IObserver<TSecond>
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})]
private readonly _ _parent;
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})]
private FirstObserver _other;
public bool HasValue { get; set; }
public TSecond Value { get; set; }
public bool Done { get; set; }
public SecondObserver([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})] _ parent)
{
_parent = parent;
_other = null;
}
public void SetOther([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})] FirstObserver other)
{
_other = other;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public void OnNext(TSecond value)
{
lock (_parent._gate) {
HasValue = true;
Value = value;
if (_other.HasValue) {
TResult value2;
try {
value2 = _parent._resultSelector(_other.Value, value);
} catch (Exception error) {
_parent.ForwardOnError(error);
return;
}
_parent.ForwardOnNext(value2);
} else if (_other.Done) {
_parent.ForwardOnCompleted();
}
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
public void OnError(Exception error)
{
lock (_parent._gate) {
_parent.ForwardOnError(error);
}
}
public void OnCompleted()
{
lock (_parent._gate) {
Done = true;
if (_other.Done)
_parent.ForwardOnCompleted();
else
_parent._secondDisposable.Dispose();
}
}
}
private readonly Func<TFirst, TSecond, TResult> _resultSelector;
private readonly object _gate = new object();
private SingleAssignmentDisposableValue _firstDisposable;
private SingleAssignmentDisposableValue _secondDisposable;
public _(Func<TFirst, TSecond, TResult> resultSelector, IObserver<TResult> observer)
: base(observer)
{
_resultSelector = resultSelector;
}
public void Run(IObservable<TFirst> first, IObservable<TSecond> second)
{
FirstObserver firstObserver = new FirstObserver(this);
SecondObserver secondObserver = new SecondObserver(this);
firstObserver.SetOther(secondObserver);
secondObserver.SetOther(firstObserver);
_firstDisposable.Disposable = ObservableExtensions.SubscribeSafe<TFirst>(first, (IObserver<TFirst>)firstObserver);
_secondDisposable.Disposable = ObservableExtensions.SubscribeSafe<TSecond>(second, (IObserver<TSecond>)secondObserver);
}
protected override void Dispose(bool disposing)
{
if (disposing) {
_firstDisposable.Dispose();
_secondDisposable.Dispose();
}
base.Dispose(disposing);
}
}
private readonly IObservable<TFirst> _first;
private readonly IObservable<TSecond> _second;
private readonly Func<TFirst, TSecond, TResult> _resultSelector;
public CombineLatest(IObservable<TFirst> first, IObservable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
{
_first = first;
_second = second;
_resultSelector = resultSelector;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})]
protected override _ CreateSink(IObserver<TResult> observer)
{
return new _(_resultSelector, observer);
}
protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0,
0
})] _ sink)
{
sink.Run(_first, _second);
}
}
}