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

observeOn.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Observable } from '../Observable';
  2. import { Operator } from '../Operator';
  3. import { Subscriber } from '../Subscriber';
  4. import { Subscription } from '../Subscription';
  5. import { Notification } from '../Notification';
  6. import { MonoTypeOperatorFunction, PartialObserver, SchedulerAction, SchedulerLike, TeardownLogic } from '../types';
  7. /**
  8. *
  9. * Re-emits all notifications from source Observable with specified scheduler.
  10. *
  11. * <span class="informal">Ensure a specific scheduler is used, from outside of an Observable.</span>
  12. *
  13. * `observeOn` is an operator that accepts a scheduler as a first parameter, which will be used to reschedule
  14. * notifications emitted by the source Observable. It might be useful, if you do not have control over
  15. * internal scheduler of a given Observable, but want to control when its values are emitted nevertheless.
  16. *
  17. * Returned Observable emits the same notifications (nexted values, complete and error events) as the source Observable,
  18. * but rescheduled with provided scheduler. Note that this doesn't mean that source Observables internal
  19. * scheduler will be replaced in any way. Original scheduler still will be used, but when the source Observable emits
  20. * notification, it will be immediately scheduled again - this time with scheduler passed to `observeOn`.
  21. * An anti-pattern would be calling `observeOn` on Observable that emits lots of values synchronously, to split
  22. * that emissions into asynchronous chunks. For this to happen, scheduler would have to be passed into the source
  23. * Observable directly (usually into the operator that creates it). `observeOn` simply delays notifications a
  24. * little bit more, to ensure that they are emitted at expected moments.
  25. *
  26. * As a matter of fact, `observeOn` accepts second parameter, which specifies in milliseconds with what delay notifications
  27. * will be emitted. The main difference between {@link delay} operator and `observeOn` is that `observeOn`
  28. * will delay all notifications - including error notifications - while `delay` will pass through error
  29. * from source Observable immediately when it is emitted. In general it is highly recommended to use `delay` operator
  30. * for any kind of delaying of values in the stream, while using `observeOn` to specify which scheduler should be used
  31. * for notification emissions in general.
  32. *
  33. * ## Example
  34. * Ensure values in subscribe are called just before browser repaint.
  35. * ```ts
  36. * import { interval } from 'rxjs';
  37. * import { observeOn } from 'rxjs/operators';
  38. *
  39. * const intervals = interval(10); // Intervals are scheduled
  40. * // with async scheduler by default...
  41. * intervals.pipe(
  42. * observeOn(animationFrameScheduler), // ...but we will observe on animationFrame
  43. * ) // scheduler to ensure smooth animation.
  44. * .subscribe(val => {
  45. * someDiv.style.height = val + 'px';
  46. * });
  47. * ```
  48. *
  49. * @see {@link delay}
  50. *
  51. * @param {SchedulerLike} scheduler Scheduler that will be used to reschedule notifications from source Observable.
  52. * @param {number} [delay] Number of milliseconds that states with what delay every notification should be rescheduled.
  53. * @return {Observable<T>} Observable that emits the same notifications as the source Observable,
  54. * but with provided scheduler.
  55. *
  56. * @method observeOn
  57. * @owner Observable
  58. */
  59. export function observeOn<T>(scheduler: SchedulerLike, delay: number = 0): MonoTypeOperatorFunction<T> {
  60. return function observeOnOperatorFunction(source: Observable<T>): Observable<T> {
  61. return source.lift(new ObserveOnOperator(scheduler, delay));
  62. };
  63. }
  64. export class ObserveOnOperator<T> implements Operator<T, T> {
  65. constructor(private scheduler: SchedulerLike, private delay: number = 0) {
  66. }
  67. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  68. return source.subscribe(new ObserveOnSubscriber(subscriber, this.scheduler, this.delay));
  69. }
  70. }
  71. /**
  72. * We need this JSDoc comment for affecting ESDoc.
  73. * @ignore
  74. * @extends {Ignored}
  75. */
  76. export class ObserveOnSubscriber<T> extends Subscriber<T> {
  77. /** @nocollapse */
  78. static dispatch(this: SchedulerAction<ObserveOnMessage>, arg: ObserveOnMessage) {
  79. const { notification, destination } = arg;
  80. notification.observe(destination);
  81. this.unsubscribe();
  82. }
  83. constructor(destination: Subscriber<T>,
  84. private scheduler: SchedulerLike,
  85. private delay: number = 0) {
  86. super(destination);
  87. }
  88. private scheduleMessage(notification: Notification<any>): void {
  89. const destination = this.destination as Subscription;
  90. destination.add(this.scheduler.schedule(
  91. ObserveOnSubscriber.dispatch,
  92. this.delay,
  93. new ObserveOnMessage(notification, this.destination)
  94. ));
  95. }
  96. protected _next(value: T): void {
  97. this.scheduleMessage(Notification.createNext(value));
  98. }
  99. protected _error(err: any): void {
  100. this.scheduleMessage(Notification.createError(err));
  101. this.unsubscribe();
  102. }
  103. protected _complete(): void {
  104. this.scheduleMessage(Notification.createComplete());
  105. this.unsubscribe();
  106. }
  107. }
  108. export class ObserveOnMessage {
  109. constructor(public notification: Notification<any>,
  110. public destination: PartialObserver<any>) {
  111. }
  112. }