Skip to content

Commit

Permalink
feat(switchMap): add higher-order lettable version of switchMap
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jun 15, 2017
1 parent cb1f6d2 commit b6e5b56
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 93 deletions.
96 changes: 3 additions & 93 deletions src/operator/switchMap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Operator } from '../Operator';

import { Observable, ObservableInput } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
import { switchMap as higherOrderSwitchMap } from '../operators';

/* tslint:disable:max-line-length */
export function switchMap<T, R>(this: Observable<T>, project: (value: T, index: number) => ObservableInput<R>): Observable<R>;
Expand Down Expand Up @@ -60,91 +56,5 @@ export function switchMap<T, I, R>(this: Observable<T>, project: (value: T, inde
*/
export function switchMap<T, I, R>(this: Observable<T>, project: (value: T, index: number) => ObservableInput<I>,
resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): Observable<I | R> {
return this.lift(new SwitchMapOperator(project, resultSelector));
}

class SwitchMapOperator<T, I, R> implements Operator<T, I> {
constructor(private project: (value: T, index: number) => ObservableInput<I>,
private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) {
}

call(subscriber: Subscriber<I>, source: any): any {
return source.subscribe(new SwitchMapSubscriber(subscriber, this.project, this.resultSelector));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SwitchMapSubscriber<T, I, R> extends OuterSubscriber<T, I> {
private index: number = 0;
private innerSubscription: Subscription;

constructor(destination: Subscriber<I>,
private project: (value: T, index: number) => ObservableInput<I>,
private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) {
super(destination);
}

protected _next(value: T) {
let result: ObservableInput<I>;
const index = this.index++;
try {
result = this.project(value, index);
} catch (error) {
this.destination.error(error);
return;
}
this._innerSub(result, value, index);
}

private _innerSub(result: ObservableInput<I>, value: T, index: number) {
const innerSubscription = this.innerSubscription;
if (innerSubscription) {
innerSubscription.unsubscribe();
}
this.add(this.innerSubscription = subscribeToResult(this, result, value, index));
}

protected _complete(): void {
const {innerSubscription} = this;
if (!innerSubscription || innerSubscription.closed) {
super._complete();
}
}

protected _unsubscribe() {
this.innerSubscription = null;
}

notifyComplete(innerSub: Subscription): void {
this.remove(innerSub);
this.innerSubscription = null;
if (this.isStopped) {
super._complete();
}
}

notifyNext(outerValue: T, innerValue: I,
outerIndex: number, innerIndex: number,
innerSub: InnerSubscriber<T, I>): void {
if (this.resultSelector) {
this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex);
} else {
this.destination.next(innerValue);
}
}

private _tryNotifyNext(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): void {
let result: R;
try {
result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
} catch (err) {
this.destination.error(err);
return;
}
this.destination.next(result);
}
return higherOrderSwitchMap(project, resultSelector)(this);
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { concatMap } from './concatMap';
export { filter } from './filter';
export { map } from './map';
export { mergeMap } from './mergeMap';
export { switchMap } from './switchMap';
155 changes: 155 additions & 0 deletions src/operators/switchMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { Operator } from '../Operator';
import { Observable, ObservableInput } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
import { OperatorFunction } from '../interfaces';

/* tslint:disable:max-line-length */
export function switchMap<T, R>(project: (value: T, index: number) => ObservableInput<R>): OperatorFunction<T, R>;
export function switchMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
/* 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<T, I, R>(
project: (value: T, index: number) => ObservableInput<I>,
resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R
): OperatorFunction<T, I | R> {
return function switchMapOperatorFunction(source: Observable<T>): Observable<I | R> {
return source.lift(new SwitchMapOperator(project, resultSelector));
};
}

class SwitchMapOperator<T, I, R> implements Operator<T, I> {
constructor(private project: (value: T, index: number) => ObservableInput<I>,
private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) {
}

call(subscriber: Subscriber<I>, source: any): any {
return source.subscribe(new SwitchMapSubscriber(subscriber, this.project, this.resultSelector));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SwitchMapSubscriber<T, I, R> extends OuterSubscriber<T, I> {
private index: number = 0;
private innerSubscription: Subscription;

constructor(destination: Subscriber<I>,
private project: (value: T, index: number) => ObservableInput<I>,
private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) {
super(destination);
}

protected _next(value: T) {
let result: ObservableInput<I>;
const index = this.index++;
try {
result = this.project(value, index);
} catch (error) {
this.destination.error(error);
return;
}
this._innerSub(result, value, index);
}

private _innerSub(result: ObservableInput<I>, value: T, index: number) {
const innerSubscription = this.innerSubscription;
if (innerSubscription) {
innerSubscription.unsubscribe();
}
this.add(this.innerSubscription = subscribeToResult(this, result, value, index));
}

protected _complete(): void {
const {innerSubscription} = this;
if (!innerSubscription || innerSubscription.closed) {
super._complete();
}
}

protected _unsubscribe() {
this.innerSubscription = null;
}

notifyComplete(innerSub: Subscription): void {
this.remove(innerSub);
this.innerSubscription = null;
if (this.isStopped) {
super._complete();
}
}

notifyNext(outerValue: T, innerValue: I,
outerIndex: number, innerIndex: number,
innerSub: InnerSubscriber<T, I>): void {
if (this.resultSelector) {
this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex);
} else {
this.destination.next(innerValue);
}
}

private _tryNotifyNext(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): void {
let result: R;
try {
result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
} catch (err) {
this.destination.error(err);
return;
}
this.destination.next(result);
}
}

0 comments on commit b6e5b56

Please sign in to comment.