skipWhile.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { Subscriber } from '../Subscriber';
  2. /**
  3. * Returns an Observable that skips all items emitted by the source Observable as long as a specified condition holds
  4. * true, but emits all further source items as soon as the condition becomes false.
  5. *
  6. * <img src="./img/skipWhile.png" width="100%">
  7. *
  8. * @param {Function} predicate - A function to test each item emitted from the source Observable.
  9. * @return {Observable<T>} An Observable that begins emitting items emitted by the source Observable when the
  10. * specified predicate becomes false.
  11. * @method skipWhile
  12. * @owner Observable
  13. */
  14. export function skipWhile(predicate) {
  15. return (source) => source.lift(new SkipWhileOperator(predicate));
  16. }
  17. class SkipWhileOperator {
  18. constructor(predicate) {
  19. this.predicate = predicate;
  20. }
  21. call(subscriber, source) {
  22. return source.subscribe(new SkipWhileSubscriber(subscriber, this.predicate));
  23. }
  24. }
  25. /**
  26. * We need this JSDoc comment for affecting ESDoc.
  27. * @ignore
  28. * @extends {Ignored}
  29. */
  30. class SkipWhileSubscriber extends Subscriber {
  31. constructor(destination, predicate) {
  32. super(destination);
  33. this.predicate = predicate;
  34. this.skipping = true;
  35. this.index = 0;
  36. }
  37. _next(value) {
  38. const destination = this.destination;
  39. if (this.skipping) {
  40. this.tryCallPredicate(value);
  41. }
  42. if (!this.skipping) {
  43. destination.next(value);
  44. }
  45. }
  46. tryCallPredicate(value) {
  47. try {
  48. const result = this.predicate(value, this.index++);
  49. this.skipping = Boolean(result);
  50. }
  51. catch (err) {
  52. this.destination.error(err);
  53. }
  54. }
  55. }
  56. //# sourceMappingURL=skipWhile.js.map