-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathDemultiplexor.cs
164 lines (145 loc) · 5.61 KB
/
Demultiplexor.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using System.Reflection;
namespace System.Reactive
{
/// <summary>
/// Efficiently demultiplexes input sequence of objects into output sequences of fixed types
/// The callbacks on the output sequences are called in the order of occurence of input events
/// OnNext of the Demultiplexor should not be called from multiple threads
/// </summary>
public class Demultiplexor : IObserver<object>
{
private readonly Dictionary<Type, IObserver<object>> _outputs = new Dictionary<Type, IObserver<object>>();
private readonly Dictionary<Type, List<Type>> _knownOutputMappings = new Dictionary<Type, List<Type>>();
/// <summary>
/// Notifies the observer that the provider has finished sending push-based notifications.
/// </summary>
public void OnCompleted()
{
foreach (var output in _outputs.Values.ToArray())
{
output.OnCompleted();
}
}
/// <summary>
/// Notifies the observer that the provider has experienced an error condition.
/// </summary>
/// <param name="error">An object that provides additional information about the error.</param>
public void OnError(Exception error)
{
foreach (var output in _outputs.Values)
{
output.OnError(error);
}
}
/// <summary>
/// Provides the observer with new data.
/// </summary>
/// <param name="inputObject">The current notification information.</param>
public void OnNext(object inputObject)
{
var inputObjectType = inputObject.GetType();
List<Type> outputKeys;
_knownOutputMappings.TryGetValue(inputObjectType, out outputKeys);
if (outputKeys == null)
{
outputKeys = new List<Type>();
_knownOutputMappings.Add(inputObjectType, outputKeys);
outputKeys.AddRange(GetTypes(inputObjectType).Where(type => _outputs.ContainsKey(type)));
}
foreach (var keyType in outputKeys)
{
_outputs[keyType].OnNext(inputObject);
}
}
/// <summary>
/// Returns an output sequence of given type
/// </summary>
/// <typeparam name="TOutput">The desired type</typeparam>
/// <returns>Sequence in which all events are of type TOutput</returns>
public IObservable<TOutput> GetObservable<TOutput>()
{
IObserver<object> o;
if (!_outputs.TryGetValue(typeof(TOutput), out o))
{
o = new OutputSubject<TOutput>();
_outputs.Add(typeof(TOutput), o);
RefreshKnownOutputMappings(typeof(TOutput));
}
var output = (IObservable<TOutput>)o;
return output;
}
private static List<Type> GetTypes(Type inputType)
{
var typeList = new List<Type>();
var temp = inputType;
while (temp != typeof(object))
{
typeList.Add(temp);
temp = temp.GetTypeInfo().BaseType;
}
typeList.AddRange(inputType.GetTypeInfo().ImplementedInterfaces);
return typeList;
}
private void RefreshKnownOutputMappings(Type outputType)
{
foreach (var knownMappings in _knownOutputMappings)
{
if (GetTypes(knownMappings.Key).Contains(outputType) && !knownMappings.Value.Contains(outputType))
{
knownMappings.Value.Add(outputType);
}
}
}
private sealed class OutputSubject<T> : ISubject<object, T>, IDisposable
{
private readonly Subject<T> _subject;
private int _refcount;
public OutputSubject()
{
_subject = new Subject<T>();
}
public void Dispose()
{
_refcount--;
//if (_refcount == 0)
//{
// _parent._outputs.Remove(typeof(T));
//}
}
/// <summary>
/// Notifies the observer that the provider has finished sending push-based notifications.
/// </summary>
public void OnCompleted()
{
_subject.OnCompleted();
}
/// <summary>
/// Notifies the observer that the provider has experienced an error condition.
/// </summary>
/// <param name="error">An object that provides additional information about the error.</param>
public void OnError(Exception error)
{
_subject.OnError(error);
}
/// <summary>
/// Provides the observer with new data.
/// </summary>
/// <param name="value">The current notification information.</param>
public void OnNext(object value)
{
_subject.OnNext((T)value);
}
public IDisposable Subscribe(IObserver<T> observer)
{
IDisposable subscription = _subject.Subscribe(observer);
_refcount++;
return new CompositeDisposable(subscription, this);
}
}
}
}