repeatWhen.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { innerFrom } from '../observable/innerFrom';
  2. import { Subject } from '../Subject';
  3. import { operate } from '../util/lift';
  4. import { createOperatorSubscriber } from './OperatorSubscriber';
  5. export function repeatWhen(notifier) {
  6. return operate((source, subscriber) => {
  7. let innerSub;
  8. let syncResub = false;
  9. let completions$;
  10. let isNotifierComplete = false;
  11. let isMainComplete = false;
  12. const checkComplete = () => isMainComplete && isNotifierComplete && (subscriber.complete(), true);
  13. const getCompletionSubject = () => {
  14. if (!completions$) {
  15. completions$ = new Subject();
  16. innerFrom(notifier(completions$)).subscribe(createOperatorSubscriber(subscriber, () => {
  17. if (innerSub) {
  18. subscribeForRepeatWhen();
  19. }
  20. else {
  21. syncResub = true;
  22. }
  23. }, () => {
  24. isNotifierComplete = true;
  25. checkComplete();
  26. }));
  27. }
  28. return completions$;
  29. };
  30. const subscribeForRepeatWhen = () => {
  31. isMainComplete = false;
  32. innerSub = source.subscribe(createOperatorSubscriber(subscriber, undefined, () => {
  33. isMainComplete = true;
  34. !checkComplete() && getCompletionSubject().next();
  35. }));
  36. if (syncResub) {
  37. innerSub.unsubscribe();
  38. innerSub = null;
  39. syncResub = false;
  40. subscribeForRepeatWhen();
  41. }
  42. };
  43. subscribeForRepeatWhen();
  44. });
  45. }
  46. //# sourceMappingURL=repeatWhen.js.map