b4f14fedc58c255ce7bd97b93fa1b5504ea416ab56023ae73bbd944f4a3c4244392971d8cce5496b6cf07ec0cc4d2098402f2efd65d803b22367c5585c7694 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { OperatorFunction, MonoTypeOperatorFunction, TruthyTypesOf } from '../types';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. export function takeWhile<T>(predicate: BooleanConstructor, inclusive: true): MonoTypeOperatorFunction<T>;
  5. export function takeWhile<T>(predicate: BooleanConstructor, inclusive: false): OperatorFunction<T, TruthyTypesOf<T>>;
  6. export function takeWhile<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
  7. export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
  8. export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>;
  9. export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>;
  10. /**
  11. * Emits values emitted by the source Observable so long as each value satisfies
  12. * the given `predicate`, and then completes as soon as this `predicate` is not
  13. * satisfied.
  14. *
  15. * <span class="informal">Takes values from the source only while they pass the
  16. * condition given. When the first value does not satisfy, it completes.</span>
  17. *
  18. * ![](takeWhile.png)
  19. *
  20. * `takeWhile` subscribes and begins mirroring the source Observable. Each value
  21. * emitted on the source is given to the `predicate` function which returns a
  22. * boolean, representing a condition to be satisfied by the source values. The
  23. * output Observable emits the source values until such time as the `predicate`
  24. * returns false, at which point `takeWhile` stops mirroring the source
  25. * Observable and completes the output Observable.
  26. *
  27. * ## Example
  28. *
  29. * Emit click events only while the clientX property is greater than 200
  30. *
  31. * ```ts
  32. * import { fromEvent, takeWhile } from 'rxjs';
  33. *
  34. * const clicks = fromEvent<PointerEvent>(document, 'click');
  35. * const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
  36. * result.subscribe(x => console.log(x));
  37. * ```
  38. *
  39. * @see {@link take}
  40. * @see {@link takeLast}
  41. * @see {@link takeUntil}
  42. * @see {@link skip}
  43. *
  44. * @param predicate A function that evaluates a value emitted by the source
  45. * Observable and returns a boolean. Also takes the (zero-based) index as the
  46. * second argument.
  47. * @param inclusive When set to `true` the value that caused `predicate` to
  48. * return `false` will also be emitted.
  49. * @return A function that returns an Observable that emits values from the
  50. * source Observable so long as each value satisfies the condition defined by
  51. * the `predicate`, then completes.
  52. */
  53. export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive = false): MonoTypeOperatorFunction<T> {
  54. return operate((source, subscriber) => {
  55. let index = 0;
  56. source.subscribe(
  57. createOperatorSubscriber(subscriber, (value) => {
  58. const result = predicate(value, index++);
  59. (result || inclusive) && subscriber.next(value);
  60. !result && subscriber.complete();
  61. })
  62. );
  63. });
  64. }