Skip to content

Commit c18c42e

Browse files
committed
fix(ScalarObservable): fix issue where scalar map fired twice
- removes special optimization from ScalarObservable closes #1142 fixes #1140
1 parent f42eed2 commit c18c42e

File tree

2 files changed

+2
-248
lines changed

2 files changed

+2
-248
lines changed

spec/observables/ScalarObservable-spec.js

+2-177
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('ScalarObservable', function () {
1111
expect(s.value).toBe(1);
1212
});
1313

14-
it('should create ScalarObservable via staic create function', function () {
14+
it('should create ScalarObservable via static create function', function () {
1515
var s = new ScalarObservable(1);
1616
var r = ScalarObservable.create(1);
1717

@@ -25,179 +25,4 @@ describe('ScalarObservable', function () {
2525
subscriber.isUnsubscribed = true;
2626
rxTestScheduler.flush();
2727
});
28-
29-
describe('prototype.map()', function () {
30-
it('should map to a new ScalarObservable', function () {
31-
var s = new ScalarObservable(1);
32-
var r = s.map(function (x) { return x + '!!!'; });
33-
expect(r instanceof ScalarObservable).toBe(true);
34-
expect(r.value).toBe('1!!!');
35-
});
36-
37-
it('should map using a custom thisArg', function () {
38-
var s = new ScalarObservable(1);
39-
var foo = 42;
40-
var selector = function (x) {
41-
expect(this).toEqual(foo);
42-
return x + '!!!';
43-
};
44-
var r = s.map(selector, 42);
45-
46-
expect(r instanceof ScalarObservable).toBe(true);
47-
expect(r.value).toBe('1!!!');
48-
});
49-
50-
it('should return an ErrorObservable if map errors', function () {
51-
var s = new ScalarObservable(1);
52-
var r = s.map(function (x) { throw 'bad!'; });
53-
expect(r instanceof ErrorObservable).toBe(true);
54-
expect(r.error).toBe('bad!');
55-
});
56-
});
57-
58-
describe('prototype.count()', function () {
59-
it('should map to a new ScalarObservable of 1', function () {
60-
var s = new ScalarObservable(1);
61-
var r = s.count();
62-
expect(r instanceof ScalarObservable).toBe(true);
63-
expect(r.value).toBe(1);
64-
});
65-
66-
it('should map to a new ScalarObservable of 1 if predicate matches', function () {
67-
var s = new ScalarObservable(1);
68-
var r = s.count(function (x) { return x === 1; });
69-
expect(r instanceof ScalarObservable).toBe(true);
70-
expect(r.value).toBe(1);
71-
});
72-
73-
it('should map to a new ScalarObservable of 0 if predicate does not match', function () {
74-
var s = new ScalarObservable(1);
75-
var r = s.count(function (x) { return x === 0; });
76-
expect(r instanceof ScalarObservable).toBe(true);
77-
expect(r.value).toBe(0);
78-
});
79-
80-
it('should map to a new ErrorObservable if predicate errors', function () {
81-
var s = new ScalarObservable(1);
82-
var r = s.count(function () { throw 'bad!'; });
83-
expect(r instanceof ErrorObservable).toBe(true);
84-
expect(r.error).toBe('bad!');
85-
});
86-
});
87-
88-
describe('prototype.filter()', function () {
89-
it('should return itself if the filter matches its value', function () {
90-
var s = new ScalarObservable(1);
91-
var r = s.filter(function (x) { return x === 1; });
92-
expect(s).toBe(r);
93-
});
94-
95-
it('should filter using a custom thisArg', function () {
96-
var s = new ScalarObservable(1);
97-
var filterer = {
98-
filter1: function (x) { return x > 0; },
99-
filter2: function (x) { return x === 1; }
100-
};
101-
102-
var r = s
103-
.filter(function (x) { return this.filter1(x); }, filterer)
104-
.filter(function (x) { return this.filter2(x); }, filterer);
105-
106-
expect(s).toBe(r);
107-
});
108-
109-
it('should return EmptyObservable if filter does not match', function () {
110-
var s = new ScalarObservable(1);
111-
var r = s.filter(function (x) { return x === 0; });
112-
expect(r instanceof EmptyObservable).toBe(true);
113-
});
114-
115-
it('should map to a new ErrorObservable if predicate errors', function () {
116-
var s = new ScalarObservable(1);
117-
var r = s.filter(function () { throw 'bad!'; });
118-
expect(r instanceof ErrorObservable).toBe(true);
119-
expect(r.error).toBe('bad!');
120-
});
121-
});
122-
123-
describe('prototype.take()', function () {
124-
it('should return itself if count > 0', function () {
125-
var s = new ScalarObservable(1);
126-
var r = s.take(1);
127-
expect(s).toBe(r);
128-
});
129-
130-
it('should return EmptyObservable if count === 0', function () {
131-
var s = new ScalarObservable(1);
132-
var r = s.take(0);
133-
expect(r instanceof EmptyObservable).toBe(true);
134-
});
135-
});
136-
137-
describe('prototype.skip()', function () {
138-
it('should return itself if count === 0', function () {
139-
var s = new ScalarObservable(1);
140-
var r = s.skip(0);
141-
expect(s).toBe(r);
142-
});
143-
144-
it('should return EmptyObservable if count > 0', function () {
145-
var s = new ScalarObservable(1);
146-
var r = s.skip(1);
147-
expect(r instanceof EmptyObservable).toBe(true);
148-
});
149-
});
150-
151-
describe('prototype.reduce()', function () {
152-
it('should return a ScalarObservable of the result if there is a seed', function () {
153-
var s = new ScalarObservable(1);
154-
var r = s.reduce(function (a, x) { return a + x; }, 1);
155-
expect(r instanceof ScalarObservable).toBe(true);
156-
expect(r.value).toBe(2);
157-
});
158-
159-
it('should return itself if there is no seed', function () {
160-
var s = new ScalarObservable(1);
161-
var r = s.reduce(function (a, x) { return a + x; });
162-
expect(r).toBe(s);
163-
});
164-
165-
it('should return ErrorObservable if projection throws', function () {
166-
var s = new ScalarObservable(1);
167-
var r = s.reduce(function (a, x) { throw 'error'; }, 1);
168-
var expected = new ErrorObservable('error');
169-
170-
expect(r).toEqual(expected);
171-
});
172-
});
173-
174-
describe('prototype.scan()', function () {
175-
it('should return a ScalarObservable of the result if there is a seed', function () {
176-
var s = new ScalarObservable(1);
177-
var r = s.scan(function (a, x) { return a + x; }, 1);
178-
expect(r instanceof ScalarObservable).toBe(true);
179-
expect(r.value).toBe(2);
180-
});
181-
182-
it('should return itself if there is no seed', function () {
183-
var s = new ScalarObservable(1);
184-
var r = s.scan(function (a, x) { return a + x; });
185-
expect(r).toBe(s);
186-
});
187-
188-
it('should return ErrorObservable if projection throws', function () {
189-
var s = new ScalarObservable(1);
190-
var r = s.scan(function (a, x) { throw 'error'; }, 1);
191-
var expected = new ErrorObservable('error');
192-
193-
expect(r).toEqual(expected);
194-
});
195-
});
196-
});
197-
198-
// If you uncomment this, jasmine breaks? WEIRD
199-
// describe('reality', function () {
200-
// it('should exist in this universe', function () {
201-
// expect(true).toBe(true);
202-
// });
203-
// });
28+
});

