[6a3a178] | 1 | import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
|
---|
| 2 | /**
|
---|
| 3 | * Asynchronously subscribes Observers to this Observable on the specified {@link SchedulerLike}.
|
---|
| 4 | *
|
---|
| 5 | * With `subscribeOn` you can decide what type of scheduler a specific Observable will be using when it is subscribed to.
|
---|
| 6 | *
|
---|
| 7 | * Schedulers control the speed and order of emissions to observers from an Observable stream.
|
---|
| 8 | *
|
---|
| 9 | * ![](subscribeOn.png)
|
---|
| 10 | *
|
---|
| 11 | * ## Example
|
---|
| 12 | * Given the following code:
|
---|
| 13 | * ```javascript
|
---|
| 14 | * import { of, merge } from 'rxjs';
|
---|
| 15 | *
|
---|
| 16 | * const a = of(1, 2, 3, 4);
|
---|
| 17 | * const b = of(5, 6, 7, 8, 9);
|
---|
| 18 | * merge(a, b).subscribe(console.log);
|
---|
| 19 | * ```
|
---|
| 20 | *
|
---|
| 21 | * Both Observable `a` and `b` will emit their values directly and synchronously once they are subscribed to.
|
---|
| 22 | * This will result in the output of `1 2 3 4 5 6 7 8 9`.
|
---|
| 23 | *
|
---|
| 24 | * But if we instead us the `subscribeOn` operator declaring that we want to use the {@link asyncScheduler} for values emited by Observable `a`:
|
---|
| 25 | * ```javascript
|
---|
| 26 | * import { of, merge, asyncScheduler } from 'rxjs';
|
---|
| 27 | * import { subscribeOn } from 'rxjs/operators';
|
---|
| 28 | *
|
---|
| 29 | * const a = of(1, 2, 3, 4).pipe(subscribeOn(asyncScheduler));
|
---|
| 30 | * const b = of(5, 6, 7, 8, 9);
|
---|
| 31 | * merge(a, b).subscribe(console.log);
|
---|
| 32 | * ```
|
---|
| 33 | *
|
---|
| 34 | * The output will instead be `5 6 7 8 9 1 2 3 4`.
|
---|
| 35 | * The reason for this is that Observable `b` emits its values directly and synchronously like before
|
---|
| 36 | * but the emissions from `a` are scheduled on the event loop because we are now using the {@link asyncScheduler} for that specific Observable.
|
---|
| 37 | *
|
---|
| 38 | * @param {SchedulerLike} scheduler - The {@link SchedulerLike} to perform subscription actions on.
|
---|
| 39 | * @return {Observable<T>} The source Observable modified so that its subscriptions happen on the specified {@link SchedulerLike}.
|
---|
| 40 | .
|
---|
| 41 | * @method subscribeOn
|
---|
| 42 | * @owner Observable
|
---|
| 43 | */
|
---|
| 44 | export declare function subscribeOn<T>(scheduler: SchedulerLike, delay?: number): MonoTypeOperatorFunction<T>;
|
---|