| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Subscription } from '../Subscription'; |
| 3 |
import { Scheduler } from '../Scheduler'; |
| 4 |
import { TestMessage } from './TestMessage'; |
| 5 |
import { SubscriptionLog } from './SubscriptionLog'; |
| 6 |
import { SubscriptionLoggable } from './SubscriptionLoggable'; |
| 7 |
import { applyMixins } from '../util/applyMixins'; |
| 8 |
import { Subscriber } from '../Subscriber'; |
| 9 |
import { observeNotification } from '../Notification'; |
| 10 |
|
| 11 |
export class ColdObservable<T> extends Observable<T> implements SubscriptionLoggable { |
| 12 |
public subscriptions: SubscriptionLog[] = []; |
| 13 |
scheduler: Scheduler; |
| 14 |
// @ts-ignore: Property has no initializer and is not definitely assigned |
| 15 |
logSubscribedFrame: () => number; |
| 16 |
// @ts-ignore: Property has no initializer and is not definitely assigned |
| 17 |
logUnsubscribedFrame: (index: number) => void; |
| 18 |
|
| 19 |
constructor(public messages: TestMessage[], scheduler: Scheduler) { |
| 20 |
super(function (this: Observable<T>, subscriber: Subscriber<any>) { |
| 21 |
const observable: ColdObservable<T> = this as any; |
| 22 |
const index = observable.logSubscribedFrame(); |
| 23 |
const subscription = new Subscription(); |
| 24 |
subscription.add( |
| 25 |
new Subscription(() => { |
| 26 |
observable.logUnsubscribedFrame(index); |
| 27 |
}) |
| 28 |
); |
| 29 |
observable.scheduleMessages(subscriber); |
| 30 |
return subscription; |
| 31 |
}); |
| 32 |
this.scheduler = scheduler; |
| 33 |
} |
| 34 |
|
| 35 |
scheduleMessages(subscriber: Subscriber<any>) { |
| 36 |
const messagesLength = this.messages.length; |
| 37 |
for (let i = 0; i < messagesLength; i++) { |
| 38 |
const message = this.messages[i]; |
| 39 |
subscriber.add( |
| 40 |
this.scheduler.schedule( |
| 41 |
(state) => { |
| 42 |
const { message: { notification }, subscriber: destination } = state!; |
| 43 |
observeNotification(notification, destination); |
| 44 |
}, |
| 45 |
message.frame, |
| 46 |
{ message, subscriber } |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
applyMixins(ColdObservable, [SubscriptionLoggable]); |