我很難理解主題對象。無功擴展,主題<T>
考慮下面的代碼:
var sub = new Subject<int>();
sub.Subscribe(x => Console.WriteLine(x)); //subscriber #1
sub.Subscribe(x => Console.WriteLine(x)); //subscriber #2
sub.OnNext(2);
我創建int類型的題目,當我執行OnNext它調用其它付費用戶(#1和#2)。 我沒有得到的東西是我讀的那個主體意味着一個對象,它既是可觀察的,又是觀察者,但是這是如何解釋爲什麼當我調用OnNext時,其他用戶被調用。
我會理解,如果主題的OnNext會將其傳播給所有訂閱者=發佈給所有其他人(這是有道理的),但是當我檢查源代碼時,我看不到任何內容,請參閱下文。
有人可能從下面的代碼瞭解究竟是什麼使OnNext(2)傳播到其他訂閱? (#1,#2)?
公共密封類主題:ISubject,ISubject,IObserver,的IObservable,IDisposable的 {// 領域 私人揮發性IObserver _observer;
// Methods
public Subject()
{
this._observer = NopObserver<T>.Instance;
}
public void Dispose()
{
this._observer = DisposedObserver<T>.Instance;
}
public void OnCompleted()
{
IObserver<T> comparand = null;
IObserver<T> completed = DoneObserver<T>.Completed;
do
{
comparand = this._observer;
}
while (((comparand != DisposedObserver<T>.Instance) && !(comparand is DoneObserver<T>)) && (Interlocked.CompareExchange<IObserver<T>>(ref this._observer, completed, comparand) != comparand));
comparand.OnCompleted();
}
public void OnError(Exception error)
{
if (error == null)
{
throw new ArgumentNullException("error");
}
IObserver<T> comparand = null;
DoneObserver<T> observer3 = new DoneObserver<T> {
Exception = error
};
DoneObserver<T> observer2 = observer3;
do
{
comparand = this._observer;
}
while (((comparand != DisposedObserver<T>.Instance) && !(comparand is DoneObserver<T>)) && (Interlocked.CompareExchange<IObserver<T>>(ref this._observer, observer2, comparand) != comparand));
comparand.OnError(error);
}
public void OnNext(T value)
{
this._observer.OnNext(value);
}
public IDisposable Subscribe(IObserver<T> observer)
{
if (observer == null)
{
throw new ArgumentNullException("observer");
}
IObserver<T> comparand = null;
IObserver<T> observer3 = null;
do
{
comparand = this._observer;
if (comparand == DisposedObserver<T>.Instance)
{
throw new ObjectDisposedException("");
}
if (comparand == DoneObserver<T>.Completed)
{
observer.OnCompleted();
return Disposable.Empty;
}
DoneObserver<T> observer4 = comparand as DoneObserver<T>;
if (observer4 != null)
{
observer.OnError(observer4.Exception);
return Disposable.Empty;
}
if (comparand == NopObserver<T>.Instance)
{
observer3 = observer;
}
else
{
Observer<T> observer5 = comparand as Observer<T>;
if (observer5 != null)
{
observer3 = observer5.Add(observer);
}
else
{
observer3 = new Observer<T>(new ImmutableList<IObserver<T>>(new IObserver<T>[] { comparand, observer }));
}
}
}
while (Interlocked.CompareExchange<IObserver<T>>(ref this._observer, observer3, comparand) != comparand);
return new Subscription<T>((Subject<T>) this, observer);
}
private void Unsubscribe(IObserver<T> observer)
{
IObserver<T> comparand = null;
IObserver<T> instance = null;
Label_0004:
comparand = this._observer;
if ((comparand != DisposedObserver<T>.Instance) && !(comparand is DoneObserver<T>))
{
Observer<T> observer4 = comparand as Observer<T>;
if (observer4 != null)
{
instance = observer4.Remove(observer);
}
else
{
if (comparand != observer)
{
return;
}
instance = NopObserver<T>.Instance;
}
if (Interlocked.CompareExchange<IObserver<T>>(ref this._observer, instance, comparand) != comparand)
{
goto Label_0004;
}
}
}
// Properties
public bool HasObservers
{
get
{
return (((this._observer != NopObserver<T>.Instance) && !(this._observer is DoneObserver<T>)) && (this._observer != DisposedObserver<T>.Instance));
}
}
// Nested Types
private class Subscription : IDisposable
{
// Fields
private IObserver<T> _observer;
private Subject<T> _subject;
// Methods
public Subscription(Subject<T> subject, IObserver<T> observer)
{
this._subject = subject;
this._observer = observer;
}
public void Dispose()
{
IObserver<T> observer = Interlocked.Exchange<IObserver<T>>(ref this._observer, null);
if (observer != null)
{
this._subject.Unsubscribe(observer);
this._subject = null;
}
}
}
}
順便說一句,在複雜這裏是[接收1.1/2.0的性能優化]的一部分(http://blogs.msdn.com/b/rxteam/archive/2012/03/12/reactive-extensions- v2-0-β-可供now.aspx)。 (§提高生產者速度)。 – 2014-09-09 20:13:03