mirror of
https://github.com/biobulkbende/biobulkbende.org.git
synced 2024-12-27 05:15:25 +00:00
132 lines
5.0 KiB
JavaScript
132 lines
5.0 KiB
JavaScript
import { OuterSubscriber } from '../OuterSubscriber';
|
|
import { subscribeToResult } from '../util/subscribeToResult';
|
|
/* 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.
|
|
* @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
|
|
* A function to produce the value on the output Observable based on the values
|
|
* and the indices of the source (outer) emission and the inner Observable
|
|
* emission. The arguments passed to this function are:
|
|
* - `outerValue`: the value that came from the source
|
|
* - `innerValue`: the value that came from the projected Observable
|
|
* - `outerIndex`: the "index" of the value that came from the source
|
|
* - `innerIndex`: the "index" of the value from the projected 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) {
|
|
return function switchMapOperatorFunction(source) {
|
|
return source.lift(new SwitchMapOperator(project, resultSelector));
|
|
};
|
|
}
|
|
class SwitchMapOperator {
|
|
constructor(project, resultSelector) {
|
|
this.project = project;
|
|
this.resultSelector = resultSelector;
|
|
}
|
|
call(subscriber, source) {
|
|
return source.subscribe(new SwitchMapSubscriber(subscriber, this.project, this.resultSelector));
|
|
}
|
|
}
|
|
/**
|
|
* We need this JSDoc comment for affecting ESDoc.
|
|
* @ignore
|
|
* @extends {Ignored}
|
|
*/
|
|
class SwitchMapSubscriber extends OuterSubscriber {
|
|
constructor(destination, project, resultSelector) {
|
|
super(destination);
|
|
this.project = project;
|
|
this.resultSelector = resultSelector;
|
|
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();
|
|
}
|
|
}
|
|
/** @deprecated internal use only */ _unsubscribe() {
|
|
this.innerSubscription = null;
|
|
}
|
|
notifyComplete(innerSub) {
|
|
this.remove(innerSub);
|
|
this.innerSubscription = null;
|
|
if (this.isStopped) {
|
|
super._complete();
|
|
}
|
|
}
|
|
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
|
if (this.resultSelector) {
|
|
this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex);
|
|
}
|
|
else {
|
|
this.destination.next(innerValue);
|
|
}
|
|
}
|
|
_tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex) {
|
|
let result;
|
|
try {
|
|
result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
return;
|
|
}
|
|
this.destination.next(result);
|
|
}
|
|
}
|
|
//# sourceMappingURL=switchMap.js.map
|