-
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.
- Loading branch information
Showing
5 changed files
with
88 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,29 @@ | ||
/* globals describe, it */ | ||
var Rx = require('../../dist/cjs/Rx.KitchenSink'); | ||
var Observable = Rx.Observable; | ||
var Subscription = Rx.Subscription; | ||
|
||
describe('Observable.using', function () { | ||
it('should dispose of the resource when the subscription is disposed', function (done) { | ||
var disposed = false; | ||
var source = Observable.using( | ||
function () { | ||
return new Subscription(function () { | ||
disposed = true; | ||
}); | ||
}, | ||
function (resource) { | ||
return 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); | ||
} | ||
} | ||
} | ||
} |