UNPKG

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