-
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(usingObservable): accepts factory returns promise
relates to #1483
- Loading branch information
Showing
2 changed files
with
111 additions
and
30 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,55 +1,57 @@ | ||
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 UsingObservable<T> extends Observable<T> { | ||
|
||
static create<T>(resourceFactory: () => Subscription, | ||
observableFactory: (resource: Subscription) => Observable<T>): Observable<T> { | ||
static create<T>(resourceFactory: () => Subscription | void, | ||
observableFactory: (resource: Subscription) => SubscribableOrPromise<T> | void): Observable<T> { | ||
return new UsingObservable<T>(resourceFactory, observableFactory); | ||
} | ||
|
||
constructor(private resourceFactory: () => Subscription, | ||
private observableFactory: (resource: Subscription) => Observable<T>) { | ||
constructor(private resourceFactory: () => Subscription | void, | ||
private observableFactory: (resource: Subscription) => SubscribableOrPromise<T> | void) { | ||
super(); | ||
} | ||
|
||
protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void { | ||
|
||
const { resourceFactory, observableFactory } = this; | ||
|
||
let resource: Subscription, | ||
source: Observable<T>, | ||
error: any, errorHappened = false; | ||
let resource: Subscription; | ||
|
||
try { | ||
resource = resourceFactory(); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
resource = <Subscription>resourceFactory(); | ||
return new UsingSubscriber(subscriber, resource, observableFactory); | ||
} catch (err) { | ||
subscriber.error(err); | ||
} | ||
} | ||
} | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else { | ||
subscriber.add(resource); | ||
try { | ||
source = observableFactory(resource); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
} | ||
class UsingSubscriber<T> extends OuterSubscriber<T, T> { | ||
constructor(destination: Subscriber<T>, | ||
private resource: Subscription, | ||
private observableFactory: (resource: Subscription) => SubscribableOrPromise<T> | void) { | ||
super(destination); | ||
destination.add(resource); | ||
this.tryUse(); | ||
} | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else { | ||
return source.subscribe(subscriber); | ||
private tryUse(): void { | ||
try { | ||
const source = this.observableFactory.call(this, this.resource); | ||
if (source) { | ||
this.add(subscribeToResult(this, source)); | ||
} | ||
} catch (err) { | ||
this._error(err); | ||
} | ||
} | ||
} | ||
} |