|
1 | 1 | import { Observable } from '../Observable';
|
2 |
| -import { Operator } from '../Operator'; |
3 |
| -import { Subscriber } from '../Subscriber'; |
4 |
| -import { Subscription } from '../Subscription'; |
5 |
| -import { OuterSubscriber } from '../OuterSubscriber'; |
6 |
| -import { InnerSubscriber } from '../InnerSubscriber'; |
7 |
| -import { subscribeToResult } from '../util/subscribeToResult'; |
| 2 | +import { switchAll as higherOrder } from '../operators'; |
8 | 3 |
|
9 | 4 | /**
|
10 | 5 | * Converts a higher-order Observable into a first-order Observable by
|
@@ -48,66 +43,6 @@ import { subscribeToResult } from '../util/subscribeToResult';
|
48 | 43 | * @name switch
|
49 | 44 | * @owner Observable
|
50 | 45 | */
|
51 |
| -export function _switch<T>(this: Observable<T>): T { |
52 |
| - return <any>this.lift<any>(new SwitchOperator()); |
53 |
| -} |
54 |
| - |
55 |
| -class SwitchOperator<T, R> implements Operator<T, R> { |
56 |
| - call(subscriber: Subscriber<R>, source: any): any { |
57 |
| - return source.subscribe(new SwitchSubscriber(subscriber)); |
58 |
| - } |
59 |
| -} |
60 |
| - |
61 |
| -/** |
62 |
| - * We need this JSDoc comment for affecting ESDoc. |
63 |
| - * @ignore |
64 |
| - * @extends {Ignored} |
65 |
| - */ |
66 |
| -class SwitchSubscriber<T, R> extends OuterSubscriber<T, R> { |
67 |
| - private active: number = 0; |
68 |
| - private hasCompleted: boolean = false; |
69 |
| - innerSubscription: Subscription; |
70 |
| - |
71 |
| - constructor(destination: Subscriber<R>) { |
72 |
| - super(destination); |
73 |
| - } |
74 |
| - |
75 |
| - protected _next(value: T): void { |
76 |
| - this.unsubscribeInner(); |
77 |
| - this.active++; |
78 |
| - this.add(this.innerSubscription = subscribeToResult(this, value)); |
79 |
| - } |
80 |
| - |
81 |
| - protected _complete(): void { |
82 |
| - this.hasCompleted = true; |
83 |
| - if (this.active === 0) { |
84 |
| - this.destination.complete(); |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - private unsubscribeInner(): void { |
89 |
| - this.active = this.active > 0 ? this.active - 1 : 0; |
90 |
| - const innerSubscription = this.innerSubscription; |
91 |
| - if (innerSubscription) { |
92 |
| - innerSubscription.unsubscribe(); |
93 |
| - this.remove(innerSubscription); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - notifyNext(outerValue: T, innerValue: R, |
98 |
| - outerIndex: number, innerIndex: number, |
99 |
| - innerSub: InnerSubscriber<T, R>): void { |
100 |
| - this.destination.next(innerValue); |
101 |
| - } |
102 |
| - |
103 |
| - notifyError(err: any): void { |
104 |
| - this.destination.error(err); |
105 |
| - } |
106 |
| - |
107 |
| - notifyComplete(): void { |
108 |
| - this.unsubscribeInner(); |
109 |
| - if (this.hasCompleted && this.active === 0) { |
110 |
| - this.destination.complete(); |
111 |
| - } |
112 |
| - } |
| 46 | +export function _switch<T>(this: Observable<Observable<T>>): Observable<T> { |
| 47 | + return higherOrder()(this); |
113 | 48 | }
|
0 commit comments