take.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /** PURE_IMPORTS_START tslib,_Subscriber,_util_ArgumentOutOfRangeError,_observable_empty PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  5. import { empty } from '../observable/empty';
  6. export function take(count) {
  7. return function (source) {
  8. if (count === 0) {
  9. return empty();
  10. }
  11. else {
  12. return source.lift(new TakeOperator(count));
  13. }
  14. };
  15. }
  16. var TakeOperator = /*@__PURE__*/ (function () {
  17. function TakeOperator(total) {
  18. this.total = total;
  19. if (this.total < 0) {
  20. throw new ArgumentOutOfRangeError;
  21. }
  22. }
  23. TakeOperator.prototype.call = function (subscriber, source) {
  24. return source.subscribe(new TakeSubscriber(subscriber, this.total));
  25. };
  26. return TakeOperator;
  27. }());
  28. var TakeSubscriber = /*@__PURE__*/ (function (_super) {
  29. tslib_1.__extends(TakeSubscriber, _super);
  30. function TakeSubscriber(destination, total) {
  31. var _this = _super.call(this, destination) || this;
  32. _this.total = total;
  33. _this.count = 0;
  34. return _this;
  35. }
  36. TakeSubscriber.prototype._next = function (value) {
  37. var total = this.total;
  38. var count = ++this.count;
  39. if (count <= total) {
  40. this.destination.next(value);
  41. if (count === total) {
  42. this.destination.complete();
  43. this.unsubscribe();
  44. }
  45. }
  46. };
  47. return TakeSubscriber;
  48. }(Subscriber));
  49. //# sourceMappingURL=take.js.map