Skip to content

Commit

Permalink
feat(takeWhile): remove thisArg
Browse files Browse the repository at this point in the history
relates to #878
  • Loading branch information
kwonoj authored and benlesh committed Dec 4, 2015
1 parent 8637d47 commit b5219a4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 32 deletions.
17 changes: 0 additions & 17 deletions spec/operators/takeWhile-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,6 @@ describe('Observable.prototype.takeWhile()', function () {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should pass element thisArg to predicate', function () {
var e1 = hot('--a-^-b--c--d--e--|');
var e1subs = '^ ! ';
var expected = '--b--c--| ';

function predicate() {
this.take = function (value) {
return value !== 'd';
};
}

var result = e1.takeWhile(function (v) { return this.take(v); }, new predicate());

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should take elements until unsubscribed', function () {
var e1 = hot('--a-^-b--c--d--e--|');
var unsub = '-----! ';
Expand Down
2 changes: 1 addition & 1 deletion src/CoreOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface CoreOperators<T> {
switchMapTo?: <R>(observable: Observable<any>, projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
take?: (count: number) => Observable<T>;
takeUntil?: (notifier: Observable<any>) => Observable<T>;
takeWhile?: (predicate: (value: T, index: number) => boolean, thisArg?: any) => Observable<T>;
takeWhile?: (predicate: (value: T, index: number) => boolean) => Observable<T>;
throttle?: (durationSelector: (value: T) => Observable<any> | Promise<any>) => Observable<T>;
throttleTime?: (delay: number, scheduler?: Scheduler) => Observable<T>;
timeout?: (due: number | Date, errorToSend?: any, scheduler?: Scheduler) => Observable<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class Observable<T> implements CoreOperators<T> {
switchMapTo: <R>(observable: Observable<any>, projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
take: (count: number) => Observable<T>;
takeUntil: (notifier: Observable<any>) => Observable<T>;
takeWhile: (predicate: (value: T, index: number) => boolean, thisArg?: any) => Observable<T>;
takeWhile: (predicate: (value: T, index: number) => boolean) => Observable<T>;
throttle: (durationSelector: (value: T) => Observable<any> | Promise<any>) => Observable<T>;
throttleTime: (delay: number, scheduler?: Scheduler) => Observable<T>;
timeout: (due: number | Date, errorToSend?: any, scheduler?: Scheduler) => Observable<T>;
Expand Down
18 changes: 5 additions & 13 deletions src/operator/takeWhile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@ import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {bindCallback} from '../util/bindCallback';

export function takeWhile<T>(predicate: (value: T, index: number) => boolean,
thisArg?: any): Observable<T> {
return this.lift(new TakeWhileOperator(predicate, thisArg));
export function takeWhile<T>(predicate: (value: T, index: number) => boolean): Observable<T> {
return this.lift(new TakeWhileOperator(predicate));
}

class TakeWhileOperator<T, R> implements Operator<T, R> {
constructor(private predicate: (value: T, index: number) => boolean,
private thisArg?: any) {
constructor(private predicate: (value: T, index: number) => boolean) {
}

call(subscriber: Subscriber<T>): Subscriber<T> {
return new TakeWhileSubscriber(subscriber, this.predicate, this.thisArg);
return new TakeWhileSubscriber(subscriber, this.predicate);
}
}

class TakeWhileSubscriber<T> extends Subscriber<T> {
private predicate: (value: T, index: number) => boolean;
private index: number = 0;

constructor(destination: Subscriber<T>,
predicate: (value: T, index: number) => boolean,
thisArg?: any) {
private predicate: (value: T, index: number) => boolean) {
super(destination);
if (typeof predicate === 'function') {
this.predicate = <(value: T, index: number) => boolean>bindCallback(predicate, thisArg, 2);
}
}

_next(value: T): void {
Expand Down

0 comments on commit b5219a4

Please sign in to comment.