observeOn.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { IScheduler } from '../Scheduler';
  2. import { Operator } from '../Operator';
  3. import { PartialObserver } from '../Observer';
  4. import { Subscriber } from '../Subscriber';
  5. import { Notification } from '../Notification';
  6. import { TeardownLogic } from '../Subscription';
  7. import { Action } from '../scheduler/Action';
  8. import { MonoTypeOperatorFunction } from '../interfaces';
  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. export declare function observeOn<T>(scheduler: IScheduler, delay?: number): MonoTypeOperatorFunction<T>;
  56. export declare class ObserveOnOperator<T> implements Operator<T, T> {
  57. private scheduler;
  58. private delay;
  59. constructor(scheduler: IScheduler, delay?: number);
  60. call(subscriber: Subscriber<T>, source: any): TeardownLogic;
  61. }
  62. /**
  63. * We need this JSDoc comment for affecting ESDoc.
  64. * @ignore
  65. * @extends {Ignored}
  66. */
  67. export declare class ObserveOnSubscriber<T> extends Subscriber<T> {
  68. private scheduler;
  69. private delay;
  70. static dispatch(this: Action<ObserveOnMessage>, arg: ObserveOnMessage): void;
  71. constructor(destination: Subscriber<T>, scheduler: IScheduler, delay?: number);
  72. private scheduleMessage(notification);
  73. protected _next(value: T): void;
  74. protected _error(err: any): void;
  75. protected _complete(): void;
  76. }
  77. export declare class ObserveOnMessage {
  78. notification: Notification<any>;
  79. destination: PartialObserver<any>;
  80. constructor(notification: Notification<any>, destination: PartialObserver<any>);
  81. }