| 1 |
import { concat } from '../observable/concat'; |
| 2 |
import { OperatorFunction, SchedulerLike, ValueFromArray } from '../types'; |
| 3 |
import { popScheduler } from '../util/args'; |
| 4 |
import { operate } from '../util/lift'; |
| 5 |
|
| 6 |
// Devs are more likely to pass null or undefined than they are a scheduler |
| 7 |
// without accompanying values. To make things easier for (naughty) devs who |
| 8 |
// use the `strictNullChecks: false` TypeScript compiler option, these |
| 9 |
// overloads with explicit null and undefined values are included. |
| 10 |
|
| 11 |
export function startWith<T>(value: null): OperatorFunction<T, T | null>; |
| 12 |
export function startWith<T>(value: undefined): OperatorFunction<T, T | undefined>; |
| 13 |
|
| 14 |
/** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `concatAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */ |
| 15 |
export function startWith<T, A extends readonly unknown[] = T[]>( |
| 16 |
...valuesAndScheduler: [...A, SchedulerLike] |
| 17 |
): OperatorFunction<T, T | ValueFromArray<A>>; |
| 18 |
export function startWith<T, A extends readonly unknown[] = T[]>(...values: A): OperatorFunction<T, T | ValueFromArray<A>>; |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns an observable that, at the moment of subscription, will synchronously emit all |
| 22 |
* values provided to this operator, then subscribe to the source and mirror all of its emissions |
| 23 |
* to subscribers. |
| 24 |
* |
| 25 |
* This is a useful way to know when subscription has occurred on an existing observable. |
| 26 |
* |
| 27 |
* <span class="informal">First emits its arguments in order, and then any |
| 28 |
* emissions from the source.</span> |
| 29 |
* |
| 30 |
*  |
| 31 |
* |
| 32 |
* ## Examples |
| 33 |
* |
| 34 |
* Emit a value when a timer starts. |
| 35 |
* |
| 36 |
* ```ts |
| 37 |
* import { timer, map, startWith } from 'rxjs'; |
| 38 |
* |
| 39 |
* timer(1000) |
| 40 |
* .pipe( |
| 41 |
* map(() => 'timer emit'), |
| 42 |
* startWith('timer start') |
| 43 |
* ) |
| 44 |
* .subscribe(x => console.log(x)); |
| 45 |
* |
| 46 |
* // results: |
| 47 |
* // 'timer start' |
| 48 |
* // 'timer emit' |
| 49 |
* ``` |
| 50 |
* |
| 51 |
* @param values Items you want the modified Observable to emit first. |
| 52 |
* @return A function that returns an Observable that synchronously emits |
| 53 |
* provided values before subscribing to the source Observable. |
| 54 |
* |
| 55 |
* @see {@link endWith} |
| 56 |
* @see {@link finalize} |
| 57 |
* @see {@link concat} |
| 58 |
*/ |
| 59 |
export function startWith<T, D>(...values: D[]): OperatorFunction<T, T | D> { |
| 60 |
const scheduler = popScheduler(values); |
| 61 |
return operate((source, subscriber) => { |
| 62 |
// Here we can't pass `undefined` as a scheduler, because if we did, the |
| 63 |
// code inside of `concat` would be confused by the `undefined`, and treat it |
| 64 |
// like an invalid observable. So we have to split it two different ways. |
| 65 |
(scheduler ? concat(values, source, scheduler) : concat(values, source)).subscribe(subscriber); |
| 66 |
}); |
| 67 |
} |