123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
- #if !NO_PERF
- using System;
- using System.Collections.Generic;
- using System.Reactive.Disposables;
- namespace System.Reactive.Linq.ObservableImpl
- {
- class Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> : Producer<TResult>
- {
- private readonly IObservable<TLeft> _left;
- private readonly IObservable<TRight> _right;
- private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
- private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
- private readonly Func<TLeft, TRight, TResult> _resultSelector;
- public Join(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, IObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
- {
- _left = left;
- _right = right;
- _leftDurationSelector = leftDurationSelector;
- _rightDurationSelector = rightDurationSelector;
- _resultSelector = resultSelector;
- }
- protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(this, observer, cancel);
- setSink(sink);
- return sink.Run();
- }
- class _ : Sink<TResult>
- {
- private readonly Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> _parent;
- public _(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _parent = parent;
- }
- private object _gate;
- private CompositeDisposable _group;
- private bool _leftDone;
- private int _leftID;
- private SortedDictionary<int, TLeft> _leftMap;
-
- private bool _rightDone;
- private int _rightID;
- private SortedDictionary<int, TRight> _rightMap;
- public IDisposable Run()
- {
- _gate = new object();
- _group = new CompositeDisposable();
-
- var leftSubscription = new SingleAssignmentDisposable();
- _group.Add(leftSubscription);
- _leftDone = false;
- _leftID = 0;
- _leftMap = new SortedDictionary<int, TLeft>();
- var rightSubscription = new SingleAssignmentDisposable();
- _group.Add(rightSubscription);
- _rightDone = false;
- _rightID = 0;
- _rightMap = new SortedDictionary<int, TRight>();
- leftSubscription.Disposable = _parent._left.SubscribeSafe(new LeftObserver(this, leftSubscription));
- rightSubscription.Disposable = _parent._right.SubscribeSafe(new RightObserver(this, rightSubscription));
- return _group;
- }
- class LeftObserver : IObserver<TLeft>
- {
- private readonly _ _parent;
- private readonly IDisposable _self;
- public LeftObserver(_ parent, IDisposable self)
- {
- _parent = parent;
- _self = self;
- }
- private void Expire(int id, IDisposable resource)
- {
- lock (_parent._gate)
- {
- if (_parent._leftMap.Remove(id) && _parent._leftMap.Count == 0 && _parent._leftDone)
- {
- _parent._observer.OnCompleted();
- _parent.Dispose();
- }
- }
- _parent._group.Remove(resource);
- }
- public void OnNext(TLeft value)
- {
- var id = 0;
- var rightID = 0;
- lock (_parent._gate)
- {
- id = _parent._leftID++;
- rightID = _parent._rightID;
- _parent._leftMap.Add(id, value);
- }
- var md = new SingleAssignmentDisposable();
- _parent._group.Add(md);
- var duration = default(IObservable<TLeftDuration>);
- try
- {
- duration = _parent._parent._leftDurationSelector(value);
- }
- catch (Exception exception)
- {
- _parent._observer.OnError(exception);
- _parent.Dispose();
- return;
- }
- md.Disposable = duration.SubscribeSafe(new Delta(this, id, md));
- lock (_parent._gate)
- {
- foreach (var rightValue in _parent._rightMap)
- {
- if (rightValue.Key < rightID)
- {
- var result = default(TResult);
- try
- {
- result = _parent._parent._resultSelector(value, rightValue.Value);
- }
- catch (Exception exception)
- {
- _parent._observer.OnError(exception);
- _parent.Dispose();
- return;
- }
- _parent._observer.OnNext(result);
- }
- }
- }
- }
- class Delta : IObserver<TLeftDuration>
- {
- private readonly LeftObserver _parent;
- private readonly int _id;
- private readonly IDisposable _self;
- public Delta(LeftObserver parent, int id, IDisposable self)
- {
- _parent = parent;
- _id = id;
- _self = self;
- }
- public void OnNext(TLeftDuration value)
- {
- _parent.Expire(_id, _self);
- }
- public void OnError(Exception error)
- {
- _parent.OnError(error);
- }
- public void OnCompleted()
- {
- _parent.Expire(_id, _self);
- }
- }
- public void OnError(Exception error)
- {
- lock (_parent._gate)
- {
- _parent._observer.OnError(error);
- _parent.Dispose();
- }
- }
- public void OnCompleted()
- {
- lock (_parent._gate)
- {
- _parent._leftDone = true;
- if (_parent._rightDone || _parent._leftMap.Count == 0)
- {
- _parent._observer.OnCompleted();
- _parent.Dispose();
- }
- else
- {
- _self.Dispose();
- }
- }
- }
- }
- class RightObserver : IObserver<TRight>
- {
- private readonly _ _parent;
- private readonly IDisposable _self;
- public RightObserver(_ parent, IDisposable self)
- {
- _parent = parent;
- _self = self;
- }
- private void Expire(int id, IDisposable resource)
- {
- lock (_parent._gate)
- {
- if (_parent._rightMap.Remove(id) && _parent._rightMap.Count == 0 && _parent._rightDone)
- {
- _parent._observer.OnCompleted();
- _parent.Dispose();
- }
- }
- _parent._group.Remove(resource);
- }
- public void OnNext(TRight value)
- {
- var id = 0;
- var leftID = 0;
- lock (_parent._gate)
- {
- id = _parent._rightID++;
- leftID = _parent._leftID;
- _parent._rightMap.Add(id, value);
- }
- var md = new SingleAssignmentDisposable();
- _parent._group.Add(md);
- var duration = default(IObservable<TRightDuration>);
- try
- {
- duration = _parent._parent._rightDurationSelector(value);
- }
- catch (Exception exception)
- {
- _parent._observer.OnError(exception);
- _parent.Dispose();
- return;
- }
- md.Disposable = duration.SubscribeSafe(new Delta(this, id, md));
- lock (_parent._gate)
- {
- foreach (var leftValue in _parent._leftMap)
- {
- if (leftValue.Key < leftID)
- {
- var result = default(TResult);
- try
- {
- result = _parent._parent._resultSelector(leftValue.Value, value);
- }
- catch (Exception exception)
- {
- _parent._observer.OnError(exception);
- _parent.Dispose();
- return;
- }
- _parent._observer.OnNext(result);
- }
- }
- }
- }
- class Delta : IObserver<TRightDuration>
- {
- private readonly RightObserver _parent;
- private readonly int _id;
- private readonly IDisposable _self;
- public Delta(RightObserver parent, int id, IDisposable self)
- {
- _parent = parent;
- _id = id;
- _self = self;
- }
- public void OnNext(TRightDuration value)
- {
- _parent.Expire(_id, _self);
- }
- public void OnError(Exception error)
- {
- _parent.OnError(error);
- }
- public void OnCompleted()
- {
- _parent.Expire(_id, _self);
- }
- }
- public void OnError(Exception error)
- {
- lock (_parent._gate)
- {
- _parent._observer.OnError(error);
- _parent.Dispose();
- }
- }
- public void OnCompleted()
- {
- lock (_parent._gate)
- {
- _parent._rightDone = true;
- if (_parent._leftDone || _parent._rightMap.Count == 0)
- {
- _parent._observer.OnCompleted();
- _parent.Dispose();
- }
- else
- {
- _self.Dispose();
- }
- }
- }
- }
- }
- }
- }
- #endif
|