repeat.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.repeat = void 0;
  4. var empty_1 = require("../observable/empty");
  5. var lift_1 = require("../util/lift");
  6. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  7. var innerFrom_1 = require("../observable/innerFrom");
  8. var timer_1 = require("../observable/timer");
  9. function repeat(countOrConfig) {
  10. var _a;
  11. var count = Infinity;
  12. var delay;
  13. if (countOrConfig != null) {
  14. if (typeof countOrConfig === 'object') {
  15. (_a = countOrConfig.count, count = _a === void 0 ? Infinity : _a, delay = countOrConfig.delay);
  16. }
  17. else {
  18. count = countOrConfig;
  19. }
  20. }
  21. return count <= 0
  22. ? function () { return empty_1.EMPTY; }
  23. : lift_1.operate(function (source, subscriber) {
  24. var soFar = 0;
  25. var sourceSub;
  26. var resubscribe = function () {
  27. sourceSub === null || sourceSub === void 0 ? void 0 : sourceSub.unsubscribe();
  28. sourceSub = null;
  29. if (delay != null) {
  30. var notifier = typeof delay === 'number' ? timer_1.timer(delay) : innerFrom_1.innerFrom(delay(soFar));
  31. var notifierSubscriber_1 = OperatorSubscriber_1.createOperatorSubscriber(subscriber, function () {
  32. notifierSubscriber_1.unsubscribe();
  33. subscribeToSource();
  34. });
  35. notifier.subscribe(notifierSubscriber_1);
  36. }
  37. else {
  38. subscribeToSource();
  39. }
  40. };
  41. var subscribeToSource = function () {
  42. var syncUnsub = false;
  43. sourceSub = source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, undefined, function () {
  44. if (++soFar < count) {
  45. if (sourceSub) {
  46. resubscribe();
  47. }
  48. else {
  49. syncUnsub = true;
  50. }
  51. }
  52. else {
  53. subscriber.complete();
  54. }
  55. }));
  56. if (syncUnsub) {
  57. resubscribe();
  58. }
  59. };
  60. subscribeToSource();
  61. });
  62. }
  63. exports.repeat = repeat;
  64. //# sourceMappingURL=repeat.js.map