| 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 ColdObservable<T> : ITestableObservable<T> |
| 11 |
{ |
| 12 |
readonly TestScheduler scheduler; |
| 13 |
readonly Recorded<Notification<T>>[] messages; |
| 14 |
readonly List<Subscription> subscriptions = new List<Subscription>(); |
| 15 |
|
| 16 |
public ColdObservable(TestScheduler scheduler, params Recorded<Notification<T>>[] messages) |
| 17 |
{ |
| 18 |
if (scheduler == null) |
| 19 |
throw new ArgumentNullException("scheduler"); |
| 20 |
if (messages == null) |
| 21 |
throw new ArgumentNullException("messages"); |
| 22 |
|
| 23 |
this.scheduler = scheduler; |
| 24 |
this.messages = messages; |
| 25 |
} |
| 26 |
|
| 27 |
public virtual IDisposable Subscribe(IObserver<T> observer) |
| 28 |
{ |
| 29 |
if (observer == null) |
| 30 |
throw new ArgumentNullException("observer"); |
| 31 |
|
| 32 |
subscriptions.Add(new Subscription(scheduler.Clock)); |
| 33 |
var index = subscriptions.Count - 1; |
| 34 |
|
| 35 |
var d = new CompositeDisposable(); |
| 36 |
|
| 37 |
for (var i = 0; i < messages.Length; ++i) |
| 38 |
{ |
| 39 |
var notification = messages[i].Value; |
| 40 |
d.Add(scheduler.ScheduleRelative(default(object), messages[i].Time, (scheduler1, state1) => { notification.Accept(observer); return Disposable.Empty; })); |
| 41 |
} |
| 42 |
|
| 43 |
return Disposable.Create(() => |
| 44 |
{ |
| 45 |
subscriptions[index] = new Subscription(subscriptions[index].Subscribe, scheduler.Clock); |
| 46 |
d.Dispose(); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
public IList<Subscription> Subscriptions |
| 51 |
{ |
| 52 |
get { return subscriptions; } |
| 53 |
} |
| 54 |
|
| 55 |
public IList<Recorded<Notification<T>>> Messages |
| 56 |
{ |
| 57 |
get { return messages; } |
| 58 |
} |
| 59 |
} |
| 60 |
} |