Skip to content

Commit fc1724d

Browse files
Andre Medeirosbenlesh
authored andcommitted
fix(operators): reorder signature of resultSelectors
This commit swaps around the arguments of resultSelector functions of operators that internally create an Observable<Observable<T>>, such as switchMap, mergeMap, and concatMap. BREAKING CHANGES: The function signature of resultSelectors used to be (innerValue, outerValue, innerIndex, outerIndex) but this commits changes it to be (outerValue, innerValue, outerIndex, innerIndex), to match signatures in RxJS 4.
1 parent b7b02d7 commit fc1724d

15 files changed

+170
-172
lines changed

spec/operators/switchMap-spec.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ describe('Observable.prototype.switchMap()', function () {
1313
expect(x).toBe(expected.shift());
1414
}, null, done);
1515
});
16-
16+
1717
it('should unsub inner observables', function(){
1818
var unsubbed = [];
19-
19+
2020
Observable.of('a', 'b').switchMap(function(x) {
2121
return Observable.create(function(subscriber) {
2222
subscriber.complete();
@@ -25,159 +25,159 @@ describe('Observable.prototype.switchMap()', function () {
2525
};
2626
});
2727
}).subscribe();
28-
28+
2929
expect(unsubbed).toEqual(['a', 'b']);
3030
});
3131

3232
it('should switch inner cold observables', function (){
33-
var x = cold( '--a--b--c--d--e--|')
33+
var x = cold( '--a--b--c--d--e--|');
3434
var y = cold( '---f---g---h---i--|');
3535
var e1 = hot('---------x---------y---------|');
3636
var expected = '-----------a--b--c----f---g---h---i--|';
37-
37+
3838
var observableLookup = { x: x, y: y };
39-
39+
4040
expectObservable(e1.switchMap(function(value) {
4141
return observableLookup[value];
42-
})).toBe(expected);
42+
})).toBe(expected);
4343
});
44-
44+
4545
it('should switch inner hot observables', function (){
46-
var x = hot('-----a--b--c--d--e--|')
46+
var x = hot('-----a--b--c--d--e--|');
4747
var y = hot('--p-o-o-p-------------f---g---h---i--|');
4848
var e1 = hot('---------x---------y---------|');
4949
var expected = '-----------c--d--e----f---g---h---i--|';
50-
50+
5151
var observableLookup = { x: x, y: y };
52-
52+
5353
expectObservable(e1.switchMap(function(value) {
5454
return observableLookup[value];
55-
})).toBe(expected);
55+
})).toBe(expected);
5656
});
57-
57+
5858
it('should switch inner empty and empty', function () {
5959
var x = Observable.empty();
6060
var y = Observable.empty();
6161
var e1 = hot('---------x---------y---------|');
6262
var expected = '-----------------------------|';
63-
63+
6464
var observableLookup = { x: x, y: y };
65-
65+
6666
expectObservable(e1.switchMap(function(value) {
6767
return observableLookup[value];
68-
})).toBe(expected);
68+
})).toBe(expected);
6969
});
70-
70+
7171
it('should switch inner empty and never', function() {
72-
var x = Observable.empty()
72+
var x = Observable.empty();
7373
var y = Observable.never();
7474
var e1 = hot('---------x---------y---------|');
7575
var expected = '----------------------------------';
76-
76+
7777
var observableLookup = { x: x, y: y };
78-
78+
7979
expectObservable(e1.switchMap(function(value) {
8080
return observableLookup[value];
81-
})).toBe(expected);
81+
})).toBe(expected);
8282
});
83-
83+
8484
it('should switch inner never and empty', function (){
8585
var x = Observable.never();
8686
var y = Observable.empty();
8787
var e1 = hot('---------x---------y---------|');
8888
var expected = '-----------------------------|';
89-
89+
9090
var observableLookup = { x: x, y: y };
91-
91+
9292
expectObservable(e1.switchMap(function(value) {
9393
return observableLookup[value];
94-
})).toBe(expected);
94+
})).toBe(expected);
9595
});
96-
96+
9797
it('should switch inner never and throw', function (){
9898
var x = Observable.never();
9999
var y = Observable.throw(new Error('sad'));
100100
var e1 = hot('---------x---------y---------|');
101101
var expected = '-------------------#';
102-
102+
103103
var observableLookup = { x: x, y: y };
104-
104+
105105
expectObservable(e1.switchMap(function(value) {
106106
return observableLookup[value];
107-
})).toBe(expected, undefined, new Error('sad'));
107+
})).toBe(expected, undefined, new Error('sad'));
108108
});
109-
109+
110110
it('should switch inner empty and throw', function (){
111111
var x = Observable.empty();
112112
var y = Observable.throw(new Error('sad'));
113113
var e1 = hot('---------x---------y---------|');
114114
var expected = '-------------------#';
115-
115+
116116
var observableLookup = { x: x, y: y };
117-
117+
118118
expectObservable(e1.switchMap(function(value) {
119119
return observableLookup[value];
120-
})).toBe(expected, undefined, new Error('sad'));
120+
})).toBe(expected, undefined, new Error('sad'));
121121
});
122-
122+
123123
it('should handle outer empty', function (){
124124
var e1 = Observable.empty();
125125
var expected = '|';
126126
expectObservable(e1.switchMap(function(value) {
127127
return Observable.of(value);
128128
})).toBe(expected);
129129
});
130-
130+
131131
it('should handle outer never', function (){
132132
var e1 = Observable.never();
133133
var expected = '----';
134134
expectObservable(e1.switchMap(function(value) {
135135
return Observable.of(value);
136136
})).toBe(expected);
137137
});
138-
138+
139139
it('should handle outer throw', function (){
140140
var e1 = Observable.throw(new Error('wah'));
141141
var expected = '#';
142142
expectObservable(e1.switchMap(function(value) {
143143
return Observable.of(value);
144144
})).toBe(expected, undefined, new Error('wah'));
145145
});
146-
146+
147147
it('should handle outer error', function (){
148-
var x = cold( '--a--b--c--d--e--|')
148+
var x = cold( '--a--b--c--d--e--|');
149149
var e1 = hot('---------x---------#', undefined, new Error('boo-hoo'));
150150
var expected = '-----------a--b--c-#';
151-
151+
152152
var observableLookup = { x: x };
153-
153+
154154
expectObservable(e1.switchMap(function(value) {
155155
return observableLookup[value];
156-
})).toBe(expected, undefined, new Error('boo-hoo'));
156+
})).toBe(expected, undefined, new Error('boo-hoo'));
157157
});
158-
158+
159159
it('should switch with resultSelector goodness', function (){
160-
var x = cold( '--a--b--c--d--e--|')
160+
var x = cold( '--a--b--c--d--e--|');
161161
var y = cold( '---f---g---h---i--|');
162162
var e1 = hot('---------x---------y---------|');
163163
var expected = '-----------a--b--c----f---g---h---i--|';
164-
164+
165165
var observableLookup = { x: x, y: y };
166-
166+
167167
var expectedValues = {
168-
a: ['a', 'x', 0, 0],
169-
b: ['b', 'x', 1, 0],
170-
c: ['c', 'x', 2, 0],
171-
f: ['f', 'y', 0, 1],
172-
g: ['g', 'y', 1, 1],
173-
h: ['h', 'y', 2, 1],
174-
i: ['i', 'y', 3, 1]
168+
a: ['x', 'a', 0, 0],
169+
b: ['x', 'b', 0, 1],
170+
c: ['x', 'c', 0, 2],
171+
f: ['y', 'f', 1, 0],
172+
g: ['y', 'g', 1, 1],
173+
h: ['y', 'h', 1, 2],
174+
i: ['y', 'i', 1, 3]
175175
};
176-
176+
177177
expectObservable(e1.switchMap(function(value) {
178178
return observableLookup[value];
179179
}, function(innerValue, outerValue, innerIndex, outerIndex) {
180180
return [innerValue, outerValue, innerIndex, outerIndex];
181-
})).toBe(expected, expectedValues);
181+
})).toBe(expected, expectedValues);
182182
});
183183
});

