blob: 8c88968ab67148d0a3dfa24f7642a3405f7de217 (
plain)
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
|
// 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.Reactive.Disposables;
using System.Reactive.Linq;
namespace System.Reactive.Joins
{
internal interface IJoinObserver : IDisposable
{
void Subscribe(object gate);
void Dequeue();
}
internal sealed class JoinObserver<T> : ObserverBase<Notification<T>>, IJoinObserver
{
private object gate;
private readonly IObservable<T> source;
private readonly Action<Exception> onError;
private List<ActivePlan> activePlans;
public Queue<Notification<T>> Queue { get; private set; }
private readonly SingleAssignmentDisposable subscription;
private bool isDisposed;
public JoinObserver(IObservable<T> source, Action<Exception> onError)
{
this.source = source;
this.onError = onError;
Queue = new Queue<Notification<T>>();
subscription = new SingleAssignmentDisposable();
activePlans = new List<ActivePlan>();
}
public void AddActivePlan(ActivePlan activePlan)
{
activePlans.Add(activePlan);
}
public void Subscribe(object gate)
{
this.gate = gate;
subscription.Disposable = source.Materialize().SubscribeSafe(this);
}
public void Dequeue()
{
Queue.Dequeue();
}
protected override void OnNextCore(Notification<T> notification)
{
lock (gate)
{
if (!isDisposed)
{
if (notification.Kind == NotificationKind.OnError)
{
onError(notification.Exception);
return;
}
Queue.Enqueue(notification);
foreach (var activePlan in activePlans.ToArray())
activePlan.Match();
}
}
}
protected override void OnErrorCore(Exception exception)
{
}
protected override void OnCompletedCore()
{
}
internal void RemoveActivePlan(ActivePlan activePlan)
{
activePlans.Remove(activePlan);
if (activePlans.Count == 0)
Dispose();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!isDisposed)
{
if (disposing)
subscription.Dispose();
isDisposed = true;
}
}
}
}
|