1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Threading;
namespace System.Reactive
{
//
// See AutoDetachObserver.cs for more information on the safeguarding requirement and
// its implementation aspects.
//
/// <summary>
/// This class fuses logic from ObserverBase, AnonymousObserver, and SafeObserver into one class. When an observer
/// needs to be safeguarded, an instance of this type can be created by SafeObserver.Create when it detects its
/// input is an AnonymousObserver, which is commonly used by end users when using the Subscribe extension methods
/// that accept delegates for the On* handlers. By doing the fusion, we make the call stack depth shorter which
/// helps debugging and some performance.
/// </summary>
class AnonymousSafeObserver<T> : IObserver<T>
{
private readonly Action<T> _onNext;
private readonly Action<Exception> _onError;
private readonly Action _onCompleted;
private readonly IDisposable _disposable;
private int isStopped;
public AnonymousSafeObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted, IDisposable disposable)
{
_onNext = onNext;
_onError = onError;
_onCompleted = onCompleted;
_disposable = disposable;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
var __noError = false;
try
{
_onNext(value);
__noError = true;
}
finally
{
if (!__noError)
_disposable.Dispose();
}
}
}
public void OnError(Exception error)
{
if (Interlocked.Exchange(ref isStopped, 1) == 0)
{
try
{
_onError(error);
}
finally
{
_disposable.Dispose();
}
}
}
public void OnCompleted()
{
if (Interlocked.Exchange(ref isStopped, 1) == 0)
{
try
{
_onCompleted();
}
finally
{
_disposable.Dispose();
}
}
}
}
}
|