Opal-Estate-Pro/node_modules/rxjs/_esm2015/operators/last.js
2019-09-13 11:27:52 +07:00

109 lines
3.6 KiB
JavaScript
Executable File

import { Subscriber } from '../Subscriber';
import { EmptyError } from '../util/EmptyError';
/* tslint:enable:max-line-length */
/**
* Returns an Observable that emits only the last item emitted by the source Observable.
* It optionally takes a predicate function as a parameter, in which case, rather than emitting
* the last item from the source Observable, the resulting Observable will emit the last item
* from the source Observable that satisfies the predicate.
*
* <img src="./img/last.png" width="100%">
*
* @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
* callback if the Observable completes before any `next` notification was sent.
* @param {function} predicate - The condition any source emitted item has to satisfy.
* @return {Observable} An Observable that emits only the last item satisfying the given condition
* from the source, or an NoSuchElementException if no such items are emitted.
* @throws - Throws if no items that match the predicate are emitted by the source Observable.
* @method last
* @owner Observable
*/
export function last(predicate, resultSelector, defaultValue) {
return (source) => source.lift(new LastOperator(predicate, resultSelector, defaultValue, source));
}
class LastOperator {
constructor(predicate, resultSelector, defaultValue, source) {
this.predicate = predicate;
this.resultSelector = resultSelector;
this.defaultValue = defaultValue;
this.source = source;
}
call(observer, source) {
return source.subscribe(new LastSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class LastSubscriber extends Subscriber {
constructor(destination, predicate, resultSelector, defaultValue, source) {
super(destination);
this.predicate = predicate;
this.resultSelector = resultSelector;
this.defaultValue = defaultValue;
this.source = source;
this.hasValue = false;
this.index = 0;
if (typeof defaultValue !== 'undefined') {
this.lastValue = defaultValue;
this.hasValue = true;
}
}
_next(value) {
const index = this.index++;
if (this.predicate) {
this._tryPredicate(value, index);
}
else {
if (this.resultSelector) {
this._tryResultSelector(value, index);
return;
}
this.lastValue = value;
this.hasValue = true;
}
}
_tryPredicate(value, index) {
let result;
try {
result = this.predicate(value, index, this.source);
}
catch (err) {
this.destination.error(err);
return;
}
if (result) {
if (this.resultSelector) {
this._tryResultSelector(value, index);
return;
}
this.lastValue = value;
this.hasValue = true;
}
}
_tryResultSelector(value, index) {
let result;
try {
result = this.resultSelector(value, index);
}
catch (err) {
this.destination.error(err);
return;
}
this.lastValue = result;
this.hasValue = true;
}
_complete() {
const destination = this.destination;
if (this.hasValue) {
destination.next(this.lastValue);
destination.complete();
}
else {
destination.error(new EmptyError);
}
}
}
//# sourceMappingURL=last.js.map