Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

switchMap.js 2.3KB

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