source: trip-planner-front/node_modules/rxjs/src/internal/operators/subscribeOn.ts@ 6a80231

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

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { Observable } from '../Observable';
4import { SubscribeOnObservable } from '../observable/SubscribeOnObservable';
5import { MonoTypeOperatorFunction, SchedulerLike, TeardownLogic } from '../types';
6
7/**
8 * Asynchronously subscribes Observers to this Observable on the specified {@link SchedulerLike}.
9 *
10 * With `subscribeOn` you can decide what type of scheduler a specific Observable will be using when it is subscribed to.
11 *
12 * Schedulers control the speed and order of emissions to observers from an Observable stream.
13 *
14 * ![](subscribeOn.png)
15 *
16 * ## Example
17 * Given the following code:
18 * ```javascript
19 * import { of, merge } from 'rxjs';
20 *
21 * const a = of(1, 2, 3, 4);
22 * const b = of(5, 6, 7, 8, 9);
23 * merge(a, b).subscribe(console.log);
24 * ```
25 *
26 * Both Observable `a` and `b` will emit their values directly and synchronously once they are subscribed to.
27 * This will result in the output of `1 2 3 4 5 6 7 8 9`.
28 *
29 * But if we instead us the `subscribeOn` operator declaring that we want to use the {@link asyncScheduler} for values emited by Observable `a`:
30 * ```javascript
31 * import { of, merge, asyncScheduler } from 'rxjs';
32 * import { subscribeOn } from 'rxjs/operators';
33 *
34 * const a = of(1, 2, 3, 4).pipe(subscribeOn(asyncScheduler));
35 * const b = of(5, 6, 7, 8, 9);
36 * merge(a, b).subscribe(console.log);
37 * ```
38 *
39 * The output will instead be `5 6 7 8 9 1 2 3 4`.
40 * The reason for this is that Observable `b` emits its values directly and synchronously like before
41 * but the emissions from `a` are scheduled on the event loop because we are now using the {@link asyncScheduler} for that specific Observable.
42 *
43 * @param {SchedulerLike} scheduler - The {@link SchedulerLike} to perform subscription actions on.
44 * @return {Observable<T>} The source Observable modified so that its subscriptions happen on the specified {@link SchedulerLike}.
45 .
46 * @method subscribeOn
47 * @owner Observable
48 */
49export function subscribeOn<T>(scheduler: SchedulerLike, delay: number = 0): MonoTypeOperatorFunction<T> {
50 return function subscribeOnOperatorFunction(source: Observable<T>): Observable<T> {
51 return source.lift(new SubscribeOnOperator<T>(scheduler, delay));
52 };
53}
54
55class SubscribeOnOperator<T> implements Operator<T, T> {
56 constructor(private scheduler: SchedulerLike,
57 private delay: number) {
58 }
59 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
60 return new SubscribeOnObservable<T>(
61 source, this.delay, this.scheduler
62 ).subscribe(subscriber);
63 }
64}
Note: See TracBrowser for help on using the repository browser.