mergeInternals.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { innerFrom } from '../observable/innerFrom';
  2. import { executeSchedule } from '../util/executeSchedule';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. export function mergeInternals(source, subscriber, project, concurrent, onBeforeNext, expand, innerSubScheduler, additionalFinalizer) {
  5. const buffer = [];
  6. let active = 0;
  7. let index = 0;
  8. let isComplete = false;
  9. const checkComplete = () => {
  10. if (isComplete && !buffer.length && !active) {
  11. subscriber.complete();
  12. }
  13. };
  14. const outerNext = (value) => (active < concurrent ? doInnerSub(value) : buffer.push(value));
  15. const doInnerSub = (value) => {
  16. expand && subscriber.next(value);
  17. active++;
  18. let innerComplete = false;
  19. innerFrom(project(value, index++)).subscribe(createOperatorSubscriber(subscriber, (innerValue) => {
  20. onBeforeNext === null || onBeforeNext === void 0 ? void 0 : onBeforeNext(innerValue);
  21. if (expand) {
  22. outerNext(innerValue);
  23. }
  24. else {
  25. subscriber.next(innerValue);
  26. }
  27. }, () => {
  28. innerComplete = true;
  29. }, undefined, () => {
  30. if (innerComplete) {
  31. try {
  32. active--;
  33. while (buffer.length && active < concurrent) {
  34. const bufferedValue = buffer.shift();
  35. if (innerSubScheduler) {
  36. executeSchedule(subscriber, innerSubScheduler, () => doInnerSub(bufferedValue));
  37. }
  38. else {
  39. doInnerSub(bufferedValue);
  40. }
  41. }
  42. checkComplete();
  43. }
  44. catch (err) {
  45. subscriber.error(err);
  46. }
  47. }
  48. }));
  49. };
  50. source.subscribe(createOperatorSubscriber(subscriber, outerNext, () => {
  51. isComplete = true;
  52. checkComplete();
  53. }));
  54. return () => {
  55. additionalFinalizer === null || additionalFinalizer === void 0 ? void 0 : additionalFinalizer();
  56. };
  57. }
  58. //# sourceMappingURL=mergeInternals.js.map