takeWhile.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function takeWhile(predicate, inclusive) {
  5. if (inclusive === void 0) {
  6. inclusive = false;
  7. }
  8. return function (source) {
  9. return source.lift(new TakeWhileOperator(predicate, inclusive));
  10. };
  11. }
  12. var TakeWhileOperator = /*@__PURE__*/ (function () {
  13. function TakeWhileOperator(predicate, inclusive) {
  14. this.predicate = predicate;
  15. this.inclusive = inclusive;
  16. }
  17. TakeWhileOperator.prototype.call = function (subscriber, source) {
  18. return source.subscribe(new TakeWhileSubscriber(subscriber, this.predicate, this.inclusive));
  19. };
  20. return TakeWhileOperator;
  21. }());
  22. var TakeWhileSubscriber = /*@__PURE__*/ (function (_super) {
  23. tslib_1.__extends(TakeWhileSubscriber, _super);
  24. function TakeWhileSubscriber(destination, predicate, inclusive) {
  25. var _this = _super.call(this, destination) || this;
  26. _this.predicate = predicate;
  27. _this.inclusive = inclusive;
  28. _this.index = 0;
  29. return _this;
  30. }
  31. TakeWhileSubscriber.prototype._next = function (value) {
  32. var destination = this.destination;
  33. var result;
  34. try {
  35. result = this.predicate(value, this.index++);
  36. }
  37. catch (err) {
  38. destination.error(err);
  39. return;
  40. }
  41. this.nextOrComplete(value, result);
  42. };
  43. TakeWhileSubscriber.prototype.nextOrComplete = function (value, predicateResult) {
  44. var destination = this.destination;
  45. if (Boolean(predicateResult)) {
  46. destination.next(value);
  47. }
  48. else {
  49. if (this.inclusive) {
  50. destination.next(value);
  51. }
  52. destination.complete();
  53. }
  54. };
  55. return TakeWhileSubscriber;
  56. }(Subscriber));
  57. //# sourceMappingURL=takeWhile.js.map