From fc1724d220245ae2035afb1e5875fcb7e18be2ff Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 30 Sep 2015 17:18:37 +0300 Subject: [PATCH] fix(operators): reorder signature of resultSelectors This commit swaps around the arguments of resultSelector functions of operators that internally create an Observable>, 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. --- spec/operators/switchMap-spec.js | 108 ++++++++++++------------- src/InnerSubscriber.ts | 10 +-- src/OuterSubscriber.ts | 6 +- src/operators/combineLatest-support.ts | 14 ++-- src/operators/concatMapTo.ts | 2 +- src/operators/expand-support.ts | 16 ++-- src/operators/mergeMap-support.ts | 10 +-- src/operators/mergeMap.ts | 4 +- src/operators/mergeMapTo-support.ts | 37 ++++----- src/operators/mergeMapTo.ts | 4 +- src/operators/switch.ts | 18 ++--- src/operators/switchMap.ts | 26 +++--- src/operators/switchMapTo.ts | 7 +- src/operators/withLatestFrom.ts | 20 ++--- src/operators/zip-support.ts | 60 +++++++------- 15 files changed, 170 insertions(+), 172 deletions(-) diff --git a/spec/operators/switchMap-spec.js b/spec/operators/switchMap-spec.js index 9d91e12f36..d63ec0f6a7 100644 --- a/spec/operators/switchMap-spec.js +++ b/spec/operators/switchMap-spec.js @@ -13,10 +13,10 @@ describe('Observable.prototype.switchMap()', function () { expect(x).toBe(expected.shift()); }, null, done); }); - + it('should unsub inner observables', function(){ var unsubbed = []; - + Observable.of('a', 'b').switchMap(function(x) { return Observable.create(function(subscriber) { subscriber.complete(); @@ -25,101 +25,101 @@ describe('Observable.prototype.switchMap()', function () { }; }); }).subscribe(); - + expect(unsubbed).toEqual(['a', 'b']); }); it('should switch inner cold observables', function (){ - var x = cold( '--a--b--c--d--e--|') + var x = cold( '--a--b--c--d--e--|'); var y = cold( '---f---g---h---i--|'); var e1 = hot('---------x---------y---------|'); var expected = '-----------a--b--c----f---g---h---i--|'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected); + })).toBe(expected); }); - + it('should switch inner hot observables', function (){ - var x = hot('-----a--b--c--d--e--|') + var x = hot('-----a--b--c--d--e--|'); var y = hot('--p-o-o-p-------------f---g---h---i--|'); var e1 = hot('---------x---------y---------|'); var expected = '-----------c--d--e----f---g---h---i--|'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected); + })).toBe(expected); }); - + it('should switch inner empty and empty', function () { var x = Observable.empty(); var y = Observable.empty(); var e1 = hot('---------x---------y---------|'); var expected = '-----------------------------|'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected); + })).toBe(expected); }); - + it('should switch inner empty and never', function() { - var x = Observable.empty() + var x = Observable.empty(); var y = Observable.never(); var e1 = hot('---------x---------y---------|'); var expected = '----------------------------------'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected); + })).toBe(expected); }); - + it('should switch inner never and empty', function (){ var x = Observable.never(); var y = Observable.empty(); var e1 = hot('---------x---------y---------|'); var expected = '-----------------------------|'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected); + })).toBe(expected); }); - + it('should switch inner never and throw', function (){ var x = Observable.never(); var y = Observable.throw(new Error('sad')); var e1 = hot('---------x---------y---------|'); var expected = '-------------------#'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected, undefined, new Error('sad')); + })).toBe(expected, undefined, new Error('sad')); }); - + it('should switch inner empty and throw', function (){ var x = Observable.empty(); var y = Observable.throw(new Error('sad')); var e1 = hot('---------x---------y---------|'); var expected = '-------------------#'; - + var observableLookup = { x: x, y: y }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected, undefined, new Error('sad')); + })).toBe(expected, undefined, new Error('sad')); }); - + it('should handle outer empty', function (){ var e1 = Observable.empty(); var expected = '|'; @@ -127,7 +127,7 @@ describe('Observable.prototype.switchMap()', function () { return Observable.of(value); })).toBe(expected); }); - + it('should handle outer never', function (){ var e1 = Observable.never(); var expected = '----'; @@ -135,7 +135,7 @@ describe('Observable.prototype.switchMap()', function () { return Observable.of(value); })).toBe(expected); }); - + it('should handle outer throw', function (){ var e1 = Observable.throw(new Error('wah')); var expected = '#'; @@ -143,41 +143,41 @@ describe('Observable.prototype.switchMap()', function () { return Observable.of(value); })).toBe(expected, undefined, new Error('wah')); }); - + it('should handle outer error', function (){ - var x = cold( '--a--b--c--d--e--|') + var x = cold( '--a--b--c--d--e--|'); var e1 = hot('---------x---------#', undefined, new Error('boo-hoo')); var expected = '-----------a--b--c-#'; - + var observableLookup = { x: x }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; - })).toBe(expected, undefined, new Error('boo-hoo')); + })).toBe(expected, undefined, new Error('boo-hoo')); }); - + it('should switch with resultSelector goodness', function (){ - var x = cold( '--a--b--c--d--e--|') + var x = cold( '--a--b--c--d--e--|'); var y = cold( '---f---g---h---i--|'); var e1 = hot('---------x---------y---------|'); var expected = '-----------a--b--c----f---g---h---i--|'; - + var observableLookup = { x: x, y: y }; - + var expectedValues = { - a: ['a', 'x', 0, 0], - b: ['b', 'x', 1, 0], - c: ['c', 'x', 2, 0], - f: ['f', 'y', 0, 1], - g: ['g', 'y', 1, 1], - h: ['h', 'y', 2, 1], - i: ['i', 'y', 3, 1] + a: ['x', 'a', 0, 0], + b: ['x', 'b', 0, 1], + c: ['x', 'c', 0, 2], + f: ['y', 'f', 1, 0], + g: ['y', 'g', 1, 1], + h: ['y', 'h', 1, 2], + i: ['y', 'i', 1, 3] }; - + expectObservable(e1.switchMap(function(value) { return observableLookup[value]; }, function(innerValue, outerValue, innerIndex, outerIndex) { return [innerValue, outerValue, innerIndex, outerIndex]; - })).toBe(expected, expectedValues); + })).toBe(expected, expectedValues); }); }); \ No newline at end of file diff --git a/src/InnerSubscriber.ts b/src/InnerSubscriber.ts index 93cbc50b85..875bfe255d 100644 --- a/src/InnerSubscriber.ts +++ b/src/InnerSubscriber.ts @@ -6,20 +6,20 @@ import { errorObject } from './util/errorObject'; export default class InnerSubscriber extends Subscriber { index: number = 0; - + constructor(private parent: OuterSubscriber, private outerValue: T, private outerIndex: number) { super(); } - + _next(value: R) { const index = this.index++; - this.parent.notifyNext(value, this.outerValue, index, this.outerIndex); + this.parent.notifyNext(this.outerValue, value, this.outerIndex, index); } - + _error(error: any) { this.parent.notifyError(error, this); } - + _complete() { this.parent.notifyComplete(this); } diff --git a/src/OuterSubscriber.ts b/src/OuterSubscriber.ts index ae6516927e..f024afa8cc 100644 --- a/src/OuterSubscriber.ts +++ b/src/OuterSubscriber.ts @@ -6,11 +6,11 @@ export default class OuterSubscriber extends Subscriber { notifyComplete(inner?: InnerSubscriber) { this.destination.complete(); } - - notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) { + + notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) { this.destination.next(innerValue); } - + notifyError(error?: any, inner?: InnerSubscriber) { this.destination.error(error); } diff --git a/src/operators/combineLatest-support.ts b/src/operators/combineLatest-support.ts index 7bea1ec941..372f567a92 100644 --- a/src/operators/combineLatest-support.ts +++ b/src/operators/combineLatest-support.ts @@ -29,7 +29,7 @@ export class CombineLatestSubscriber extends OuterSubscriber { private values: any[] = []; private observables: any[] = []; private toRespond: number[] = []; - + constructor(destination: Subscriber, private project?: (...values: Array) => R) { super(destination); } @@ -39,7 +39,7 @@ export class CombineLatestSubscriber extends OuterSubscriber { toRespond.push(toRespond.length); this.observables.push(observable); } - + _complete() { const observables = this.observables; const len = observables.length; @@ -59,23 +59,23 @@ export class CombineLatestSubscriber extends OuterSubscriber { this.destination.complete(); } } - - notifyNext(value: R, observable: any, innerIndex: number, outerIndex: number) { + + notifyNext(observable: any, value: R, outerIndex: number, innerIndex: number) { const values = this.values; values[outerIndex] = value; const toRespond = this.toRespond; - + if(toRespond.length > 0) { const found = toRespond.indexOf(outerIndex); if(found !== -1) { toRespond.splice(found, 1); } } - + if(toRespond.length === 0) { const project = this.project; const destination = this.destination; - + if(project) { let result = tryCatch(project).apply(this, values); if(result === errorObject) { diff --git a/src/operators/concatMapTo.ts b/src/operators/concatMapTo.ts index 91db9e425f..1a57695670 100644 --- a/src/operators/concatMapTo.ts +++ b/src/operators/concatMapTo.ts @@ -2,6 +2,6 @@ import Observable from '../Observable'; import { MergeMapToOperator } from './mergeMapTo-support'; export default function concatMapTo(observable: Observable, - projectResult?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) : Observable { + projectResult?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) : Observable { return this.lift(new MergeMapToOperator(observable, projectResult, 1)); } \ No newline at end of file diff --git a/src/operators/expand-support.ts b/src/operators/expand-support.ts index c1db02648e..83566d93f5 100644 --- a/src/operators/expand-support.ts +++ b/src/operators/expand-support.ts @@ -13,7 +13,7 @@ import OuterSubscriber from '../OuterSubscriber'; import subscribeToResult from '../util/subscribeToResult'; export class ExpandOperator implements Operator { - constructor(private project: (value: T, index: number) => Observable, + constructor(private project: (value: T, index: number) => Observable, private concurrent: number = Number.POSITIVE_INFINITY) { } @@ -27,15 +27,15 @@ export class ExpandSubscriber extends OuterSubscriber { private active: number = 0; private hasCompleted: boolean = false; private buffer: any[]; - - constructor(destination: Observer, private project: (value: T, index: number) => Observable, + + constructor(destination: Observer, private project: (value: T, index: number) => Observable, private concurrent: number = Number.POSITIVE_INFINITY) { super(destination); if(concurrent < Number.POSITIVE_INFINITY) { this.buffer = []; } } - + _next(value: any) { const index = this.index++; this.destination.next(value); @@ -55,14 +55,14 @@ export class ExpandSubscriber extends OuterSubscriber { this.buffer.push(value); } } - + _complete() { this.hasCompleted = true; if(this.hasCompleted && this.active === 0) { this.destination.complete(); } } - + notifyComplete(innerSub: Subscription) { const buffer = this.buffer; this.remove(innerSub); @@ -74,8 +74,8 @@ export class ExpandSubscriber extends OuterSubscriber { this.destination.complete(); } } - - notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) { + + notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) { this._next(innerValue); } } diff --git a/src/operators/mergeMap-support.ts b/src/operators/mergeMap-support.ts index 7f72be8736..34489e494c 100644 --- a/src/operators/mergeMap-support.ts +++ b/src/operators/mergeMap-support.ts @@ -11,7 +11,7 @@ import InnerSubscriber from '../InnerSubscriber'; export class MergeMapOperator implements Operator { constructor(private project: (value: T, index: number) => Observable, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, private concurrent: number = Number.POSITIVE_INFINITY) { } @@ -30,7 +30,7 @@ export class MergeMapSubscriber extends OuterSubscriber { constructor(destination: Observer, private project: (value: T, index: number) => Observable, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, private concurrent: number = Number.POSITIVE_INFINITY) { super(destination); } @@ -63,12 +63,10 @@ export class MergeMapSubscriber extends OuterSubscriber { } } - notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) { + notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) { const { destination, resultSelector } = this; if (resultSelector) { - const result = tryCatch(resultSelector)( - innerValue, outerValue, innerIndex, outerIndex - ); + const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex); if (result === errorObject) { destination.error(errorObject.e); } else { diff --git a/src/operators/mergeMap.ts b/src/operators/mergeMap.ts index 6ca2316c79..60f9e95311 100644 --- a/src/operators/mergeMap.ts +++ b/src/operators/mergeMap.ts @@ -2,7 +2,7 @@ import Observable from '../Observable'; import { MergeMapOperator } from './mergeMap-support'; export default function mergeMap(project: (value: T, index: number) => Observable, - resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R, - concurrent: number = Number.POSITIVE_INFINITY) { + resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R, + concurrent: number = Number.POSITIVE_INFINITY) { return this.lift(new MergeMapOperator(project, resultSelector, concurrent)); } \ No newline at end of file diff --git a/src/operators/mergeMapTo-support.ts b/src/operators/mergeMapTo-support.ts index 9a03c339c1..b9f05578ab 100644 --- a/src/operators/mergeMapTo-support.ts +++ b/src/operators/mergeMapTo-support.ts @@ -10,11 +10,10 @@ import InnerSubscriber from '../InnerSubscriber'; export class MergeMapToOperator implements Operator { constructor(private ish: any, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, - private concurrent: number = Number.POSITIVE_INFINITY) { - + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, + private concurrent: number = Number.POSITIVE_INFINITY) { } - + call(observer: Subscriber): Subscriber { return new MergeMapToSubscriber(observer, this.ish, this.resultSelector, this.concurrent); } @@ -25,14 +24,14 @@ export class MergeMapToSubscriber extends OuterSubscriber { private buffer: Observable[] = []; private active: number = 0; protected index: number = 0; - - constructor(destination: Observer, - private ish: any, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, - private concurrent: number = Number.POSITIVE_INFINITY) { + + constructor(destination: Observer, + private ish: any, + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, + private concurrent: number = Number.POSITIVE_INFINITY) { super(destination); } - + _next(value: any) { if(this.active < this.concurrent) { const resultSelector = this.resultSelector; @@ -49,22 +48,24 @@ export class MergeMapToSubscriber extends OuterSubscriber { this.buffer.push(value); } } - - _innerSub(ish: any, destination: Observer, resultSelector: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, value: T, index: number) { + + _innerSub(ish: any, + destination: Observer, + resultSelector: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, value: T, index: number) { this.add(subscribeToResult(this, ish, value, index)); } - + _complete() { this.hasCompleted = true; if(this.active === 0 && this.buffer.length === 0) { this.destination.complete(); } } - - notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) { + + notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) { const { resultSelector, destination } = this; if(resultSelector) { - const result = tryCatch(resultSelector)(innerValue, outerValue, innerIndex, outerIndex); + const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex); if(result === errorObject) { destination.error(errorObject.e); } else { @@ -74,11 +75,11 @@ export class MergeMapToSubscriber extends OuterSubscriber { destination.next(innerValue); } } - + notifyError(err: any) { this.destination.error(err); } - + notifyComplete(innerSub: InnerSubscriber) { const buffer = this.buffer; this.remove(innerSub); diff --git a/src/operators/mergeMapTo.ts b/src/operators/mergeMapTo.ts index bd6d6419ba..cebca300d2 100644 --- a/src/operators/mergeMapTo.ts +++ b/src/operators/mergeMapTo.ts @@ -2,7 +2,7 @@ import Observable from '../Observable'; import { MergeMapToOperator } from './mergeMapTo-support'; export default function mergeMapTo(observable: Observable, - resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2, - concurrent: number = Number.POSITIVE_INFINITY) : Observable { + resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, + concurrent: number = Number.POSITIVE_INFINITY) : Observable { return this.lift(new MergeMapToOperator(observable, resultSelector, concurrent)); } \ No newline at end of file diff --git a/src/operators/switch.ts b/src/operators/switch.ts index 5d8695321f..1875d9925a 100644 --- a/src/operators/switch.ts +++ b/src/operators/switch.ts @@ -28,20 +28,20 @@ class SwitchSubscriber extends OuterSubscriber { constructor(destination: Observer) { super(destination); } - + _next(value: any) { this.unsubscribeInner(); this.active++; - this.add(this.innerSubscription = subscribeToResult(this, value)); + this.add(this.innerSubscription = subscribeToResult(this, value)); } - + _complete() { this.hasCompleted = true; if(this.active === 0) { this.destination.complete(); } } - + unsubscribeInner() { this.active = this.active > 0 ? this.active - 1 : 0; const innerSubscription = this.innerSubscription; @@ -50,15 +50,15 @@ class SwitchSubscriber extends OuterSubscriber { this.remove(innerSubscription); } } - - notifyNext(value: any) { - this.destination.next(value); + + notifyNext(outerValue: T, innerValue: any) { + this.destination.next(innerValue); } - + notifyError(err: any) { this.destination.error(err); } - + notifyComplete() { this.unsubscribeInner(); if(this.hasCompleted && this.active === 0) { diff --git a/src/operators/switchMap.ts b/src/operators/switchMap.ts index 7e17c875e6..fbf25c2a4a 100644 --- a/src/operators/switchMap.ts +++ b/src/operators/switchMap.ts @@ -10,14 +10,14 @@ import OuterSubscriber from '../OuterSubscriber'; import subscribeToResult from '../util/subscribeToResult'; export default function switchMap(project: (value: T, index: number) => Observable, - resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2): Observable{ + resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2): Observable { return this.lift(new SwitchMapOperator(project, resultSelector)); } class SwitchMapOperator implements Operator { constructor(private project: (value: T, index: number) => Observable, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) { } call(subscriber: Subscriber): Subscriber { @@ -30,16 +30,16 @@ class SwitchMapSubscriber extends OuterSubscriber { private innerSubscription: Subscription; private hasCompleted = false; index: number = 0; - + constructor(destination: Observer, private project: (value: T, index: number) => Observable, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) { super(destination); } - + _next(value: any) { const index = this.index++; - const destination = this.destination; + const destination = this.destination; let result = tryCatch(this.project)(value, index); if(result === errorObject) { destination.error(result.e); @@ -51,7 +51,7 @@ class SwitchMapSubscriber extends OuterSubscriber { this.add(this.innerSubscription = subscribeToResult(this, result, value, index)); } } - + _complete() { const innerSubscription = this.innerSubscription; this.hasCompleted = true; @@ -59,7 +59,7 @@ class SwitchMapSubscriber extends OuterSubscriber { this.destination.complete(); } } - + notifyComplete(innerSub: Subscription) { this.remove(innerSub); const prevSubscription = this.innerSubscription; @@ -67,20 +67,20 @@ class SwitchMapSubscriber extends OuterSubscriber { prevSubscription.unsubscribe(); } this.innerSubscription = null; - + if(this.hasCompleted) { this.destination.complete(); } } - + notifyError(err: any) { this.destination.error(err); } - - notifyNext(innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) { + + notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) { const { resultSelector, destination } = this; if(resultSelector) { - const result = tryCatch(resultSelector)(innerValue, outerValue, innerIndex, outerIndex); + const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex); if(result === errorObject) { destination.error(errorObject.e); } else { diff --git a/src/operators/switchMapTo.ts b/src/operators/switchMapTo.ts index 3959d913d8..a47134a615 100644 --- a/src/operators/switchMapTo.ts +++ b/src/operators/switchMapTo.ts @@ -7,13 +7,13 @@ import Subscription from '../Subscription'; import { MergeMapToSubscriber } from './mergeMapTo-support'; export default function switchMapTo(observable: Observable, - projectResult?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2): Observable { + projectResult?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2): Observable { return this.lift(new SwitchMapToOperator(observable, projectResult)); } class SwitchMapToOperator implements Operator { constructor(private observable: Observable, - private resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { + private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) { } call(subscriber: Subscriber): Subscriber { @@ -22,12 +22,11 @@ class SwitchMapToOperator implements Operator { } class SwitchMapToSubscriber extends MergeMapToSubscriber { - innerSubscription: Subscription; constructor(destination: Observer, observable: Observable, - resultSelector?: (innerValue: R, outerValue: T, innerIndex: number, outerIndex: number) => R2) { + resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) { super(destination, observable, resultSelector, 1); } } diff --git a/src/operators/withLatestFrom.ts b/src/operators/withLatestFrom.ts index 8bd8a3579d..c11a86e609 100644 --- a/src/operators/withLatestFrom.ts +++ b/src/operators/withLatestFrom.ts @@ -9,17 +9,17 @@ import OuterSubscriber from '../OuterSubscriber'; import subscribeToResult from '../util/subscribeToResult'; /** - * @param {Observable} observables the observables to get the latest values from. + * @param {Observable} observables the observables to get the latest values from. * @param {Function} [project] optional projection function for merging values together. Receives all values in order * of observables passed. (e.g. `a.withLatestFrom(b, c, (a1, b1, c1) => a1 + b1 + c1)`). If this is not passed, arrays * will be returned. * @description merges each value from an observable with the latest values from the other passed observables. * All observables must emit at least one value before the resulting observable will emit - * + * * #### example * ``` * A.withLatestFrom(B, C) - * + * * A: ----a-----------------b---------------c-----------| * B: ---d----------------e--------------f---------| * C: --x----------------y-------------z-------------| @@ -47,23 +47,23 @@ class WithLatestFromOperator implements Operator { class WithLatestFromSubscriber extends OuterSubscriber { private values: any[]; private toRespond: number[] = []; - + constructor(destination: Subscriber, private observables: Observable[], private project?: (...values: any[]) => Observable) { super(destination); const len = observables.length; this.values = new Array(len); - + for (let i = 0; i < len; i++) { this.toRespond.push(i); } - + for (let i = 0; i < len; i++) { let observable = observables[i]; this.add(subscribeToResult(this, observable, observable, i)); } } - - notifyNext(value, observable, index, observableIndex) { + + notifyNext(observable, value, observableIndex, index) { this.values[observableIndex] = value; const toRespond = this.toRespond; if(toRespond.length > 0) { @@ -73,11 +73,11 @@ class WithLatestFromSubscriber extends OuterSubscriber { } } } - + notifyComplete() { // noop } - + _next(value: T) { if (this.toRespond.length === 0) { const values = this.values; diff --git a/src/operators/zip-support.ts b/src/operators/zip-support.ts index dd16cd78a7..24bd1afb3c 100644 --- a/src/operators/zip-support.ts +++ b/src/operators/zip-support.ts @@ -32,7 +32,7 @@ export class ZipSubscriber extends Subscriber { private project: (...values: Array) => R; private iterators = []; private active = 0; - + constructor(destination: Subscriber, project?: (...values: Array) => R, values: any = Object.create(null)) { @@ -67,19 +67,19 @@ export class ZipSubscriber extends Subscriber { } } } - + notifyInactive() { this.active--; if(this.active === 0) { this.destination.complete(); } } - + checkIterators() { const iterators = this.iterators; const len = iterators.length; const destination = this.destination; - + // abort if not all of them have values for(let i = 0; i < len; i++) { let iterator = iterators[i]; @@ -87,27 +87,27 @@ export class ZipSubscriber extends Subscriber { return; } } - + let shouldComplete = false; const args = []; for(let i = 0; i < len; i++) { let iterator = iterators[i]; let result = iterator.next(); - + // check to see if it's completed now that you've gotten // the next value. if(iterator.hasCompleted()) { shouldComplete = true; } - + if(result.done) { destination.complete(); return; } - + args.push(result.value); } - + const project = this.project; if(project) { let result = tryCatch(project).apply(this, args); @@ -119,7 +119,7 @@ export class ZipSubscriber extends Subscriber { } else { destination.next(args); } - + if(shouldComplete) { destination.complete(); } @@ -133,21 +133,21 @@ interface LookAheadIterator extends Iterator { class StaticIterator implements LookAheadIterator { private nextResult: IteratorResult; - + constructor(private iterator: Iterator) { - this.nextResult = iterator.next(); + this.nextResult = iterator.next(); } - + hasValue() { return true; } - + next(): IteratorResult { const result = this.nextResult; this.nextResult = this.iterator.next(); return result; } - + hasCompleted() { const nextResult = this.nextResult; return nextResult && nextResult.done; @@ -157,25 +157,25 @@ class StaticIterator implements LookAheadIterator { class StaticArrayIterator implements LookAheadIterator { private index = 0; private length = 0; - + constructor(private array: T[]) { this.length = array.length; } - + [$$iterator]() { return this; } - + next(value?: any): IteratorResult { const i = this.index++; const array = this.array; return i < this.length ? { value: array[i], done: false } : { done: true }; } - + hasValue() { return this.array.length > this.index; } - + hasCompleted() { return this.array.length === this.index; } @@ -185,15 +185,15 @@ class ZipBufferIterator extends OuterSubscriber implements LookAhead stillUnsubscribed = true; buffer: T[] = []; isComplete = false; - + constructor(destination: Observer, private parent: ZipSubscriber, private observable: Observable, private index: number) { super(destination); } - + [$$iterator]() { return this; } - + // NOTE: there is actually a name collision here with Subscriber.next and Iterator.next // this is legit because `next()` will never be called by a subscription in this case. next(): IteratorResult { @@ -204,15 +204,15 @@ class ZipBufferIterator extends OuterSubscriber implements LookAhead return { value: buffer.shift(), done: false }; } } - + hasValue() { return this.buffer.length > 0; } - + hasCompleted() { return this.buffer.length === 0 && this.isComplete; } - + notifyComplete() { if(this.buffer.length > 0) { this.isComplete = true; @@ -221,13 +221,13 @@ class ZipBufferIterator extends OuterSubscriber implements LookAhead this.destination.complete(); } } - - notifyNext(innerValue, outerValue, innerIndex, outerIndex) { + + notifyNext(outerValue, innerValue, outerIndex, innerIndex) { this.buffer.push(innerValue); this.parent.checkIterators(); } - + subscribe(value: any, index: number) { this.add(subscribeToResult(this, this.observable, this, index)); - } + } } \ No newline at end of file