UNPKG

wed

Version:

Wed is a schema-aware editor for XML documents.

115 lines (112 loc) 4.06 kB
define(function(require,exports,module){ import { OuterSubscriber } from '../OuterSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; import { map } from './map'; import { from } from '../observable/from'; /* tslint:enable:max-line-length */ /** * Projects each source value to an Observable which is merged in the output * Observable, emitting values only from the most recently projected Observable. * * <span class="informal">Maps each value to an Observable, then flattens all of * these inner Observables using {@link switch}.</span> * * <img src="./img/switchMap.png" width="100%"> * * Returns an Observable that emits items based on applying a function that you * supply to each item emitted by the source Observable, where that function * returns an (so-called "inner") Observable. Each time it observes one of these * inner Observables, the output Observable begins emitting the items emitted by * that inner Observable. When a new inner Observable is emitted, `switchMap` * stops emitting items from the earlier-emitted inner Observable and begins * emitting items from the new one. It continues to behave like this for * subsequent inner Observables. * * @example <caption>Rerun an interval Observable on every click event</caption> * var clicks = Rx.Observable.fromEvent(document, 'click'); * var result = clicks.switchMap((ev) => Rx.Observable.interval(1000)); * result.subscribe(x => console.log(x)); * * @see {@link concatMap} * @see {@link exhaustMap} * @see {@link mergeMap} * @see {@link switch} * @see {@link switchMapTo} * * @param {function(value: T, ?index: number): ObservableInput} project A function * that, when applied to an item emitted by the source Observable, returns an * Observable. * @return {Observable} An Observable that emits the result of applying the * projection function (and the optional `resultSelector`) to each item emitted * by the source Observable and taking only the values from the most recently * projected inner Observable. * @method switchMap * @owner Observable */ export function switchMap(project, resultSelector) { if (typeof resultSelector === 'function') { return (source) => source.pipe(switchMap((a, i) => from(project(a, i)).pipe(map((b, ii) => resultSelector(a, b, i, ii))))); } return (source) => source.lift(new SwitchMapOperator(project)); } class SwitchMapOperator { constructor(project) { this.project = project; } call(subscriber, source) { return source.subscribe(new SwitchMapSubscriber(subscriber, this.project)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class SwitchMapSubscriber extends OuterSubscriber { constructor(destination, project) { super(destination); this.project = project; this.index = 0; } _next(value) { let result; const index = this.index++; try { result = this.project(value, index); } catch (error) { this.destination.error(error); return; } this._innerSub(result, value, index); } _innerSub(result, value, index) { const innerSubscription = this.innerSubscription; if (innerSubscription) { innerSubscription.unsubscribe(); } this.add(this.innerSubscription = subscribeToResult(this, result, value, index)); } _complete() { const { innerSubscription } = this; if (!innerSubscription || innerSubscription.closed) { super._complete(); } } _unsubscribe() { this.innerSubscription = null; } notifyComplete(innerSub) { this.remove(innerSub); this.innerSubscription = null; if (this.isStopped) { super._complete(); } } notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) { this.destination.next(innerValue); } } //# sourceMappingURL=switchMap.js.map return module.exports; });