|
1 | | -import { Operator } from '../Operator'; |
| 1 | + |
2 | 2 | import { Observable, ObservableInput } from '../Observable'; |
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'; |
| 3 | +import { exhaustMap as higherOrder } from '../operators'; |
8 | 4 |
|
9 | 5 | /* tslint:disable:max-line-length */ |
10 | 6 | export function exhaustMap<T, R>(this: Observable<T>, project: (value: T, index: number) => ObservableInput<R>): Observable<R>; |
@@ -58,92 +54,5 @@ export function exhaustMap<T, I, R>(this: Observable<T>, project: (value: T, ind |
58 | 54 | */ |
59 | 55 | export function exhaustMap<T, I, R>(this: Observable<T>, project: (value: T, index: number) => ObservableInput<I>, |
60 | 56 | resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): Observable<R> { |
61 | | - return this.lift(new SwitchFirstMapOperator(project, resultSelector)); |
62 | | -} |
63 | | - |
64 | | -class SwitchFirstMapOperator<T, I, R> implements Operator<T, R> { |
65 | | - constructor(private project: (value: T, index: number) => ObservableInput<I>, |
66 | | - private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) { |
67 | | - } |
68 | | - |
69 | | - call(subscriber: Subscriber<R>, source: any): any { |
70 | | - return source.subscribe(new SwitchFirstMapSubscriber(subscriber, this.project, this.resultSelector)); |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -/** |
75 | | - * We need this JSDoc comment for affecting ESDoc. |
76 | | - * @ignore |
77 | | - * @extends {Ignored} |
78 | | - */ |
79 | | -class SwitchFirstMapSubscriber<T, I, R> extends OuterSubscriber<T, I> { |
80 | | - private hasSubscription: boolean = false; |
81 | | - private hasCompleted: boolean = false; |
82 | | - private index: number = 0; |
83 | | - |
84 | | - constructor(destination: Subscriber<R>, |
85 | | - private project: (value: T, index: number) => ObservableInput<I>, |
86 | | - private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) { |
87 | | - super(destination); |
88 | | - } |
89 | | - |
90 | | - protected _next(value: T): void { |
91 | | - if (!this.hasSubscription) { |
92 | | - this.tryNext(value); |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - private tryNext(value: T): void { |
97 | | - const index = this.index++; |
98 | | - const destination = this.destination; |
99 | | - try { |
100 | | - const result = this.project(value, index); |
101 | | - this.hasSubscription = true; |
102 | | - this.add(subscribeToResult(this, result, value, index)); |
103 | | - } catch (err) { |
104 | | - destination.error(err); |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - protected _complete(): void { |
109 | | - this.hasCompleted = true; |
110 | | - if (!this.hasSubscription) { |
111 | | - this.destination.complete(); |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - notifyNext(outerValue: T, innerValue: I, |
116 | | - outerIndex: number, innerIndex: number, |
117 | | - innerSub: InnerSubscriber<T, I>): void { |
118 | | - const { resultSelector, destination } = this; |
119 | | - if (resultSelector) { |
120 | | - this.trySelectResult(outerValue, innerValue, outerIndex, innerIndex); |
121 | | - } else { |
122 | | - destination.next(innerValue); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - private trySelectResult(outerValue: T, innerValue: I, |
127 | | - outerIndex: number, innerIndex: number): void { |
128 | | - const { resultSelector, destination } = this; |
129 | | - try { |
130 | | - const result = resultSelector(outerValue, innerValue, outerIndex, innerIndex); |
131 | | - destination.next(result); |
132 | | - } catch (err) { |
133 | | - destination.error(err); |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - notifyError(err: any): void { |
138 | | - this.destination.error(err); |
139 | | - } |
140 | | - |
141 | | - notifyComplete(innerSub: Subscription): void { |
142 | | - this.remove(innerSub); |
143 | | - |
144 | | - this.hasSubscription = false; |
145 | | - if (this.hasCompleted) { |
146 | | - this.destination.complete(); |
147 | | - } |
148 | | - } |
| 57 | + return higherOrder(project, resultSelector)(this); |
149 | 58 | } |
0 commit comments