3b4689142fa8e873105acdc78265492c7157d31ab2c78a3ef04ae724dbdfc9bcba1cffa8b518893ce04d8eb1aa0a302d1d1e238eee003f644dc0dd61b5d8da 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. export class HotObservable<T> extends Subject<T> implements SubscriptionLoggable {
  11. public subscriptions: SubscriptionLog[] = [];
  12. scheduler: Scheduler;
  13. // @ts-ignore: Property has no initializer and is not definitely assigned
  14. logSubscribedFrame: () => number;
  15. // @ts-ignore: Property has no initializer and is not definitely assigned
  16. logUnsubscribedFrame: (index: number) => void;
  17. constructor(public messages: TestMessage[], scheduler: Scheduler) {
  18. super();
  19. this.scheduler = scheduler;
  20. }
  21. /** @internal */
  22. protected _subscribe(subscriber: Subscriber<any>): Subscription {
  23. const subject: HotObservable<T> = this;
  24. const index = subject.logSubscribedFrame();
  25. const subscription = new Subscription();
  26. subscription.add(
  27. new Subscription(() => {
  28. subject.logUnsubscribedFrame(index);
  29. })
  30. );
  31. subscription.add(super._subscribe(subscriber));
  32. return subscription;
  33. }
  34. setup() {
  35. const subject = this;
  36. const messagesLength = subject.messages.length;
  37. /* tslint:disable:no-var-keyword */
  38. for (let i = 0; i < messagesLength; i++) {
  39. (() => {
  40. const { notification, frame } = subject.messages[i];
  41. /* tslint:enable */
  42. subject.scheduler.schedule(() => {
  43. observeNotification(notification, subject);
  44. }, frame);
  45. })();
  46. }
  47. }
  48. }
  49. applyMixins(HotObservable, [SubscriptionLoggable]);