// 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; using System; using System.Reactive.Disposables; namespace Microsoft.Reactive.Testing { class HotObservable : ITestableObservable { readonly TestScheduler scheduler; readonly List> observers = new List>(); readonly List subscriptions = new List(); readonly Recorded>[] messages; public HotObservable(TestScheduler scheduler, params Recorded>[] messages) { if (scheduler == null) throw new ArgumentNullException("scheduler"); if (messages == null) throw new ArgumentNullException("messages"); this.scheduler = scheduler; this.messages = messages; for (var i = 0; i < messages.Length; ++i) { var notification = messages[i].Value; scheduler.ScheduleAbsolute(default(object), messages[i].Time, (scheduler1, state1) => { var _observers = observers.ToArray(); for (var j = 0; j < _observers.Length; ++j) { notification.Accept(_observers[j]); } return Disposable.Empty; }); } } public virtual IDisposable Subscribe(IObserver observer) { if (observer == null) throw new ArgumentNullException("observer"); observers.Add(observer); subscriptions.Add(new Subscription(scheduler.Clock)); var index = subscriptions.Count - 1; return Disposable.Create(() => { observers.Remove(observer); subscriptions[index] = new Subscription(subscriptions[index].Subscribe, scheduler.Clock); }); } public IList Subscriptions { get { return subscriptions; } } public IList>> Messages { get { return messages; } } } }