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

Fix elementAt defaultValue, make elementAt impl smaller #3506

Merged
merged 2 commits into from Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion compat/operator/elementAt.ts
Expand Up @@ -45,5 +45,5 @@ import { elementAt as higherOrder } from 'rxjs/operators';
* @owner Observable
*/
export function elementAt<T>(this: Observable<T>, index: number, defaultValue?: T): Observable<T> {
return higherOrder(index, defaultValue)(this);
return higherOrder.apply(undefined, arguments)(this);
}
58 changes: 13 additions & 45 deletions src/internal/operators/elementAt.ts
Expand Up @@ -3,6 +3,10 @@ import { Subscriber } from '../Subscriber';
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
import { Observable } from '../Observable';
import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
import { filter } from './filter';
import { throwIfEmpty } from './throwIfEmpty';
import { defaultIfEmpty } from './defaultIfEmpty';
import { take } from './take';

/**
* Emits the single value at the specified `index` in a sequence of emissions
Expand Down Expand Up @@ -47,49 +51,13 @@ import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
* @owner Observable
*/
export function elementAt<T>(index: number, defaultValue?: T): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => source.lift(new ElementAtOperator(index, defaultValue));
}

class ElementAtOperator<T> implements Operator<T, T> {

constructor(private index: number, private defaultValue?: T) {
if (index < 0) {
throw new ArgumentOutOfRangeError;
}
}

call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(new ElementAtSubscriber(subscriber, this.index, this.defaultValue));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class ElementAtSubscriber<T> extends Subscriber<T> {

constructor(destination: Subscriber<T>, private index: number, private defaultValue?: T) {
super(destination);
}

protected _next(x: T) {
if (this.index-- === 0) {
this.destination.next(x);
this.destination.complete();
}
}

protected _complete() {
const destination = this.destination;
if (this.index >= 0) {
if (typeof this.defaultValue !== 'undefined') {
destination.next(this.defaultValue);
} else {
destination.error(new ArgumentOutOfRangeError);
}
}
destination.complete();
}
if (index < 0) { throw new ArgumentOutOfRangeError(); }
const hasDefaultValue = arguments.length >= 2;
return (source: Observable<T>) => source.pipe(
filter((v, i) => i === index),
take(1),
hasDefaultValue
? defaultIfEmpty(defaultValue)
: throwIfEmpty(() => new ArgumentOutOfRangeError()),
);
}