diff --git a/CONTRIBUTION.md b/CONTRIBUTION.md index 59c7e01522..9e59423519 100644 --- a/CONTRIBUTION.md +++ b/CONTRIBUTION.md @@ -216,7 +216,7 @@ Must be one of the following: ### Scope The scope could be anything specifying place of the commit change. For example -`Observable`, `Subject`, `switchLatest`, etc. +`Observable`, `Subject`, `switchMap`, etc. ### Subject The subject contains succinct description of the change: diff --git a/doc/index.md b/doc/index.md index de06968712..19ba9d1e28 100644 --- a/doc/index.md +++ b/doc/index.md @@ -73,8 +73,8 @@ - [startWith](function/index.html#static-function-startWith) - [subscribeOn](function/index.html#static-function-subscribeOn) - [switch](function/index.html#static-function-switch) -- [switchLatest](function/index.html#static-function-switchLatest) -- [switchLatestTo](function/index.html#static-function-switchLatestTo) +- [switchMap](function/index.html#static-function-switchMap) +- [switchMapTo](function/index.html#static-function-switchMapTo) - [take](function/index.html#static-function-take) - [takeUntil](function/index.html#static-function-takeUntil) - [throttle](function/index.html#static-function-throttle) diff --git a/spec/operators/switchLatest-spec.js b/spec/operators/switchMap-spec.js similarity index 86% rename from spec/operators/switchLatest-spec.js rename to spec/operators/switchMap-spec.js index 0502a71684..b848013737 100644 --- a/spec/operators/switchLatest-spec.js +++ b/spec/operators/switchMap-spec.js @@ -3,11 +3,11 @@ var Rx = require('../../dist/cjs/Rx'); var Observable = Rx.Observable; var immediateScheduler = Rx.Scheduler.immediate; -describe('Observable.prototype.switchLatest()', function () { +describe('Observable.prototype.switchMap()', function () { it("should switch with a selector function", function (done) { var a = Observable.of(1, 2, 3); var expected = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'a3', 'b3', 'c3']; - a.switchLatest(function(x) { + a.switchMap(function(x) { return Observable.of('a' + x, 'b' + x, 'c' + x); }).subscribe(function (x) { expect(x).toBe(expected.shift()); @@ -17,7 +17,7 @@ describe('Observable.prototype.switchLatest()', function () { it('should unsub inner observables', function(){ var unsubbed = []; - Observable.of('a', 'b').switchLatest(function(x) { + Observable.of('a', 'b').switchMap(function(x) { return Observable.create(function(subscriber) { subscriber.complete(); return function() { @@ -37,7 +37,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected); }); @@ -50,7 +50,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected); }); @@ -63,7 +63,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected); }); @@ -76,7 +76,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected); }); @@ -89,7 +89,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected); }); @@ -102,7 +102,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected, undefined, new Error('sad')); }); @@ -115,7 +115,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x, y: y }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected, undefined, new Error('sad')); }); @@ -123,7 +123,7 @@ describe('Observable.prototype.switchLatest()', function () { it('should handle outer empty', function (){ var e1 = Observable.empty(); var expected = '|'; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return Observable.of(value); })).toBe(expected); }); @@ -131,7 +131,7 @@ describe('Observable.prototype.switchLatest()', function () { it('should handle outer never', function (){ var e1 = Observable.never(); var expected = '----'; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return Observable.of(value); })).toBe(expected); }); @@ -139,7 +139,7 @@ describe('Observable.prototype.switchLatest()', function () { it('should handle outer throw', function (){ var e1 = Observable.throw(new Error('wah')); var expected = '#'; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return Observable.of(value); })).toBe(expected, undefined, new Error('wah')); }); @@ -151,7 +151,7 @@ describe('Observable.prototype.switchLatest()', function () { var observableLookup = { x: x }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; })).toBe(expected, undefined, new Error('boo-hoo')); }); @@ -174,7 +174,7 @@ describe('Observable.prototype.switchLatest()', function () { i: ['i', 'y', 3, 1] }; - expectObservable(e1.switchLatest(function(value) { + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; }, function(innerValue, outerValue, innerIndex, outerIndex) { return [innerValue, outerValue, innerIndex, outerIndex]; diff --git a/src/Observable.ts b/src/Observable.ts index 469af56864..ad5872983b 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -173,9 +173,9 @@ export default class Observable { delay: (delay: number, scheduler?: Scheduler) => Observable; switch: () => Observable; - switchLatest: (project: ((x: T, ix: number) => Observable), + switchMap: (project: ((x: T, ix: number) => Observable), projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable; - switchLatestTo: (observable: Observable, + switchMapTo: (observable: Observable, projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable; static combineLatest: (...observables: (Observable | ((...values: Array) => T)) []) => Observable; diff --git a/src/Rx.ts b/src/Rx.ts index e118a8a2cf..aba3cf0dca 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -67,8 +67,8 @@ import mergeAll from './operators/mergeAll'; import flatMap from './operators/flatMap'; import flatMapTo from './operators/flatMapTo'; import _switch from './operators/switch'; -import switchLatest from './operators/switchLatest'; -import switchLatestTo from './operators/switchLatestTo'; +import switchMap from './operators/switchMap'; +import switchMapTo from './operators/switchMapTo'; import expand from './operators/expand'; Observable.merge = merge; @@ -77,8 +77,8 @@ observableProto.mergeAll = mergeAll; observableProto.flatMap = flatMap; observableProto.flatMapTo = flatMapTo; observableProto.switch = _switch; -observableProto.switchLatest = switchLatest; -observableProto.switchLatestTo = switchLatestTo; +observableProto.switchMap = switchMap; +observableProto.switchMapTo = switchMapTo; observableProto.expand = expand; import _do from './operators/do'; diff --git a/src/operators/switchLatest.ts b/src/operators/switchMap.ts similarity index 83% rename from src/operators/switchLatest.ts rename to src/operators/switchMap.ts index 7a984eb97c..3960256170 100644 --- a/src/operators/switchLatest.ts +++ b/src/operators/switchMap.ts @@ -7,22 +7,22 @@ import tryCatch from '../util/tryCatch'; import { errorObject } from '../util/errorObject'; import { FlatMapSubscriber } from './flatMap-support'; -export default function switchLatest(project: (value: T, index: number) => Observable, +export default function switchMap(project: (value: T, index: number) => Observable, resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2): Observable{ - return this.lift(new SwitchLatestOperator(project, resultSelector)); + return this.lift(new SwitchMapOperator(project, resultSelector)); } -class SwitchLatestOperator implements Operator { +class SwitchMapOperator implements Operator { constructor(private project: (value: T, index: number) => Observable, private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { } call(subscriber: Subscriber): Subscriber { - return new SwitchLatestSubscriber(subscriber, this.project, this.resultSelector); + return new SwitchMapSubscriber(subscriber, this.project, this.resultSelector); } } -class SwitchLatestSubscriber extends Subscriber { +class SwitchMapSubscriber extends Subscriber { private innerSubscription: Subscription; private hasCompleted = false; @@ -45,7 +45,7 @@ class SwitchLatestSubscriber extends Subscriber { if(innerSubscription) { innerSubscription.unsubscribe(); } - this.add(this.innerSubscription = result.subscribe(new InnerSwitchLatestSubscriber(this, this.resultSelector, index, value))) + this.add(this.innerSubscription = result.subscribe(new InnerSwitchMapSubscriber(this, this.resultSelector, index, value))) } } @@ -74,10 +74,10 @@ class SwitchLatestSubscriber extends Subscriber { } } -class InnerSwitchLatestSubscriber extends Subscriber { +class InnerSwitchMapSubscriber extends Subscriber { private index: number = 0; - constructor(private parent: SwitchLatestSubscriber, + constructor(private parent: SwitchMapSubscriber, private resultSelector: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, private outerIndex: number, private outerValue: any) { diff --git a/src/operators/switchLatestTo.ts b/src/operators/switchMapTo.ts similarity index 70% rename from src/operators/switchLatestTo.ts rename to src/operators/switchMapTo.ts index d831b9f8fa..b049966b1e 100644 --- a/src/operators/switchLatestTo.ts +++ b/src/operators/switchMapTo.ts @@ -6,22 +6,22 @@ import Subscription from '../Subscription'; import { FlatMapToSubscriber } from './flatMapTo-support'; -export default function switchLatestTo(observable: Observable, +export default function switchMapTo(observable: Observable, projectResult?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2): Observable { - return this.lift(new SwitchLatestToOperator(observable, projectResult)); + return this.lift(new SwitchMapToOperator(observable, projectResult)); } -class SwitchLatestToOperator implements Operator { +class SwitchMapToOperator implements Operator { constructor(private observable: Observable, private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { } call(subscriber: Subscriber): Subscriber { - return new SwitchLatestToSubscriber(subscriber, this.observable, this.resultSelector); + return new SwitchMapToSubscriber(subscriber, this.observable, this.resultSelector); } } -class SwitchLatestToSubscriber extends FlatMapToSubscriber { +class SwitchMapToSubscriber extends FlatMapToSubscriber { innerSubscription: Subscription;