Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

HotObservable.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /**
  10. * We need this JSDoc comment for affecting ESDoc.
  11. * @ignore
  12. * @extends {Ignored}
  13. */
  14. export class HotObservable<T> extends Subject<T> implements SubscriptionLoggable {
  15. public subscriptions: SubscriptionLog[] = [];
  16. scheduler: Scheduler;
  17. logSubscribedFrame: () => number;
  18. logUnsubscribedFrame: (index: number) => void;
  19. constructor(public messages: TestMessage[],
  20. scheduler: Scheduler) {
  21. super();
  22. this.scheduler = scheduler;
  23. }
  24. /** @deprecated This is an internal implementation detail, do not use. */
  25. _subscribe(subscriber: Subscriber<any>): Subscription {
  26. const subject: HotObservable<T> = this;
  27. const index = subject.logSubscribedFrame();
  28. const subscription = new Subscription();
  29. subscription.add(new Subscription(() => {
  30. subject.logUnsubscribedFrame(index);
  31. }));
  32. subscription.add(super._subscribe(subscriber));
  33. return subscription;
  34. }
  35. setup() {
  36. const subject = this;
  37. const messagesLength = subject.messages.length;
  38. /* tslint:disable:no-var-keyword */
  39. for (var i = 0; i < messagesLength; i++) {
  40. (() => {
  41. var message = subject.messages[i];
  42. /* tslint:enable */
  43. subject.scheduler.schedule(
  44. () => { message.notification.observe(subject); },
  45. message.frame
  46. );
  47. })();
  48. }
  49. }
  50. }
  51. applyMixins(HotObservable, [SubscriptionLoggable]);