diff --git a/src/operator/switch.ts b/src/operator/switch.ts index 8e5629fd86..bd8e4ee0bc 100644 --- a/src/operator/switch.ts +++ b/src/operator/switch.ts @@ -1,10 +1,5 @@ import { Observable } from '../Observable'; -import { Operator } from '../Operator'; -import { Subscriber } from '../Subscriber'; -import { Subscription } from '../Subscription'; -import { OuterSubscriber } from '../OuterSubscriber'; -import { InnerSubscriber } from '../InnerSubscriber'; -import { subscribeToResult } from '../util/subscribeToResult'; +import { switchAll as higherOrder } from '../operators'; /** * Converts a higher-order Observable into a first-order Observable by @@ -48,66 +43,6 @@ import { subscribeToResult } from '../util/subscribeToResult'; * @name switch * @owner Observable */ -export function _switch(this: Observable): T { - return this.lift(new SwitchOperator()); -} - -class SwitchOperator implements Operator { - call(subscriber: Subscriber, source: any): any { - return source.subscribe(new SwitchSubscriber(subscriber)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class SwitchSubscriber extends OuterSubscriber { - private active: number = 0; - private hasCompleted: boolean = false; - innerSubscription: Subscription; - - constructor(destination: Subscriber) { - super(destination); - } - - protected _next(value: T): void { - this.unsubscribeInner(); - this.active++; - this.add(this.innerSubscription = subscribeToResult(this, value)); - } - - protected _complete(): void { - this.hasCompleted = true; - if (this.active === 0) { - this.destination.complete(); - } - } - - private unsubscribeInner(): void { - this.active = this.active > 0 ? this.active - 1 : 0; - const innerSubscription = this.innerSubscription; - if (innerSubscription) { - innerSubscription.unsubscribe(); - this.remove(innerSubscription); - } - } - - notifyNext(outerValue: T, innerValue: R, - outerIndex: number, innerIndex: number, - innerSub: InnerSubscriber): void { - this.destination.next(innerValue); - } - - notifyError(err: any): void { - this.destination.error(err); - } - - notifyComplete(): void { - this.unsubscribeInner(); - if (this.hasCompleted && this.active === 0) { - this.destination.complete(); - } - } +export function _switch(this: Observable>): Observable { + return higherOrder()(this); } diff --git a/src/operators/index.ts b/src/operators/index.ts index 14f05dc1b1..e08803359f 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -10,6 +10,7 @@ export { multicast } from './multicast'; export { publish } from './publish'; export { reduce } from './reduce'; export { scan } from './scan'; +export { switchAll } from './switchAll'; export { switchMap } from './switchMap'; export { takeLast } from './takeLast'; export { tap } from './tap'; diff --git a/src/operators/switchAll.ts b/src/operators/switchAll.ts new file mode 100644 index 0000000000..ec638fed74 --- /dev/null +++ b/src/operators/switchAll.ts @@ -0,0 +1,8 @@ +import { OperatorFunction } from '../interfaces'; +import { Observable } from '../Observable'; +import { switchMap } from './switchMap'; +import { identity } from '../util/identity'; + +export function switchAll(): OperatorFunction, T> { + return switchMap(identity); +} diff --git a/src/util/identity.ts b/src/util/identity.ts new file mode 100644 index 0000000000..6589842c03 --- /dev/null +++ b/src/util/identity.ts @@ -0,0 +1,3 @@ +export function identity(x: T): T { + return x; +}