Skip to content

Commit

Permalink
feat(mergeMap|concatMap|concatMapTo): simplified the signatures
Browse files Browse the repository at this point in the history
BREAKING CHANGE: mergeMap, concatMap and concatMapTo no longer support a result selector, if you need to use a result selector, use the following pattern: `source.mergeMap(x => of(x + x).pipe(map(y => y + x))` (the pattern would be the same for `concatMap`).
  • Loading branch information
benlesh committed Mar 2, 2018
1 parent 52cdfe8 commit d293245
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 545 deletions.
104 changes: 4 additions & 100 deletions spec/operators/concatMap-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ describe('Observable.prototype.concatMap', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

<<<<<<< HEAD
it('should concatMap many outer to inner arrays, using resultSelector', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ !';
Expand Down Expand Up @@ -573,6 +574,8 @@ describe('Observable.prototype.concatMap', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

=======
>>>>>>> feat(mergeMap|concatMap|concatMapTo): simplified the signatures
it('should mergeMap many outer to inner arrays, outer unsubscribed early', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
Expand All @@ -585,19 +588,6 @@ describe('Observable.prototype.concatMap', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should concatMap many outer to inner arrays, resultSelector, outer unsubscribed', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
const unsub = ' ! ';
const expected = '(44)--(8888)-- ';

const result = e1.concatMap((value) => arrayRepeat(value, +value),
(x, y) => String(parseInt(x) + parseInt(y)));

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should concatMap many outer to inner arrays, project throws', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
Expand All @@ -615,45 +605,7 @@ describe('Observable.prototype.concatMap', () => {
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should concatMap many outer to inner arrays, resultSelector throws', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
const expected = '(44)--(8888)---# ';

const result = e1.concatMap((value) => arrayRepeat(value, +value),
(inner, outer) => {
if (outer === '3') {
throw 'error';
}
return String(parseInt(outer) + parseInt(inner));
});

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should concatMap many outer to inner arrays, resultSelector, project throws', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
const expected = '(44)--(8888)---# ';

let invoked = 0;
const result = e1.concatMap((value) => {
invoked++;
if (invoked === 3) {
throw 'error';
}
return arrayRepeat(value, +value);
}, (inner, outer) => {
return String(parseInt(outer) + parseInt(inner));
});

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should map values to constant resolved promises and concatenate', (done) => {
it('should map values to constant resolved promises and concatenate', (done: MochaDone) => {
const source = Rx.Observable.from([4, 3, 2, 1]);
const project = (value: number) => Observable.from(Promise.resolve(42));

Expand Down Expand Up @@ -714,52 +666,4 @@ describe('Observable.prototype.concatMap', () => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});

it('should concatMap values to resolved promises with resultSelector', (done) => {
const source = Rx.Observable.from([4, 3, 2, 1]);
const resultSelectorCalledWith: number[][] = [];
const project = (value: number, index: number) => Observable.from((Promise.resolve([value, index])));

const resultSelector = function (outerVal: any, innerVal: any, outerIndex: any, innerIndex: any): number {
resultSelectorCalledWith.push([].slice.call(arguments));
return 8;
};

const results: number[] = [];
const expectedCalls = [
[4, [4, 0], 0, 0],
[3, [3, 1], 1, 0],
[2, [2, 2], 2, 0],
[1, [1, 3], 3, 0]
];
source.concatMap(project, resultSelector).subscribe(
(x) => {
results.push(x);
}, (err) => {
done(new Error('Subscriber error handler not supposed to be called.'));
}, () => {
expect(results).to.deep.equal([8, 8, 8, 8]);
expect(resultSelectorCalledWith).to.deep.equal(expectedCalls);
done();
});
});

it('should concatMap values to rejected promises with resultSelector', (done) => {
const source = Rx.Observable.from([4, 3, 2, 1]);
const project = (value: number, index: number) => Observable.from(Promise.reject('' + value + '-' + index));

const resultSelector = () => {
throw 'this should not be called';
};

source.concatMap(project, resultSelector).subscribe(
(x) => {
done(new Error('Subscriber next handler not supposed to be called.'));
}, (err) => {
expect(err).to.deep.equal('4-0');
done();
}, () => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});
});
97 changes: 1 addition & 96 deletions spec/operators/concatMapTo-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,6 @@ describe('Observable.prototype.concatMapTo', () => {
expectObservable(result).toBe(expected);
});

it('should concatMapTo many outer to inner arrays, using resultSelector', () => {
const e1 = hot('2-----4--------3--------2-------|');
const expected = '(2345)(4567)---(3456)---(2345)--|';

const result = e1.concatMapTo(['0', '1', '2', '3'],
(x, y) => String(parseInt(x) + parseInt(y)));

expectObservable(result).toBe(expected);
});

it('should concatMapTo many outer to inner arrays, and outer throws', () => {
const e1 = hot('2-----4--------3--------2-------#');
const expected = '(0123)(0123)---(0123)---(0123)--#';
Expand All @@ -271,16 +261,6 @@ describe('Observable.prototype.concatMapTo', () => {
expectObservable(result).toBe(expected);
});

it('should concatMapTo many outer to inner arrays, resultSelector, outer throws', () => {
const e1 = hot('2-----4--------3--------2-------#');
const expected = '(2345)(4567)---(3456)---(2345)--#';

const result = e1.concatMapTo(['0', '1', '2', '3'],
(x, y) => String(parseInt(x) + parseInt(y)));

expectObservable(result).toBe(expected);
});

it('should mergeMap many outer to inner arrays, outer unsubscribed early', () => {
const e1 = hot('2-----4--------3--------2-------|');
const unsub = ' !';
Expand All @@ -291,32 +271,7 @@ describe('Observable.prototype.concatMapTo', () => {
expectObservable(result, unsub).toBe(expected);
});

it('should concatMapTo many outer to inner arrays, resultSelector, outer unsubscribed', () => {
const e1 = hot('2-----4--------3--------2-------|');
const unsub = ' !';
const expected = '(2345)(4567)--';

const result = e1.concatMapTo(['0', '1', '2', '3'],
(x, y) => String(parseInt(x) + parseInt(y)));

expectObservable(result, unsub).toBe(expected);
});

it('should concatMapTo many outer to inner arrays, resultSelector throws', () => {
const e1 = hot('2-----4--------3--------2-------|');
const expected = '(2345)(4567)---#';

const result = e1.concatMapTo(['0', '1', '2', '3'], (x, y) => {
if (x === '3') {
throw 'error';
}
return String(parseInt(x) + parseInt(y));
});

expectObservable(result).toBe(expected);
});

it('should map values to constant resolved promises and concatenate', (done) => {
it('should map values to constant resolved promises and concatenate', (done: MochaDone) => {
const source = Rx.Observable.from([4, 3, 2, 1]);

const results: number[] = [];
Expand Down Expand Up @@ -348,54 +303,4 @@ describe('Observable.prototype.concatMapTo', () => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});

it('should concatMapTo values to resolved promises with resultSelector', (done) => {
const source = Rx.Observable.from([4, 3, 2, 1]);
const resultSelectorCalledWith: number[][] = [];
const inner = Observable.from(Promise.resolve(42));
const resultSelector = function (outerVal: number, innerVal: number, outerIndex: number, innerIndex: number) {
resultSelectorCalledWith.push([].slice.call(arguments));
return 8;
};

const results: number[] = [];
const expectedCalls = [
[4, 42, 0, 0],
[3, 42, 1, 0],
[2, 42, 2, 0],
[1, 42, 3, 0]
];
source.concatMapTo(inner, resultSelector).subscribe(
(x) => {
results.push(x);
},
(err) => {
done(new Error('Subscriber error handler not supposed to be called.'));
},
() => {
expect(results).to.deep.equal([8, 8, 8, 8]);
expect(resultSelectorCalledWith).to.deep.equal(expectedCalls);
done();
});
});

it('should concatMapTo values to rejected promises with resultSelector', (done) => {
const source = Rx.Observable.from([4, 3, 2, 1]);
const inner = Observable.from(Promise.reject(42));
const resultSelector = () => {
throw 'this should not be called';
};

source.concatMapTo(inner, resultSelector).subscribe(
(x) => {
done(new Error('Subscriber next handler not supposed to be called.'));
},
(err) => {
expect(err).to.equal(42);
done();
},
() => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});
});
Loading

0 comments on commit d293245

Please sign in to comment.