Skip to content

Commit 2f12572

Browse files
committed
feat(switchAll): add higher-order lettable version of switch
- also fixes typings on switch
1 parent f85c60e commit 2f12572

File tree

4 files changed

+15
-68
lines changed

4 files changed

+15
-68
lines changed

src/operator/switch.ts

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
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';
83

94
/**
105
* Converts a higher-order Observable into a first-order Observable by
@@ -48,66 +43,6 @@ import { subscribeToResult } from '../util/subscribeToResult';
4843
* @name switch
4944
* @owner Observable
5045
*/
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);
11348
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { multicast } from './multicast';
1010
export { publish } from './publish';
1111
export { reduce } from './reduce';
1212
export { scan } from './scan';
13+
export { switchAll } from './switchAll';
1314
export { switchMap } from './switchMap';
1415
export { takeLast } from './takeLast';
1516
export { tap } from './tap';

src/operators/switchAll.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { OperatorFunction } from '../interfaces';
2+
import { Observable } from '../Observable';
3+
import { switchMap } from './switchMap';
4+
import { identity } from '../util/identity';
5+
6+
export function switchAll<T>(): OperatorFunction<Observable<T>, T> {
7+
return switchMap(identity);
8+
}

src/util/identity.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function identity<T>(x: T): T {
2+
return x;
3+
}

0 commit comments

Comments
 (0)