Front end of the Slack clone application.

take.js 3.1KB

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