Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

defaultIfEmpty.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { OperatorFunction, MonoTypeOperatorFunction } from '../types';
  5. /* tslint:disable:max-line-length */
  6. export function defaultIfEmpty<T>(defaultValue?: T): MonoTypeOperatorFunction<T>;
  7. export function defaultIfEmpty<T, R>(defaultValue?: R): OperatorFunction<T, T | R>;
  8. /* tslint:enable:max-line-length */
  9. /**
  10. * Emits a given value if the source Observable completes without emitting any
  11. * `next` value, otherwise mirrors the source Observable.
  12. *
  13. * <span class="informal">If the source Observable turns out to be empty, then
  14. * this operator will emit a default value.</span>
  15. *
  16. * ![](defaultIfEmpty.png)
  17. *
  18. * `defaultIfEmpty` emits the values emitted by the source Observable or a
  19. * specified default value if the source Observable is empty (completes without
  20. * having emitted any `next` value).
  21. *
  22. * ## Example
  23. * If no clicks happen in 5 seconds, then emit "no clicks"
  24. * ```ts
  25. * import { fromEvent } from 'rxjs';
  26. * import { defaultIfEmpty, takeUntil } from 'rxjs/operators';
  27. *
  28. * const clicks = fromEvent(document, 'click');
  29. * const clicksBeforeFive = clicks.pipe(takeUntil(interval(5000)));
  30. * const result = clicksBeforeFive.pipe(defaultIfEmpty('no clicks'));
  31. * result.subscribe(x => console.log(x));
  32. * ```
  33. *
  34. * @see {@link empty}
  35. * @see {@link last}
  36. *
  37. * @param {any} [defaultValue=null] The default value used if the source
  38. * Observable is empty.
  39. * @return {Observable} An Observable that emits either the specified
  40. * `defaultValue` if the source Observable emits no items, or the values emitted
  41. * by the source Observable.
  42. * @method defaultIfEmpty
  43. * @owner Observable
  44. */
  45. export function defaultIfEmpty<T, R>(defaultValue: R = null): OperatorFunction<T, T | R> {
  46. return (source: Observable<T>) => source.lift(new DefaultIfEmptyOperator(defaultValue)) as Observable<T | R>;
  47. }
  48. class DefaultIfEmptyOperator<T, R> implements Operator<T, T | R> {
  49. constructor(private defaultValue: R) {
  50. }
  51. call(subscriber: Subscriber<T | R>, source: any): any {
  52. return source.subscribe(new DefaultIfEmptySubscriber(subscriber, this.defaultValue));
  53. }
  54. }
  55. /**
  56. * We need this JSDoc comment for affecting ESDoc.
  57. * @ignore
  58. * @extends {Ignored}
  59. */
  60. class DefaultIfEmptySubscriber<T, R> extends Subscriber<T> {
  61. private isEmpty: boolean = true;
  62. constructor(destination: Subscriber<T | R>, private defaultValue: R) {
  63. super(destination);
  64. }
  65. protected _next(value: T): void {
  66. this.isEmpty = false;
  67. this.destination.next(value);
  68. }
  69. protected _complete(): void {
  70. if (this.isEmpty) {
  71. this.destination.next(this.defaultValue);
  72. }
  73. this.destination.complete();
  74. }
  75. }