observeOn.js 5.6KB

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