operators/concat.js

  1. import { Observable } from '../Observable';
  2. /**
  3. * Concatenate any number of observables together
  4. *
  5. * @memberof operators
  6. *
  7. * @param {Observable} source$
  8. * @param {Observable} nextSource$
  9. * @param {Observable[]} otherSources$
  10. * @returns {Observable}
  11. */
  12. export const concat = function (source$, nextSource$, ...otherSources$) {
  13. return new Observable(function ({ next, error, complete }) {
  14. let innerSubscription;
  15. const completeSubscription = () => {
  16. if (nextSource$ && otherSources$.length) {
  17. innerSubscription = concat(nextSource$, ...otherSources$).subscribe({
  18. next,
  19. error,
  20. complete,
  21. });
  22. } else if (nextSource$) {
  23. innerSubscription = nextSource$.subscribe({
  24. next,
  25. error,
  26. complete,
  27. });
  28. } else {
  29. complete();
  30. }
  31. };
  32. const subscription = source$.subscribe({
  33. next,
  34. error,
  35. complete () {
  36. completeSubscription();
  37. }
  38. });
  39. return () => {
  40. if (innerSubscription) {
  41. innerSubscription.unsubscribe();
  42. }
  43. subscription.unsubscribe();
  44. };
  45. });
  46. };
  47. Observable.concat = concat;
  48. Observable.prototype.concat = function (...args$) {
  49. return concat(this, ...args$);
  50. };