repeat.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { EMPTY } from '../observable/empty';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. import { innerFrom } from '../observable/innerFrom';
  5. import { timer } from '../observable/timer';
  6. export function repeat(countOrConfig) {
  7. let count = Infinity;
  8. let delay;
  9. if (countOrConfig != null) {
  10. if (typeof countOrConfig === 'object') {
  11. ({ count = Infinity, delay } = countOrConfig);
  12. }
  13. else {
  14. count = countOrConfig;
  15. }
  16. }
  17. return count <= 0
  18. ? () => EMPTY
  19. : operate((source, subscriber) => {
  20. let soFar = 0;
  21. let sourceSub;
  22. const resubscribe = () => {
  23. sourceSub === null || sourceSub === void 0 ? void 0 : sourceSub.unsubscribe();
  24. sourceSub = null;
  25. if (delay != null) {
  26. const notifier = typeof delay === 'number' ? timer(delay) : innerFrom(delay(soFar));
  27. const notifierSubscriber = createOperatorSubscriber(subscriber, () => {
  28. notifierSubscriber.unsubscribe();
  29. subscribeToSource();
  30. });
  31. notifier.subscribe(notifierSubscriber);
  32. }
  33. else {
  34. subscribeToSource();
  35. }
  36. };
  37. const subscribeToSource = () => {
  38. let syncUnsub = false;
  39. sourceSub = source.subscribe(createOperatorSubscriber(subscriber, undefined, () => {
  40. if (++soFar < count) {
  41. if (sourceSub) {
  42. resubscribe();
  43. }
  44. else {
  45. syncUnsub = true;
  46. }
  47. }
  48. else {
  49. subscriber.complete();
  50. }
  51. }));
  52. if (syncUnsub) {
  53. resubscribe();
  54. }
  55. };
  56. subscribeToSource();
  57. });
  58. }
  59. //# sourceMappingURL=repeat.js.map