repeat.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 EmptyObservable_1 = require('../observable/EmptyObservable');
  9. /**
  10. * Returns an Observable that repeats the stream of items emitted by the source Observable at most count times.
  11. *
  12. * <img src="./img/repeat.png" width="100%">
  13. *
  14. * @param {number} [count] The number of times the source Observable items are repeated, a count of 0 will yield
  15. * an empty Observable.
  16. * @return {Observable} An Observable that repeats the stream of items emitted by the source Observable at most
  17. * count times.
  18. * @method repeat
  19. * @owner Observable
  20. */
  21. function repeat(count) {
  22. if (count === void 0) { count = -1; }
  23. return function (source) {
  24. if (count === 0) {
  25. return new EmptyObservable_1.EmptyObservable();
  26. }
  27. else if (count < 0) {
  28. return source.lift(new RepeatOperator(-1, source));
  29. }
  30. else {
  31. return source.lift(new RepeatOperator(count - 1, source));
  32. }
  33. };
  34. }
  35. exports.repeat = repeat;
  36. var RepeatOperator = (function () {
  37. function RepeatOperator(count, source) {
  38. this.count = count;
  39. this.source = source;
  40. }
  41. RepeatOperator.prototype.call = function (subscriber, source) {
  42. return source.subscribe(new RepeatSubscriber(subscriber, this.count, this.source));
  43. };
  44. return RepeatOperator;
  45. }());
  46. /**
  47. * We need this JSDoc comment for affecting ESDoc.
  48. * @ignore
  49. * @extends {Ignored}
  50. */
  51. var RepeatSubscriber = (function (_super) {
  52. __extends(RepeatSubscriber, _super);
  53. function RepeatSubscriber(destination, count, source) {
  54. _super.call(this, destination);
  55. this.count = count;
  56. this.source = source;
  57. }
  58. RepeatSubscriber.prototype.complete = function () {
  59. if (!this.isStopped) {
  60. var _a = this, source = _a.source, count = _a.count;
  61. if (count === 0) {
  62. return _super.prototype.complete.call(this);
  63. }
  64. else if (count > -1) {
  65. this.count = count - 1;
  66. }
  67. source.subscribe(this._unsubscribeAndRecycle());
  68. }
  69. };
  70. return RepeatSubscriber;
  71. }(Subscriber_1.Subscriber));
  72. //# sourceMappingURL=repeat.js.map