Skip to content

Commit

Permalink
refactor(switchLatestTo): update to use refactored flatMapTo
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Sep 15, 2015
1 parent 8cd757f commit 82deffb
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions src/operators/switchLatestTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,30 @@ import Observable from '../Observable';
import Subscriber from '../Subscriber';
import Subscription from '../Subscription';

import { FlatMapToOperator, FlatMapToSubscriber } from './flatMapTo-support';
import { FlatMapToSubscriber } from './flatMapTo-support';

export default function switchLatestTo<T, R>(observable: Observable<any>,
projectResult?: (x: T, y: any, ix: number, iy: number) => R): Observable<R> {
export default function switchLatestTo<T, R, R2>(observable: Observable<R>,
projectResult?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2): Observable<R2> {
return this.lift(new SwitchLatestToOperator(observable, projectResult));
}

class SwitchLatestToOperator<T, R> extends FlatMapToOperator<T, R> {

constructor(observable: Observable<any>,
projectResult?: (x: T, y: any, ix: number, iy: number) => R) {
super(observable, projectResult, 1);
class SwitchLatestToOperator<T, R, R2> implements Operator<T, R> {
constructor(private observable: Observable<R>,
private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) {
}

call(subscriber: Subscriber<R>): Subscriber<T> {
return new SwitchLatestToSubscriber(subscriber, this.observable, this.projectResult);
return new SwitchLatestToSubscriber(subscriber, this.observable, this.resultSelector);
}
}

class SwitchLatestToSubscriber<T, R> extends FlatMapToSubscriber<T, R> {
class SwitchLatestToSubscriber<T, R, R2> extends FlatMapToSubscriber<T, R, R2> {

innerSubscription: Subscription<T>;

constructor(destination: Observer<R>,
observable: Observable<any>,
projectResult?: (x: T, y: any, ix: number, iy: number) => R) {
super(destination, 1, observable, projectResult);
}

_buffer(value) {
const active = this.active;
if(active > 0) {
this.active = active - 1;
const inner = this.innerSubscription;
if(inner) {
inner.unsubscribe()
}
}
this._next(value);
}

_subscribeInner(observable, value, index) {
return (this.innerSubscription = super._subscribeInner(observable, value, index));
constructor(destination: Observer<T>,
observable: Observable<R>,
resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) {
super(destination, observable, resultSelector, 1);
}
}

0 comments on commit 82deffb

Please sign in to comment.