Skip to content

Commit

Permalink
perf(ScalarObservable): add fast-path for mapping scalar observables
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Oct 14, 2015
1 parent acf56ec commit 7b0d3dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions spec/operators/retry-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ describe('Observable.prototype.retry()', function () {
it('should retry a number of times, without error, then complete', function (done) {
var errors = 0;
var retries = 2;
Observable.of(42)
Observable.create(function (observer) {
observer.next(42);
observer.complete();
})
.map(function (x) {
if ((errors += 1) < retries) {
throw 'bad';
Expand All @@ -27,7 +30,10 @@ describe('Observable.prototype.retry()', function () {
it('should retry a number of times, then call error handler', function (done) {
var errors = 0;
var retries = 2;
Observable.of(42)
Observable.create(function (observer) {
observer.next(42);
observer.complete();
})
.map(function (x) {
if ((errors += 1) < retries) {
throw 'bad';
Expand All @@ -50,7 +56,10 @@ describe('Observable.prototype.retry()', function () {
it('should retry until successful completion', function (done) {
var errors = 0;
var retries = 10;
Observable.of(42)
Observable.create(function (observer) {
observer.next(42);
observer.complete();
})
.map(function (x) {
if ((errors += 1) < retries) {
throw 'bad';
Expand Down
13 changes: 13 additions & 0 deletions src/observables/ScalarObservable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Scheduler from '../Scheduler';
import Observable from '../Observable';
import tryCatch from '../util/tryCatch';
import { errorObject } from '../util/errorObject';
import ErrorObservable from './ErrorObservable';
import EmptyObservable from './EmptyObservable';

export default class ScalarObservable<T> extends Observable<T> {

Expand Down Expand Up @@ -49,4 +53,13 @@ export default class ScalarObservable<T> extends Observable<T> {
}
}
}

map<R>(project: (x: T, ix?: number) => R, thisArg?: any): Observable<R> {
let result = tryCatch(project).call(thisArg || this, this.value, 0);
if (result === errorObject) {
return new ErrorObservable(errorObject.e);
} else {
return new ScalarObservable(project.call(thisArg || this, this.value, 0));
}
}
}

0 comments on commit 7b0d3dc

Please sign in to comment.