-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ifObservable): accept promise as source
relates to #1483
- Loading branch information
Showing
2 changed files
with
105 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,59 @@ | ||
import {Observable} from '../Observable'; | ||
import {Observable, SubscribableOrPromise} from '../Observable'; | ||
import {Subscriber} from '../Subscriber'; | ||
import {Subscription} from '../Subscription'; | ||
|
||
import {subscribeToResult} from '../util/subscribeToResult'; | ||
import {OuterSubscriber} from '../OuterSubscriber'; | ||
/** | ||
* We need this JSDoc comment for affecting ESDoc. | ||
* @extends {Ignored} | ||
* @hide true | ||
*/ | ||
export class IfObservable<T, R> extends Observable<T> { | ||
|
||
static create<T, R>(condition: () => boolean, | ||
thenSource?: Observable<T>, | ||
elseSource?: Observable<R>): Observable<T|R> { | ||
static create<T, R>(condition: () => boolean | void, | ||
thenSource?: SubscribableOrPromise<T> | void, | ||
elseSource?: SubscribableOrPromise<R> | void): Observable<T|R> { | ||
return new IfObservable(condition, thenSource, elseSource); | ||
} | ||
|
||
constructor(private condition: () => boolean, | ||
private thenSource?: Observable<T>, | ||
private elseSource?: Observable<R>) { | ||
constructor(private condition: () => boolean | void, | ||
private thenSource?: SubscribableOrPromise<T> | void, | ||
private elseSource?: SubscribableOrPromise<R> | void) { | ||
super(); | ||
} | ||
|
||
protected _subscribe(subscriber: Subscriber<T|R>): Subscription | Function | void { | ||
|
||
const { condition, thenSource, elseSource } = this; | ||
|
||
let result: boolean, error: any, errorHappened = false; | ||
return new IfSubscriber(subscriber, condition, thenSource, elseSource); | ||
} | ||
} | ||
|
||
try { | ||
result = condition(); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
} | ||
class IfSubscriber<T, R> extends OuterSubscriber<T, T> { | ||
constructor(destination: Subscriber<T>, | ||
private condition: () => boolean | void, | ||
private thenSource?: SubscribableOrPromise<T> | void, | ||
private elseSource?: SubscribableOrPromise<R> | void) { | ||
super(destination); | ||
this.tryIf(); | ||
} | ||
|
||
private tryIf(): void { | ||
const { condition, thenSource, elseSource } = this; | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else if (result && thenSource) { | ||
return thenSource.subscribe(subscriber); | ||
} else if (elseSource) { | ||
return elseSource.subscribe(subscriber); | ||
} else { | ||
subscriber.complete(); | ||
let result: boolean; | ||
try { | ||
result = <boolean>condition(); | ||
const source = result ? thenSource : elseSource; | ||
|
||
if (source) { | ||
this.add(subscribeToResult(this, source)); | ||
} else { | ||
this._complete(); | ||
} | ||
} catch (err) { | ||
this._error(err); | ||
} | ||
} | ||
} |