repeat.js 2.5KB

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