src/InnerSubscriber.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import { errorObject } from './util/errorObject';
66

77
export default class InnerSubscriber<T, R> extends Subscriber<R> {
88
index: number = 0;
9-
9+
1010
constructor(private parent: OuterSubscriber<T, R>, private outerValue: T, private outerIndex: number) {
1111
super();
1212
}
13-
13+
1414
_next(value: R) {
1515
const index = this.index++;
16-
this.parent.notifyNext(value, this.outerValue, index, this.outerIndex);
16+
this.parent.notifyNext(this.outerValue, value, this.outerIndex, index);
1717
}
18-
18+
1919
_error(error: any) {
2020
this.parent.notifyError(error, this);
2121
}
22-
22+
2323
_complete() {
2424
this.parent.notifyComplete(this);
2525
}

src/OuterSubscriber.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export default class OuterSubscriber<T, R> extends Subscriber<T> {
66
notifyComplete(inner?: InnerSubscriber<T, R>) {
77
this.destination.complete();
88
}
9-
10-
notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) {
9+
10+
notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) {
1111
this.destination.next(innerValue);
1212
}
13-
13+
1414
notifyError(error?: any, inner?: InnerSubscriber<T, R>) {
1515
this.destination.error(error);
1616
}

src/operators/combineLatest-support.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
2929
private values: any[] = [];
3030
private observables: any[] = [];
3131
private toRespond: number[] = [];
32-
32+
3333
constructor(destination: Subscriber<R>, private project?: (...values: Array<any>) => R) {
3434
super(destination);
3535
}
@@ -39,7 +39,7 @@ export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
3939
toRespond.push(toRespond.length);
4040
this.observables.push(observable);
4141
}
42-
42+
4343
_complete() {
4444
const observables = this.observables;
4545
const len = observables.length;
@@ -59,23 +59,23 @@ export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
5959
this.destination.complete();
6060
}
6161
}
62-
63-
notifyNext(value: R, observable: any, innerIndex: number, outerIndex: number) {
62+
63+
notifyNext(observable: any, value: R, outerIndex: number, innerIndex: number) {
6464
const values = this.values;
6565
values[outerIndex] = value;
6666
const toRespond = this.toRespond;
67-
67+
6868
if(toRespond.length > 0) {
6969
const found = toRespond.indexOf(outerIndex);
7070
if(found !== -1) {
7171
toRespond.splice(found, 1);
7272
}
7373
}
74-
74+
7575
if(toRespond.length === 0) {
7676
const project = this.project;
7777
const destination = this.destination;
78-
78+
7979
if(project) {
8080
let result = tryCatch(project).apply(this, values);
8181
if(result === errorObject) {

src/operators/concatMapTo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import Observable from '../Observable';
22
import { MergeMapToOperator } from './mergeMapTo-support';
33

44
export default function concatMapTo<T, R, R2>(observable: Observable<R>,
5-
projectResult?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) : Observable<R2> {
5+
projectResult?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) : Observable<R2> {
66
return this.lift(new MergeMapToOperator(observable, projectResult, 1));
77
}

src/operators/expand-support.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import OuterSubscriber from '../OuterSubscriber';
1313
import subscribeToResult from '../util/subscribeToResult';
1414

1515
export class ExpandOperator<T, R> implements Operator<T, R> {
16-
constructor(private project: (value: T, index: number) => Observable<any>,
16+
constructor(private project: (value: T, index: number) => Observable<any>,
1717
private concurrent: number = Number.POSITIVE_INFINITY) {
1818
}
1919

@@ -27,15 +27,15 @@ export class ExpandSubscriber<T, R> extends OuterSubscriber<T, R> {
2727
private active: number = 0;
2828
private hasCompleted: boolean = false;
2929
private buffer: any[];
30-
31-
constructor(destination: Observer<T>, private project: (value: T, index: number) => Observable<R>,
30+
31+
constructor(destination: Observer<T>, private project: (value: T, index: number) => Observable<R>,
3232
private concurrent: number = Number.POSITIVE_INFINITY) {
3333
super(destination);
3434
if(concurrent < Number.POSITIVE_INFINITY) {
3535
this.buffer = [];
3636
}
3737
}
38-
38+
3939
_next(value: any) {
4040
const index = this.index++;
4141
this.destination.next(value);
@@ -55,14 +55,14 @@ export class ExpandSubscriber<T, R> extends OuterSubscriber<T, R> {
5555
this.buffer.push(value);
5656
}
5757
}
58-
58+
5959
_complete() {
6060
this.hasCompleted = true;
6161
if(this.hasCompleted && this.active === 0) {
6262
this.destination.complete();
6363
}
6464
}
65-
65+
6666
notifyComplete(innerSub: Subscription<T>) {
6767
const buffer = this.buffer;
6868
this.remove(innerSub);
@@ -74,8 +74,8 @@ export class ExpandSubscriber<T, R> extends OuterSubscriber<T, R> {
7474
this.destination.complete();
7575
}
7676
}
77-
78-
notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) {
77+
78+
notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) {
7979
this._next(innerValue);
8080
}
8181
}

0 commit comments

Comments
 (0)