| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
|
| 5 |
namespace System.Reactive |
| 6 |
{ |
| 7 |
/// <summary> |
| 8 |
/// Base class for classes that expose an observable sequence as a well-known event pattern (sender, event arguments). |
| 9 |
/// Contains functionality to maintain a map of event handler delegates to observable sequence subscriptions. Subclasses |
| 10 |
/// should only add an event with custom add and remove methods calling into the base class's operations. |
| 11 |
/// </summary> |
| 12 |
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam> |
| 13 |
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam> |
| 14 |
public abstract class EventPatternSourceBase<TSender, TEventArgs> |
| 15 |
#if !NO_EVENTARGS_CONSTRAINT |
| 16 |
where TEventArgs : EventArgs |
| 17 |
#endif |
| 18 |
{ |
| 19 |
private readonly IObservable<EventPattern<TSender, TEventArgs>> _source; |
| 20 |
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions; |
| 21 |
private readonly Action<Action<TSender, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> _invokeHandler; |
| 22 |
|
| 23 |
/// <summary> |
| 24 |
/// Creates a new event pattern source. |
| 25 |
/// </summary> |
| 26 |
/// <param name="source">Source sequence to expose as an event.</param> |
| 27 |
/// <param name="invokeHandler">Delegate used to invoke the event for each element of the sequence.</param> |
| 28 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="invokeHandler"/> is null.</exception> |
| 29 |
protected EventPatternSourceBase(IObservable<EventPattern<TSender, TEventArgs>> source, Action<Action<TSender, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> invokeHandler) |
| 30 |
{ |
| 31 |
if (source == null) |
| 32 |
throw new ArgumentNullException("source"); |
| 33 |
if (invokeHandler == null) |
| 34 |
throw new ArgumentNullException("invokeHandler"); |
| 35 |
|
| 36 |
_source = source; |
| 37 |
_invokeHandler = invokeHandler; |
| 38 |
_subscriptions = new Dictionary<Delegate, Stack<IDisposable>>(); |
| 39 |
} |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Adds the specified event handler, causing a subscription to the underlying source. |
| 43 |
/// </summary> |
| 44 |
/// <param name="handler">Event handler to add. The same delegate should be passed to the Remove operation in order to remove the event handler.</param> |
| 45 |
/// <param name="invoke">Invocation delegate to raise the event in the derived class.</param> |
| 46 |
/// <exception cref="ArgumentNullException"><paramref name="handler"/> or <paramref name="invoke"/> is null.</exception> |
| 47 |
protected void Add(Delegate handler, Action<TSender, TEventArgs> invoke) |
| 48 |
{ |
| 49 |
if (handler == null) |
| 50 |
throw new ArgumentNullException("handler"); |
| 51 |
if (invoke == null) |
| 52 |
throw new ArgumentNullException("invoke"); |
| 53 |
|
| 54 |
var gate = new object(); |
| 55 |
var isAdded = false; |
| 56 |
var isDone = false; |
| 57 |
|
| 58 |
var remove = new Action(() => |
| 59 |
{ |
| 60 |
lock (gate) |
| 61 |
{ |
| 62 |
if (isAdded) |
| 63 |
Remove(handler); |
| 64 |
else |
| 65 |
isDone = true; |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
// |
| 70 |
// [OK] Use of unsafe Subscribe: non-pretentious wrapper of an observable in an event; exceptions can occur during +=. |
| 71 |
// |
| 72 |
var d = _source.Subscribe/*Unsafe*/( |
| 73 |
x => _invokeHandler(invoke, /*this,*/ x), |
| 74 |
ex => { remove(); ex.Throw(); }, |
| 75 |
() => remove() |
| 76 |
); |
| 77 |
|
| 78 |
lock (gate) |
| 79 |
{ |
| 80 |
if (!isDone) |
| 81 |
{ |
| 82 |
Add(handler, d); |
| 83 |
isAdded = true; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
private void Add(Delegate handler, IDisposable disposable) |
| 89 |
{ |
| 90 |
lock (_subscriptions) |
| 91 |
{ |
| 92 |
var l = new Stack<IDisposable>(); |
| 93 |
if (!_subscriptions.TryGetValue(handler, out l)) |
| 94 |
_subscriptions[handler] = l = new Stack<IDisposable>(); |
| 95 |
|
| 96 |
l.Push(disposable); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// <summary> |
| 101 |
/// Removes the specified event handler, causing a disposal of the corresponding subscription to the underlying source that was created during the Add operation. |
| 102 |
/// </summary> |
| 103 |
/// <param name="handler">Event handler to remove. This should be the same delegate as one that was passed to the Add operation.</param> |
| 104 |
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is null.</exception> |
| 105 |
protected void Remove(Delegate handler) |
| 106 |
{ |
| 107 |
if (handler == null) |
| 108 |
throw new ArgumentNullException("handler"); |
| 109 |
|
| 110 |
var d = default(IDisposable); |
| 111 |
|
| 112 |
lock (_subscriptions) |
| 113 |
{ |
| 114 |
var l = new Stack<IDisposable>(); |
| 115 |
if (_subscriptions.TryGetValue(handler, out l)) |
| 116 |
{ |
| 117 |
d = l.Pop(); |
| 118 |
if (l.Count == 0) |
| 119 |
_subscriptions.Remove(handler); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (d != null) |
| 124 |
d.Dispose(); |
| 125 |
} |
| 126 |
} |
| 127 |
} |