rxjs
Version:
Reactive Extensions for modern JavaScript
24 lines • 1.16 kB
JavaScript
import { innerFrom } from '../observable/innerFrom';
import { operate } from '../util/lift';
import { OperatorSubscriber } from './OperatorSubscriber';
export function switchMap(project, resultSelector) {
return operate((source, subscriber) => {
let innerSubscriber = null;
let index = 0;
let isComplete = false;
const checkComplete = () => isComplete && !innerSubscriber && subscriber.complete();
source.subscribe(new OperatorSubscriber(subscriber, (value) => {
innerSubscriber === null || innerSubscriber === void 0 ? void 0 : innerSubscriber.unsubscribe();
let innerIndex = 0;
const outerIndex = index++;
innerFrom(project(value, outerIndex)).subscribe((innerSubscriber = new OperatorSubscriber(subscriber, (innerValue) => subscriber.next(resultSelector ? resultSelector(value, innerValue, outerIndex, innerIndex++) : innerValue), () => {
innerSubscriber = null;
checkComplete();
})));
}, () => {
isComplete = true;
checkComplete();
}));
});
}
//# sourceMappingURL=switchMap.js.map