From c18c42ea66f093859f7239faea456598102f4bff Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 12 Jan 2016 17:06:31 -0800 Subject: [PATCH] fix(ScalarObservable): fix issue where scalar map fired twice - removes special optimization from ScalarObservable closes #1142 fixes #1140 --- spec/observables/ScalarObservable-spec.js | 179 +--------------------- src/observable/ScalarObservable.ts | 71 --------- 2 files changed, 2 insertions(+), 248 deletions(-) diff --git a/spec/observables/ScalarObservable-spec.js b/spec/observables/ScalarObservable-spec.js index 2f7bf73fed..aab2c3f8e4 100644 --- a/spec/observables/ScalarObservable-spec.js +++ b/spec/observables/ScalarObservable-spec.js @@ -11,7 +11,7 @@ describe('ScalarObservable', function () { expect(s.value).toBe(1); }); - it('should create ScalarObservable via staic create function', function () { + it('should create ScalarObservable via static create function', function () { var s = new ScalarObservable(1); var r = ScalarObservable.create(1); @@ -25,179 +25,4 @@ describe('ScalarObservable', function () { subscriber.isUnsubscribed = true; rxTestScheduler.flush(); }); - - describe('prototype.map()', function () { - it('should map to a new ScalarObservable', function () { - var s = new ScalarObservable(1); - var r = s.map(function (x) { return x + '!!!'; }); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe('1!!!'); - }); - - it('should map using a custom thisArg', function () { - var s = new ScalarObservable(1); - var foo = 42; - var selector = function (x) { - expect(this).toEqual(foo); - return x + '!!!'; - }; - var r = s.map(selector, 42); - - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe('1!!!'); - }); - - it('should return an ErrorObservable if map errors', function () { - var s = new ScalarObservable(1); - var r = s.map(function (x) { throw 'bad!'; }); - expect(r instanceof ErrorObservable).toBe(true); - expect(r.error).toBe('bad!'); - }); - }); - - describe('prototype.count()', function () { - it('should map to a new ScalarObservable of 1', function () { - var s = new ScalarObservable(1); - var r = s.count(); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe(1); - }); - - it('should map to a new ScalarObservable of 1 if predicate matches', function () { - var s = new ScalarObservable(1); - var r = s.count(function (x) { return x === 1; }); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe(1); - }); - - it('should map to a new ScalarObservable of 0 if predicate does not match', function () { - var s = new ScalarObservable(1); - var r = s.count(function (x) { return x === 0; }); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe(0); - }); - - it('should map to a new ErrorObservable if predicate errors', function () { - var s = new ScalarObservable(1); - var r = s.count(function () { throw 'bad!'; }); - expect(r instanceof ErrorObservable).toBe(true); - expect(r.error).toBe('bad!'); - }); - }); - - describe('prototype.filter()', function () { - it('should return itself if the filter matches its value', function () { - var s = new ScalarObservable(1); - var r = s.filter(function (x) { return x === 1; }); - expect(s).toBe(r); - }); - - it('should filter using a custom thisArg', function () { - var s = new ScalarObservable(1); - var filterer = { - filter1: function (x) { return x > 0; }, - filter2: function (x) { return x === 1; } - }; - - var r = s - .filter(function (x) { return this.filter1(x); }, filterer) - .filter(function (x) { return this.filter2(x); }, filterer); - - expect(s).toBe(r); - }); - - it('should return EmptyObservable if filter does not match', function () { - var s = new ScalarObservable(1); - var r = s.filter(function (x) { return x === 0; }); - expect(r instanceof EmptyObservable).toBe(true); - }); - - it('should map to a new ErrorObservable if predicate errors', function () { - var s = new ScalarObservable(1); - var r = s.filter(function () { throw 'bad!'; }); - expect(r instanceof ErrorObservable).toBe(true); - expect(r.error).toBe('bad!'); - }); - }); - - describe('prototype.take()', function () { - it('should return itself if count > 0', function () { - var s = new ScalarObservable(1); - var r = s.take(1); - expect(s).toBe(r); - }); - - it('should return EmptyObservable if count === 0', function () { - var s = new ScalarObservable(1); - var r = s.take(0); - expect(r instanceof EmptyObservable).toBe(true); - }); - }); - - describe('prototype.skip()', function () { - it('should return itself if count === 0', function () { - var s = new ScalarObservable(1); - var r = s.skip(0); - expect(s).toBe(r); - }); - - it('should return EmptyObservable if count > 0', function () { - var s = new ScalarObservable(1); - var r = s.skip(1); - expect(r instanceof EmptyObservable).toBe(true); - }); - }); - - describe('prototype.reduce()', function () { - it('should return a ScalarObservable of the result if there is a seed', function () { - var s = new ScalarObservable(1); - var r = s.reduce(function (a, x) { return a + x; }, 1); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe(2); - }); - - it('should return itself if there is no seed', function () { - var s = new ScalarObservable(1); - var r = s.reduce(function (a, x) { return a + x; }); - expect(r).toBe(s); - }); - - it('should return ErrorObservable if projection throws', function () { - var s = new ScalarObservable(1); - var r = s.reduce(function (a, x) { throw 'error'; }, 1); - var expected = new ErrorObservable('error'); - - expect(r).toEqual(expected); - }); - }); - - describe('prototype.scan()', function () { - it('should return a ScalarObservable of the result if there is a seed', function () { - var s = new ScalarObservable(1); - var r = s.scan(function (a, x) { return a + x; }, 1); - expect(r instanceof ScalarObservable).toBe(true); - expect(r.value).toBe(2); - }); - - it('should return itself if there is no seed', function () { - var s = new ScalarObservable(1); - var r = s.scan(function (a, x) { return a + x; }); - expect(r).toBe(s); - }); - - it('should return ErrorObservable if projection throws', function () { - var s = new ScalarObservable(1); - var r = s.scan(function (a, x) { throw 'error'; }, 1); - var expected = new ErrorObservable('error'); - - expect(r).toEqual(expected); - }); - }); -}); - -// If you uncomment this, jasmine breaks? WEIRD -// describe('reality', function () { -// it('should exist in this universe', function () { -// expect(true).toBe(true); -// }); -// }); \ No newline at end of file +}); \ No newline at end of file diff --git a/src/observable/ScalarObservable.ts b/src/observable/ScalarObservable.ts index 3499e0a08a..f62339acf6 100644 --- a/src/observable/ScalarObservable.ts +++ b/src/observable/ScalarObservable.ts @@ -3,11 +3,6 @@ import {Observable} from '../Observable'; import {Subscriber} from '../Subscriber'; import {Subscription} from '../Subscription'; -import {tryCatch} from '../util/tryCatch'; -import {errorObject} from '../util/errorObject'; -import {ErrorObservable} from './throw'; -import {EmptyObservable} from './empty'; - export class ScalarObservable extends Observable { static create(value: T, scheduler?: Scheduler): ScalarObservable { return new ScalarObservable(value, scheduler); @@ -52,69 +47,3 @@ export class ScalarObservable extends Observable { } } } - -// TypeScript is weird about class prototype member functions and instance properties touching on it's plate. -const proto = ScalarObservable.prototype; - -proto.map = function (project: (x: T, ix?: number) => R, thisArg?: any): Observable { - 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)); - } -}; - -proto.filter = function (select: (x: T, ix?: number) => boolean, thisArg?: any): Observable { - let result = tryCatch(select).call(thisArg || this, this.value, 0); - if (result === errorObject) { - return new ErrorObservable(errorObject.e); - } else if (result) { - return this; - } else { - return new EmptyObservable(); - } -}; - -proto.reduce = function (project: (acc: R, x: T) => R, seed?: R): Observable { - if (typeof seed === 'undefined') { - return this; - } - let result = tryCatch(project)(seed, this.value); - if (result === errorObject) { - return new ErrorObservable(errorObject.e); - } else { - return new ScalarObservable(result); - } -}; - -proto.scan = function (project: (acc: R, x: T) => R, acc?: R): Observable { - return this.reduce(project, acc); -}; - -proto.count = function (predicate?: (value: T, index: number, source: Observable) => boolean): Observable { - if (!predicate) { - return new ScalarObservable(1); - } else { - let result = tryCatch(predicate).call(this, this.value, 0, this); - if (result === errorObject) { - return new ErrorObservable(errorObject.e); - } else { - return new ScalarObservable(result ? 1 : 0); - } - } -}; - -proto.skip = function (count: number): Observable { - if (count > 0) { - return new EmptyObservable(); - } - return this; -}; - -proto.take = function (count: number): Observable { - if (count > 0) { - return this; - } - return new EmptyObservable(); -};