6db069298c071e39a1421c1f09d70395e85d411c3a5cd7b4b7e86bf38514e290a5748ddbabdd1207df080085f86dfbc6e79c2440c2597d0d557a744c350661 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Falsy, MonoTypeOperatorFunction, OperatorFunction } from '../types';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. export function skipWhile<T>(predicate: BooleanConstructor): OperatorFunction<T, Extract<T, Falsy> extends never ? never : T>;
  5. export function skipWhile<T>(predicate: (value: T, index: number) => true): OperatorFunction<T, never>;
  6. export function skipWhile<T>(predicate: (value: T, index: number) => boolean): MonoTypeOperatorFunction<T>;
  7. /**
  8. * Returns an Observable that skips all items emitted by the source Observable as long as a specified condition holds
  9. * true, but emits all further source items as soon as the condition becomes false.
  10. *
  11. * ![](skipWhile.png)
  12. *
  13. * Skips all the notifications with a truthy predicate. It will not skip the notifications when the predicate is falsy.
  14. * It can also be skipped using index. Once the predicate is true, it will not be called again.
  15. *
  16. * ## Example
  17. *
  18. * Skip some super heroes
  19. *
  20. * ```ts
  21. * import { from, skipWhile } from 'rxjs';
  22. *
  23. * const source = from(['Green Arrow', 'SuperMan', 'Flash', 'SuperGirl', 'Black Canary'])
  24. * // Skip the heroes until SuperGirl
  25. * const example = source.pipe(skipWhile(hero => hero !== 'SuperGirl'));
  26. * // output: SuperGirl, Black Canary
  27. * example.subscribe(femaleHero => console.log(femaleHero));
  28. * ```
  29. *
  30. * Skip values from the array until index 5
  31. *
  32. * ```ts
  33. * import { from, skipWhile } from 'rxjs';
  34. *
  35. * const source = from([1, 2, 3, 4, 5, 6, 7, 9, 10]);
  36. * const example = source.pipe(skipWhile((_, i) => i !== 5));
  37. * // output: 6, 7, 9, 10
  38. * example.subscribe(value => console.log(value));
  39. * ```
  40. *
  41. * @see {@link last}
  42. * @see {@link skip}
  43. * @see {@link skipUntil}
  44. * @see {@link skipLast}
  45. *
  46. * @param predicate A function to test each item emitted from the source Observable.
  47. * @return A function that returns an Observable that begins emitting items
  48. * emitted by the source Observable when the specified predicate becomes false.
  49. */
  50. export function skipWhile<T>(predicate: (value: T, index: number) => boolean): MonoTypeOperatorFunction<T> {
  51. return operate((source, subscriber) => {
  52. let taking = false;
  53. let index = 0;
  54. source.subscribe(
  55. createOperatorSubscriber(subscriber, (value) => (taking || (taking = !predicate(value, index++))) && subscriber.next(value))
  56. );
  57. });
  58. }