| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System; |
| 5 |
using System.Reactive.Disposables; |
| 6 |
|
| 7 |
namespace System.Reactive.Linq.ObservableImpl |
| 8 |
{ |
| 9 |
class Switch<TSource> : Producer<TSource> |
| 10 |
{ |
| 11 |
private readonly IObservable<IObservable<TSource>> _sources; |
| 12 |
|
| 13 |
public Switch(IObservable<IObservable<TSource>> sources) |
| 14 |
{ |
| 15 |
_sources = sources; |
| 16 |
} |
| 17 |
|
| 18 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 19 |
{ |
| 20 |
var sink = new _(this, observer, cancel); |
| 21 |
setSink(sink); |
| 22 |
return sink.Run(); |
| 23 |
} |
| 24 |
|
| 25 |
class _ : Sink<TSource>, IObserver<IObservable<TSource>> |
| 26 |
{ |
| 27 |
private readonly Switch<TSource> _parent; |
| 28 |
|
| 29 |
public _(Switch<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 30 |
: base(observer, cancel) |
| 31 |
{ |
| 32 |
_parent = parent; |
| 33 |
} |
| 34 |
|
| 35 |
private object _gate; |
| 36 |
private IDisposable _subscription; |
| 37 |
private SerialDisposable _innerSubscription; |
| 38 |
private bool _isStopped; |
| 39 |
private ulong _latest; |
| 40 |
private bool _hasLatest; |
| 41 |
|
| 42 |
public IDisposable Run() |
| 43 |
{ |
| 44 |
_gate = new object(); |
| 45 |
_innerSubscription = new SerialDisposable(); |
| 46 |
_isStopped = false; |
| 47 |
_latest = 0UL; |
| 48 |
_hasLatest = false; |
| 49 |
|
| 50 |
var subscription = new SingleAssignmentDisposable(); |
| 51 |
_subscription = subscription; |
| 52 |
subscription.Disposable = _parent._sources.SubscribeSafe(this); |
| 53 |
|
| 54 |
return new CompositeDisposable(_subscription, _innerSubscription); |
| 55 |
} |
| 56 |
|
| 57 |
public void OnNext(IObservable<TSource> value) |
| 58 |
{ |
| 59 |
var id = default(ulong); |
| 60 |
lock (_gate) |
| 61 |
{ |
| 62 |
id = unchecked(++_latest); |
| 63 |
_hasLatest = true; |
| 64 |
} |
| 65 |
|
| 66 |
var d = new SingleAssignmentDisposable(); |
| 67 |
_innerSubscription.Disposable = d; |
| 68 |
d.Disposable = value.SubscribeSafe(new Iter(this, id, d)); |
| 69 |
} |
| 70 |
|
| 71 |
public void OnError(Exception error) |
| 72 |
{ |
| 73 |
lock (_gate) |
| 74 |
base._observer.OnError(error); |
| 75 |
|
| 76 |
base.Dispose(); |
| 77 |
} |
| 78 |
|
| 79 |
public void OnCompleted() |
| 80 |
{ |
| 81 |
lock (_gate) |
| 82 |
{ |
| 83 |
_subscription.Dispose(); |
| 84 |
|
| 85 |
_isStopped = true; |
| 86 |
if (!_hasLatest) |
| 87 |
{ |
| 88 |
base._observer.OnCompleted(); |
| 89 |
base.Dispose(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
class Iter : IObserver<TSource> |
| 95 |
{ |
| 96 |
private readonly _ _parent; |
| 97 |
private readonly ulong _id; |
| 98 |
private readonly IDisposable _self; |
| 99 |
|
| 100 |
public Iter(_ parent, ulong id, IDisposable self) |
| 101 |
{ |
| 102 |
_parent = parent; |
| 103 |
_id = id; |
| 104 |
_self = self; |
| 105 |
} |
| 106 |
|
| 107 |
public void OnNext(TSource value) |
| 108 |
{ |
| 109 |
lock (_parent._gate) |
| 110 |
{ |
| 111 |
if (_parent._latest == _id) |
| 112 |
_parent._observer.OnNext(value); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public void OnError(Exception error) |
| 117 |
{ |
| 118 |
lock (_parent._gate) |
| 119 |
{ |
| 120 |
_self.Dispose(); |
| 121 |
|
| 122 |
if (_parent._latest == _id) |
| 123 |
{ |
| 124 |
_parent._observer.OnError(error); |
| 125 |
_parent.Dispose(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public void OnCompleted() |
| 131 |
{ |
| 132 |
lock (_parent._gate) |
| 133 |
{ |
| 134 |
_self.Dispose(); |
| 135 |
|
| 136 |
if (_parent._latest == _id) |
| 137 |
{ |
| 138 |
_parent._hasLatest = false; |
| 139 |
|
| 140 |
if (_parent._isStopped) |
| 141 |
{ |
| 142 |
_parent._observer.OnCompleted(); |
| 143 |
_parent.Dispose(); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
#endif |