18d8f0cd7912d76489fc632544fcda178233c0a22dce10826df5b48ef1b61f6a8bee671e9437c7a2718e772ca93118bd0209e95a8686e70ff65109e407171d 2.6 KB

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