| 1 |
import { Subject } from '../Subject'; |
| 2 |
import { Subscriber } from '../Subscriber'; |
| 3 |
import { Subscription } from '../Subscription'; |
| 4 |
import { Scheduler } from '../Scheduler'; |
| 5 |
import { TestMessage } from './TestMessage'; |
| 6 |
import { SubscriptionLog } from './SubscriptionLog'; |
| 7 |
import { SubscriptionLoggable } from './SubscriptionLoggable'; |
| 8 |
import { applyMixins } from '../util/applyMixins'; |
| 9 |
import { observeNotification } from '../Notification'; |
| 10 |
|
| 11 |
export class HotObservable<T> extends Subject<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(); |
| 21 |
this.scheduler = scheduler; |
| 22 |
} |
| 23 |
|
| 24 |
/** @internal */ |
| 25 |
protected _subscribe(subscriber: Subscriber<any>): Subscription { |
| 26 |
const subject: HotObservable<T> = this; |
| 27 |
const index = subject.logSubscribedFrame(); |
| 28 |
const subscription = new Subscription(); |
| 29 |
subscription.add( |
| 30 |
new Subscription(() => { |
| 31 |
subject.logUnsubscribedFrame(index); |
| 32 |
}) |
| 33 |
); |
| 34 |
subscription.add(super._subscribe(subscriber)); |
| 35 |
return subscription; |
| 36 |
} |
| 37 |
|
| 38 |
setup() { |
| 39 |
const subject = this; |
| 40 |
const messagesLength = subject.messages.length; |
| 41 |
/* tslint:disable:no-var-keyword */ |
| 42 |
for (let i = 0; i < messagesLength; i++) { |
| 43 |
(() => { |
| 44 |
const { notification, frame } = subject.messages[i]; |
| 45 |
/* tslint:enable */ |
| 46 |
subject.scheduler.schedule(() => { |
| 47 |
observeNotification(notification, subject); |
| 48 |
}, frame); |
| 49 |
})(); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
applyMixins(HotObservable, [SubscriptionLoggable]); |