Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

switchMap.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { map } from './map';
  2. import { from } from '../observable/from';
  3. import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
  4. export function switchMap(project, resultSelector) {
  5. if (typeof resultSelector === 'function') {
  6. return (source) => source.pipe(switchMap((a, i) => from(project(a, i)).pipe(map((b, ii) => resultSelector(a, b, i, ii)))));
  7. }
  8. return (source) => source.lift(new SwitchMapOperator(project));
  9. }
  10. class SwitchMapOperator {
  11. constructor(project) {
  12. this.project = project;
  13. }
  14. call(subscriber, source) {
  15. return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
  16. }
  17. }
  18. class SwitchMapSubscriber extends SimpleOuterSubscriber {
  19. constructor(destination, project) {
  20. super(destination);
  21. this.project = project;
  22. this.index = 0;
  23. }
  24. _next(value) {
  25. let result;
  26. const index = this.index++;
  27. try {
  28. result = this.project(value, index);
  29. }
  30. catch (error) {
  31. this.destination.error(error);
  32. return;
  33. }
  34. this._innerSub(result);
  35. }
  36. _innerSub(result) {
  37. const innerSubscription = this.innerSubscription;
  38. if (innerSubscription) {
  39. innerSubscription.unsubscribe();
  40. }
  41. const innerSubscriber = new SimpleInnerSubscriber(this);
  42. const destination = this.destination;
  43. destination.add(innerSubscriber);
  44. this.innerSubscription = innerSubscribe(result, innerSubscriber);
  45. if (this.innerSubscription !== innerSubscriber) {
  46. destination.add(this.innerSubscription);
  47. }
  48. }
  49. _complete() {
  50. const { innerSubscription } = this;
  51. if (!innerSubscription || innerSubscription.closed) {
  52. super._complete();
  53. }
  54. this.unsubscribe();
  55. }
  56. _unsubscribe() {
  57. this.innerSubscription = undefined;
  58. }
  59. notifyComplete() {
  60. this.innerSubscription = undefined;
  61. if (this.isStopped) {
  62. super._complete();
  63. }
  64. }
  65. notifyNext(innerValue) {
  66. this.destination.next(innerValue);
  67. }
  68. }
  69. //# sourceMappingURL=switchMap.js.map