a zip code crypto-currency system good for red ONLY

take.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /** PURE_IMPORTS_START .._Subscriber,.._util_ArgumentOutOfRangeError,.._observable_EmptyObservable PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscriber } from '../Subscriber';
  10. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  11. import { EmptyObservable } from '../observable/EmptyObservable';
  12. /**
  13. * Emits only the first `count` values emitted by the source Observable.
  14. *
  15. * <span class="informal">Takes the first `count` values from the source, then
  16. * completes.</span>
  17. *
  18. * <img src="./img/take.png" width="100%">
  19. *
  20. * `take` returns an Observable that emits only the first `count` values emitted
  21. * by the source Observable. If the source emits fewer than `count` values then
  22. * all of its values are emitted. After that, it completes, regardless if the
  23. * source completes.
  24. *
  25. * @example <caption>Take the first 5 seconds of an infinite 1-second interval Observable</caption>
  26. * var interval = Rx.Observable.interval(1000);
  27. * var five = interval.take(5);
  28. * five.subscribe(x => console.log(x));
  29. *
  30. * @see {@link takeLast}
  31. * @see {@link takeUntil}
  32. * @see {@link takeWhile}
  33. * @see {@link skip}
  34. *
  35. * @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
  36. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
  37. *
  38. * @param {number} count The maximum number of `next` values to emit.
  39. * @return {Observable<T>} An Observable that emits only the first `count`
  40. * values emitted by the source Observable, or all of the values from the source
  41. * if the source emits fewer than `count` values.
  42. * @method take
  43. * @owner Observable
  44. */
  45. export function take(count) {
  46. return function (source) {
  47. if (count === 0) {
  48. return new EmptyObservable();
  49. }
  50. else {
  51. return source.lift(new TakeOperator(count));
  52. }
  53. };
  54. }
  55. var TakeOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  56. function TakeOperator(total) {
  57. this.total = total;
  58. if (this.total < 0) {
  59. throw new ArgumentOutOfRangeError;
  60. }
  61. }
  62. TakeOperator.prototype.call = function (subscriber, source) {
  63. return source.subscribe(new TakeSubscriber(subscriber, this.total));
  64. };
  65. return TakeOperator;
  66. }());
  67. /**
  68. * We need this JSDoc comment for affecting ESDoc.
  69. * @ignore
  70. * @extends {Ignored}
  71. */
  72. var TakeSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  73. __extends(TakeSubscriber, _super);
  74. function TakeSubscriber(destination, total) {
  75. _super.call(this, destination);
  76. this.total = total;
  77. this.count = 0;
  78. }
  79. TakeSubscriber.prototype._next = function (value) {
  80. var total = this.total;
  81. var count = ++this.count;
  82. if (count <= total) {
  83. this.destination.next(value);
  84. if (count === total) {
  85. this.destination.complete();
  86. this.unsubscribe();
  87. }
  88. }
  89. };
  90. return TakeSubscriber;
  91. }(Subscriber));
  92. //# sourceMappingURL=take.js.map