src/observable/ScalarObservable.ts

-71
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import {Observable} from '../Observable';
33
import {Subscriber} from '../Subscriber';
44
import {Subscription} from '../Subscription';
55

6-
import {tryCatch} from '../util/tryCatch';
7-
import {errorObject} from '../util/errorObject';
8-
import {ErrorObservable} from './throw';
9-
import {EmptyObservable} from './empty';
10-
116
export class ScalarObservable<T> extends Observable<T> {
127
static create<T>(value: T, scheduler?: Scheduler): ScalarObservable<T> {
138
return new ScalarObservable(value, scheduler);
@@ -52,69 +47,3 @@ export class ScalarObservable<T> extends Observable<T> {
5247
}
5348
}
5449
}
55-
56-
// TypeScript is weird about class prototype member functions and instance properties touching on it's plate.
57-
const proto = ScalarObservable.prototype;
58-
59-
proto.map = function <T, R>(project: (x: T, ix?: number) => R, thisArg?: any): Observable<R> {
60-
let result = tryCatch(project).call(thisArg || this, this.value, 0);
61-
if (result === errorObject) {
62-
return new ErrorObservable(errorObject.e);
63-
} else {
64-
return new ScalarObservable(project.call(thisArg || this, this.value, 0));
65-
}
66-
};
67-
68-
proto.filter = function <T>(select: (x: T, ix?: number) => boolean, thisArg?: any): Observable<T> {
69-
let result = tryCatch(select).call(thisArg || this, this.value, 0);
70-
if (result === errorObject) {
71-
return new ErrorObservable(errorObject.e);
72-
} else if (result) {
73-
return this;
74-
} else {
75-
return new EmptyObservable<T>();
76-
}
77-
};
78-
79-
proto.reduce = function <T, R>(project: (acc: R, x: T) => R, seed?: R): Observable<R> {
80-
if (typeof seed === 'undefined') {
81-
return <any>this;
82-
}
83-
let result = tryCatch(project)(seed, this.value);
84-
if (result === errorObject) {
85-
return new ErrorObservable(errorObject.e);
86-
} else {
87-
return new ScalarObservable(result);
88-
}
89-
};
90-
91-
proto.scan = function <T, R>(project: (acc: R, x: T) => R, acc?: R): Observable<R> {
92-
return this.reduce(project, acc);
93-
};
94-
95-
proto.count = function <T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): Observable<number> {
96-
if (!predicate) {
97-
return new ScalarObservable(1);
98-
} else {
99-
let result = tryCatch(predicate).call(this, this.value, 0, this);
100-
if (result === errorObject) {
101-
return new ErrorObservable(errorObject.e);
102-
} else {
103-
return new ScalarObservable(result ? 1 : 0);
104-
}
105-
}
106-
};
107-
108-
proto.skip = function <T>(count: number): Observable<T> {
109-
if (count > 0) {
110-
return new EmptyObservable<T>();
111-
}
112-
return this;
113-
};
114-
115-
proto.take = function <T>(count: number): Observable<T> {
116-
if (count > 0) {
117-
return this;
118-
}
119-
return new EmptyObservable<T>();
120-
};

0 commit comments

Comments
 (0)