Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(single): remove thisArg #928

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CoreOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface CoreOperators<T> {
sampleTime?: (delay: number, scheduler?: Scheduler) => Observable<T>;
scan?: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
share?: () => Observable<T>;
single?: (predicate?: (value: T, index: number) => boolean, thisArg?: any) => Observable<T>;
single?: (predicate?: (value: T, index: number) => boolean) => Observable<T>;
skip?: (count: number) => Observable<T>;
skipUntil?: (notifier: Observable<any>) => Observable<T>;
skipWhile?: (predicate: (x: T, index: number) => boolean, thisArg?: any) => 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 @@ -229,7 +229,7 @@ export class Observable<T> implements CoreOperators<T> {
sampleTime: (delay: number, scheduler?: Scheduler) => Observable<T>;
scan: <R>(accumulator: (acc: R, x: T) => R, seed?: T | R) => Observable<R>;
share: () => Observable<T>;
single: (predicate?: (value: T, index: number) => boolean, thisArg?: any) => Observable<T>;
single: (predicate?: (value: T, index: number) => boolean) => Observable<T>;
skip: (count: number) => Observable<T>;
skipUntil: (notifier: Observable<any>) => Observable<T>;
skipWhile: (predicate: (x: T, index: number) => boolean, thisArg?: any) => Observable<T>;
Expand Down
17 changes: 4 additions & 13 deletions src/operator/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,33 @@ import {Observer} from '../Observer';

import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {bindCallback} from '../util/bindCallback';
import {EmptyError} from '../util/EmptyError';

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

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

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

class SingleSubscriber<T> extends Subscriber<T> {
private predicate: Function;
private seenValue: boolean = false;
private singleValue: T;
private index: number = 0;

constructor(destination: Observer<T>,
predicate?: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg?: any,
private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
private source?: Observable<T>) {
super(destination);

if (typeof predicate === 'function') {
this.predicate = bindCallback(predicate, thisArg, 3);
}
}

private applySingleValue(value): void {
Expand Down