Debian Codegraph @

external/rx/Rx/NET/Source/Microsoft.Reactive.Testing/HotObservable.cs

raw
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 using System.Reactive;
5 using System;
6 using System.Reactive.Disposables;
7
8 namespace Microsoft.Reactive.Testing
9 {
10 class HotObservable<T> : ITestableObservable<T>
11 {
12 readonly TestScheduler scheduler;
13 readonly List<IObserver<T>> observers = new List<IObserver<T>>();
14 readonly List<Subscription> subscriptions = new List<Subscription>();
15 readonly Recorded<Notification<T>>[] messages;
16
17 public HotObservable(TestScheduler scheduler, params Recorded<Notification<T>>[] messages)
18 {
19 if (scheduler == null)
20 throw new ArgumentNullException("scheduler");
21 if (messages == null)
22 throw new ArgumentNullException("messages");
23
24 this.scheduler = scheduler;
25 this.messages = messages;
26
27 for (var i = 0; i < messages.Length; ++i)
28 {
29 var notification = messages[i].Value;
30 scheduler.ScheduleAbsolute(default(object), messages[i].Time, (scheduler1, state1) =>
31 {
32 var _observers = observers.ToArray();
33 for (var j = 0; j < _observers.Length; ++j)
34 {
35 notification.Accept(_observers[j]);
36 }
37 return Disposable.Empty;
38 });
39 }
40 }
41
42 public virtual IDisposable Subscribe(IObserver<T> observer)
43 {
44 if (observer == null)
45 throw new ArgumentNullException("observer");
46
47 observers.Add(observer);
48 subscriptions.Add(new Subscription(scheduler.Clock));
49 var index = subscriptions.Count - 1;
50
51 return Disposable.Create(() =>
52 {
53 observers.Remove(observer);
54 subscriptions[index] = new Subscription(subscriptions[index].Subscribe, scheduler.Clock);
55 });
56 }
57
58 public IList<Subscription> Subscriptions
59 {
60 get { return subscriptions; }
61 }
62
63 public IList<Recorded<Notification<T>>> Messages
64 {
65 get { return messages; }
66 }
67 }
68 }