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';
10 * Re-emits all notifications from source Observable with specified scheduler.
12 * <span class="informal">Ensure a specific scheduler is used, from outside of an Observable.</span>
14 * `observeOn` is an operator that accepts a scheduler as a first parameter, which will be used to reschedule
15 * notifications emitted by the source Observable. It might be useful, if you do not have control over
16 * internal scheduler of a given Observable, but want to control when its values are emitted nevertheless.
18 * Returned Observable emits the same notifications (nexted values, complete and error events) as the source Observable,
19 * but rescheduled with provided scheduler. Note that this doesn't mean that source Observables internal
20 * scheduler will be replaced in any way. Original scheduler still will be used, but when the source Observable emits
21 * notification, it will be immediately scheduled again - this time with scheduler passed to `observeOn`.
22 * An anti-pattern would be calling `observeOn` on Observable that emits lots of values synchronously, to split
23 * that emissions into asynchronous chunks. For this to happen, scheduler would have to be passed into the source
24 * Observable directly (usually into the operator that creates it). `observeOn` simply delays notifications a
25 * little bit more, to ensure that they are emitted at expected moments.
27 * As a matter of fact, `observeOn` accepts second parameter, which specifies in milliseconds with what delay notifications
28 * will be emitted. The main difference between {@link delay} operator and `observeOn` is that `observeOn`
29 * will delay all notifications - including error notifications - while `delay` will pass through error
30 * from source Observable immediately when it is emitted. In general it is highly recommended to use `delay` operator
31 * for any kind of delaying of values in the stream, while using `observeOn` to specify which scheduler should be used
32 * for notification emissions in general.
35 * Ensure values in subscribe are called just before browser repaint.
37 * import { interval } from 'rxjs';
38 * import { observeOn } from 'rxjs/operators';
40 * const intervals = interval(10); // Intervals are scheduled
41 * // with async scheduler by default...
43 * observeOn(animationFrameScheduler), // ...but we will observe on animationFrame
44 * ) // scheduler to ensure smooth animation.
46 * someDiv.style.height = val + 'px';
52 * @param {SchedulerLike} scheduler Scheduler that will be used to reschedule notifications from source Observable.
53 * @param {number} [delay] Number of milliseconds that states with what delay every notification should be rescheduled.
54 * @return {Observable<T>} Observable that emits the same notifications as the source Observable,
55 * but with provided scheduler.
60 export function observeOn<T>(scheduler: SchedulerLike, delay: number = 0): MonoTypeOperatorFunction<T> {
61 return function observeOnOperatorFunction(source: Observable<T>): Observable<T> {
62 return source.lift(new ObserveOnOperator(scheduler, delay));
66 export class ObserveOnOperator<T> implements Operator<T, T> {
67 constructor(private scheduler: SchedulerLike, private delay: number = 0) {
70 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
71 return source.subscribe(new ObserveOnSubscriber(subscriber, this.scheduler, this.delay));
76 * We need this JSDoc comment for affecting ESDoc.
80 export class ObserveOnSubscriber<T> extends Subscriber<T> {
82 static dispatch(this: SchedulerAction<ObserveOnMessage>, arg: ObserveOnMessage) {
83 const { notification, destination } = arg;
84 notification.observe(destination);
88 constructor(destination: Subscriber<T>,
89 private scheduler: SchedulerLike,
90 private delay: number = 0) {
94 private scheduleMessage(notification: Notification<any>): void {
95 const destination = this.destination as Subscription;
96 destination.add(this.scheduler.schedule(
97 ObserveOnSubscriber.dispatch,
99 new ObserveOnMessage(notification, this.destination)
103 protected _next(value: T): void {
104 this.scheduleMessage(Notification.createNext(value));
107 protected _error(err: any): void {
108 this.scheduleMessage(Notification.createError(err));
112 protected _complete(): void {
113 this.scheduleMessage(Notification.createComplete());
118 export class ObserveOnMessage {
119 constructor(public notification: Notification<any>,
120 public destination: PartialObserver<any>) {