source: trip-planner-front/node_modules/rxjs/src/internal/operators/observeOn.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1import { Observable } from '../Observable';
2import { Operator } from '../Operator';
3import { Subscriber } from '../Subscriber';
4import { Subscription } from '../Subscription';
5import { Notification } from '../Notification';
6import { MonoTypeOperatorFunction, PartialObserver, SchedulerAction, SchedulerLike, TeardownLogic } from '../types';
7
8/**
9 *
10 * Re-emits all notifications from source Observable with specified scheduler.
11 *
12 * <span class="informal">Ensure a specific scheduler is used, from outside of an Observable.</span>
13 *
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.
17 *
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.
26 *
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.
33 *
34 * ## Example
35 * Ensure values in subscribe are called just before browser repaint.
36 * ```ts
37 * import { interval } from 'rxjs';
38 * import { observeOn } from 'rxjs/operators';
39 *
40 * const intervals = interval(10); // Intervals are scheduled
41 * // with async scheduler by default...
42 * intervals.pipe(
43 * observeOn(animationFrameScheduler), // ...but we will observe on animationFrame
44 * ) // scheduler to ensure smooth animation.
45 * .subscribe(val => {
46 * someDiv.style.height = val + 'px';
47 * });
48 * ```
49 *
50 * @see {@link delay}
51 *
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.
56 *
57 * @method observeOn
58 * @owner Observable
59 */
60export 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));
63 };
64}
65
66export class ObserveOnOperator<T> implements Operator<T, T> {
67 constructor(private scheduler: SchedulerLike, private delay: number = 0) {
68 }
69
70 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
71 return source.subscribe(new ObserveOnSubscriber(subscriber, this.scheduler, this.delay));
72 }
73}
74
75/**
76 * We need this JSDoc comment for affecting ESDoc.
77 * @ignore
78 * @extends {Ignored}
79 */
80export class ObserveOnSubscriber<T> extends Subscriber<T> {
81 /** @nocollapse */
82 static dispatch(this: SchedulerAction<ObserveOnMessage>, arg: ObserveOnMessage) {
83 const { notification, destination } = arg;
84 notification.observe(destination);
85 this.unsubscribe();
86 }
87
88 constructor(destination: Subscriber<T>,
89 private scheduler: SchedulerLike,
90 private delay: number = 0) {
91 super(destination);
92 }
93
94 private scheduleMessage(notification: Notification<any>): void {
95 const destination = this.destination as Subscription;
96 destination.add(this.scheduler.schedule(
97 ObserveOnSubscriber.dispatch,
98 this.delay,
99 new ObserveOnMessage(notification, this.destination)
100 ));
101 }
102
103 protected _next(value: T): void {
104 this.scheduleMessage(Notification.createNext(value));
105 }
106
107 protected _error(err: any): void {
108 this.scheduleMessage(Notification.createError(err));
109 this.unsubscribe();
110 }
111
112 protected _complete(): void {
113 this.scheduleMessage(Notification.createComplete());
114 this.unsubscribe();
115 }
116}
117
118export class ObserveOnMessage {
119 constructor(public notification: Notification<any>,
120 public destination: PartialObserver<any>) {
121 }
122}
Note: See TracBrowser for help on using the repository browser.