187ef4ed40ed131e68e6b69c84397ab7ffd4b8e3c3b914b0245ca80f1359f0056aa43d917867e3320486be5a85a18893ae676aad331adb4a0c81a013c03055 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /** prettier */
  2. import { Observable } from '../Observable';
  3. import { concat } from '../observable/concat';
  4. import { of } from '../observable/of';
  5. import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ValueFromArray } from '../types';
  6. /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `concatAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */
  7. export function endWith<T>(scheduler: SchedulerLike): MonoTypeOperatorFunction<T>;
  8. /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `concatAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */
  9. export function endWith<T, A extends unknown[] = T[]>(
  10. ...valuesAndScheduler: [...A, SchedulerLike]
  11. ): OperatorFunction<T, T | ValueFromArray<A>>;
  12. export function endWith<T, A extends unknown[] = T[]>(...values: A): OperatorFunction<T, T | ValueFromArray<A>>;
  13. /**
  14. * Returns an observable that will emit all values from the source, then synchronously emit
  15. * the provided value(s) immediately after the source completes.
  16. *
  17. * NOTE: Passing a last argument of a Scheduler is _deprecated_, and may result in incorrect
  18. * types in TypeScript.
  19. *
  20. * This is useful for knowing when an observable ends. Particularly when paired with an
  21. * operator like {@link takeUntil}
  22. *
  23. * ![](endWith.png)
  24. *
  25. * ## Example
  26. *
  27. * Emit values to know when an interval starts and stops. The interval will
  28. * stop when a user clicks anywhere on the document.
  29. *
  30. * ```ts
  31. * import { interval, map, fromEvent, startWith, takeUntil, endWith } from 'rxjs';
  32. *
  33. * const ticker$ = interval(5000).pipe(
  34. * map(() => 'tick')
  35. * );
  36. *
  37. * const documentClicks$ = fromEvent(document, 'click');
  38. *
  39. * ticker$.pipe(
  40. * startWith('interval started'),
  41. * takeUntil(documentClicks$),
  42. * endWith('interval ended by click')
  43. * )
  44. * .subscribe(x => console.log(x));
  45. *
  46. * // Result (assuming a user clicks after 15 seconds)
  47. * // 'interval started'
  48. * // 'tick'
  49. * // 'tick'
  50. * // 'tick'
  51. * // 'interval ended by click'
  52. * ```
  53. *
  54. * @see {@link startWith}
  55. * @see {@link concat}
  56. * @see {@link takeUntil}
  57. *
  58. * @param values Items you want the modified Observable to emit last.
  59. * @return A function that returns an Observable that emits all values from the
  60. * source, then synchronously emits the provided value(s) immediately after the
  61. * source completes.
  62. */
  63. export function endWith<T>(...values: Array<T | SchedulerLike>): MonoTypeOperatorFunction<T> {
  64. return (source: Observable<T>) => concat(source, of(...values)) as Observable<T>;
  65. }