observeOn.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. *
  6. * Re-emits all notifications from source Observable with specified scheduler.
  7. *
  8. * <span class="informal">Ensure a specific scheduler is used, from outside of an Observable.</span>
  9. *
  10. * `observeOn` is an operator that accepts a scheduler as a first parameter, which will be used to reschedule
  11. * notifications emitted by the source Observable. It might be useful, if you do not have control over
  12. * internal scheduler of a given Observable, but want to control when its values are emitted nevertheless.
  13. *
  14. * Returned Observable emits the same notifications (nexted values, complete and error events) as the source Observable,
  15. * but rescheduled with provided scheduler. Note that this doesn't mean that source Observables internal
  16. * scheduler will be replaced in any way. Original scheduler still will be used, but when the source Observable emits
  17. * notification, it will be immediately scheduled again - this time with scheduler passed to `observeOn`.
  18. * An anti-pattern would be calling `observeOn` on Observable that emits lots of values synchronously, to split
  19. * that emissions into asynchronous chunks. For this to happen, scheduler would have to be passed into the source
  20. * Observable directly (usually into the operator that creates it). `observeOn` simply delays notifications a
  21. * little bit more, to ensure that they are emitted at expected moments.
  22. *
  23. * As a matter of fact, `observeOn` accepts second parameter, which specifies in milliseconds with what delay notifications
  24. * will be emitted. The main difference between {@link delay} operator and `observeOn` is that `observeOn`
  25. * will delay all notifications - including error notifications - while `delay` will pass through error
  26. * from source Observable immediately when it is emitted. In general it is highly recommended to use `delay` operator
  27. * for any kind of delaying of values in the stream, while using `observeOn` to specify which scheduler should be used
  28. * for notification emissions in general.
  29. *
  30. * @example <caption>Ensure values in subscribe are called just before browser repaint.</caption>
  31. * const intervals = Rx.Observable.interval(10); // Intervals are scheduled
  32. * // with async scheduler by default...
  33. *
  34. * intervals
  35. * .observeOn(Rx.Scheduler.animationFrame) // ...but we will observe on animationFrame
  36. * .subscribe(val => { // scheduler to ensure smooth animation.
  37. * someDiv.style.height = val + 'px';
  38. * });
  39. *
  40. * @see {@link delay}
  41. *
  42. * @param {SchedulerLike} scheduler Scheduler that will be used to reschedule notifications from source Observable.
  43. * @param {number} [delay] Number of milliseconds that states with what delay every notification should be rescheduled.
  44. * @return {Observable<T>} Observable that emits the same notifications as the source Observable,
  45. * but with provided scheduler.
  46. *
  47. * @method observeOn
  48. * @owner Observable
  49. */
  50. function observeOn(scheduler, delay) {
  51. if (delay === void 0) { delay = 0; }
  52. return operators_1.observeOn(scheduler, delay)(this);
  53. }
  54. exports.observeOn = observeOn;
  55. //# sourceMappingURL=observeOn.js.map