Debian Codegraph @

node/rxjs/src/internal/operators/observeOn.ts

raw
1 /** @prettier */
2 import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
3 import { executeSchedule } from '../util/executeSchedule';
4 import { operate } from '../util/lift';
5 import { createOperatorSubscriber } from './OperatorSubscriber';
6
7 /**
8 * Re-emits all notifications from source Observable with specified scheduler.
9 *
10 * <span class="informal">Ensure a specific scheduler is used, from outside of an Observable.</span>
11 *
12 * `observeOn` is an operator that accepts a scheduler as a first parameter, which will be used to reschedule
13 * notifications emitted by the source Observable. It might be useful, if you do not have control over
14 * internal scheduler of a given Observable, but want to control when its values are emitted nevertheless.
15 *
16 * Returned Observable emits the same notifications (nexted values, complete and error events) as the source Observable,
17 * but rescheduled with provided scheduler. Note that this doesn't mean that source Observables internal
18 * scheduler will be replaced in any way. Original scheduler still will be used, but when the source Observable emits
19 * notification, it will be immediately scheduled again - this time with scheduler passed to `observeOn`.
20 * An anti-pattern would be calling `observeOn` on Observable that emits lots of values synchronously, to split
21 * that emissions into asynchronous chunks. For this to happen, scheduler would have to be passed into the source
22 * Observable directly (usually into the operator that creates it). `observeOn` simply delays notifications a
23 * little bit more, to ensure that they are emitted at expected moments.
24 *
25 * As a matter of fact, `observeOn` accepts second parameter, which specifies in milliseconds with what delay notifications
26 * will be emitted. The main difference between {@link delay} operator and `observeOn` is that `observeOn`
27 * will delay all notifications - including error notifications - while `delay` will pass through error
28 * from source Observable immediately when it is emitted. In general it is highly recommended to use `delay` operator
29 * for any kind of delaying of values in the stream, while using `observeOn` to specify which scheduler should be used
30 * for notification emissions in general.
31 *
32 * ## Example
33 *
34 * Ensure values in subscribe are called just before browser repaint
35 *
36 * ```ts
37 * import { interval, observeOn, animationFrameScheduler } from 'rxjs';
38 *
39 * const someDiv = document.createElement('div');
40 * someDiv.style.cssText = 'width: 200px;background: #09c';
41 * document.body.appendChild(someDiv);
42 * const intervals = interval(10); // Intervals are scheduled
43 * // with async scheduler by default...
44 * intervals.pipe(
45 * observeOn(animationFrameScheduler) // ...but we will observe on animationFrame
46 * ) // scheduler to ensure smooth animation.
47 * .subscribe(val => {
48 * someDiv.style.height = val + 'px';
49 * });
50 * ```
51 *
52 * @see {@link delay}
53 *
54 * @param scheduler Scheduler that will be used to reschedule notifications from source Observable.
55 * @param delay Number of milliseconds that states with what delay every notification should be rescheduled.
56 * @return A function that returns an Observable that emits the same
57 * notifications as the source Observable, but with provided scheduler.
58 */
59 export function observeOn<T>(scheduler: SchedulerLike, delay = 0): MonoTypeOperatorFunction<T> {
60 return operate((source, subscriber) => {
61 source.subscribe(
62 createOperatorSubscriber(
63 subscriber,
64 (value) => executeSchedule(subscriber, scheduler, () => subscriber.next(value), delay),
65 () => executeSchedule(subscriber, scheduler, () => subscriber.complete(), delay),
66 (err) => executeSchedule(subscriber, scheduler, () => subscriber.error(err), delay)
67 )
68 );
69 });
70 }