observeOn.js 5.6 KB

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