Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

takeWhile.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
  5. export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
  6. export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>;
  7. export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>;
  8. /**
  9. * Emits values emitted by the source Observable so long as each value satisfies
  10. * the given `predicate`, and then completes as soon as this `predicate` is not
  11. * satisfied.
  12. *
  13. * <span class="informal">Takes values from the source only while they pass the
  14. * condition given. When the first value does not satisfy, it completes.</span>
  15. *
  16. * ![](takeWhile.png)
  17. *
  18. * `takeWhile` subscribes and begins mirroring the source Observable. Each value
  19. * emitted on the source is given to the `predicate` function which returns a
  20. * boolean, representing a condition to be satisfied by the source values. The
  21. * output Observable emits the source values until such time as the `predicate`
  22. * returns false, at which point `takeWhile` stops mirroring the source
  23. * Observable and completes the output Observable.
  24. *
  25. * ## Example
  26. * Emit click events only while the clientX property is greater than 200
  27. * ```ts
  28. * import { fromEvent } from 'rxjs';
  29. * import { takeWhile } from 'rxjs/operators';
  30. *
  31. * const clicks = fromEvent(document, 'click');
  32. * const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
  33. * result.subscribe(x => console.log(x));
  34. * ```
  35. *
  36. * @see {@link take}
  37. * @see {@link takeLast}
  38. * @see {@link takeUntil}
  39. * @see {@link skip}
  40. *
  41. * @param {function(value: T, index: number): boolean} predicate A function that
  42. * evaluates a value emitted by the source Observable and returns a boolean.
  43. * Also takes the (zero-based) index as the second argument.
  44. * @param {boolean} inclusive When set to `true` the value that caused
  45. * `predicate` to return `false` will also be emitted.
  46. * @return {Observable<T>} An Observable that emits the values from the source
  47. * Observable so long as each value satisfies the condition defined by the
  48. * `predicate`, then completes.
  49. * @method takeWhile
  50. * @owner Observable
  51. */
  52. export function takeWhile<T>(
  53. predicate: (value: T, index: number) => boolean,
  54. inclusive = false): MonoTypeOperatorFunction<T> {
  55. return (source: Observable<T>) =>
  56. source.lift(new TakeWhileOperator(predicate, inclusive));
  57. }
  58. class TakeWhileOperator<T> implements Operator<T, T> {
  59. constructor(
  60. private predicate: (value: T, index: number) => boolean,
  61. private inclusive: boolean) {}
  62. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  63. return source.subscribe(
  64. new TakeWhileSubscriber(subscriber, this.predicate, this.inclusive));
  65. }
  66. }
  67. /**
  68. * We need this JSDoc comment for affecting ESDoc.
  69. * @ignore
  70. * @extends {Ignored}
  71. */
  72. class TakeWhileSubscriber<T> extends Subscriber<T> {
  73. private index: number = 0;
  74. constructor(
  75. destination: Subscriber<T>,
  76. private predicate: (value: T, index: number) => boolean,
  77. private inclusive: boolean) {
  78. super(destination);
  79. }
  80. protected _next(value: T): void {
  81. const destination = this.destination;
  82. let result: boolean;
  83. try {
  84. result = this.predicate(value, this.index++);
  85. } catch (err) {
  86. destination.error(err);
  87. return;
  88. }
  89. this.nextOrComplete(value, result);
  90. }
  91. private nextOrComplete(value: T, predicateResult: boolean): void {
  92. const destination = this.destination;
  93. if (Boolean(predicateResult)) {
  94. destination.next(value);
  95. } else {
  96. if (this.inclusive) {
  97. destination.next(value);
  98. }
  99. destination.complete();
  100. }
  101. }
  102. }