-
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.
feat(using): add static Observable.using creation operator.
- Loading branch information
Showing
5 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as Rx from '../../dist/cjs/Rx.KitchenSink'; | ||
import {it} from '../helpers/test-helper'; | ||
|
||
const Observable = Rx.Observable; | ||
const Subscription = Rx.Subscription; | ||
|
||
describe('Observable.using', () => { | ||
it('should dispose of the resource when the subscription is disposed', (done) => { | ||
let disposed = false; | ||
const source = Observable.using( | ||
() => new Subscription(() => disposed = true), | ||
(resource) => Observable.range(0, 3) | ||
) | ||
.take(2); | ||
|
||
source.subscribe(); | ||
|
||
if (disposed) { | ||
done(); | ||
} else { | ||
done.fail('disposed should be true but was false'); | ||
} | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {Observable} from '../../Observable'; | ||
import {UsingObservable} from '../../observable/UsingObservable'; | ||
|
||
Observable.using = UsingObservable.create; | ||
|
||
export var _void: void; |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {Observable} from '../Observable'; | ||
import {Subscriber} from '../Subscriber'; | ||
import {Subscription} from '../Subscription'; | ||
|
||
export class UsingObservable<T> extends Observable<T> { | ||
|
||
static create<T>(resourceFactory: () => Subscription, | ||
observableFactory: (resource: Subscription) => Observable<T>): Observable<T> { | ||
return new UsingObservable<T>(resourceFactory, observableFactory); | ||
} | ||
|
||
constructor(private resourceFactory: () => Subscription, | ||
private observableFactory: (resource: Subscription) => Observable<T>) { | ||
super(); | ||
} | ||
|
||
protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void { | ||
|
||
const { resourceFactory, observableFactory } = this; | ||
|
||
let resource: Subscription, | ||
source: Observable<T>, | ||
error: any, errorHappened = false; | ||
|
||
try { | ||
resource = resourceFactory(); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
} | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else { | ||
subscriber.add(resource); | ||
try { | ||
source = observableFactory(resource); | ||
} catch (e) { | ||
error = e; | ||
errorHappened = true; | ||
} | ||
|
||
if (errorHappened) { | ||
subscriber.error(error); | ||
} else { | ||
return source.subscribe(subscriber); | ||
} | ||
} | ||
} | ||
} |