import { Observable } from '../Observable'; import { Unsubscribable, ObservableInput } from '../types'; import { from } from './from'; // from from from! LAWL import { EMPTY } from './empty'; /** * Creates an Observable that uses a resource which will be disposed at the same time as the Observable. * * <span class="informal">Use it when you catch yourself cleaning up after an Observable.</span> * * `using` is a factory operator, which accepts two functions. First function returns a disposable resource. * It can be an arbitrary object that implements `unsubscribe` method. Second function will be injected with * that object and should return an Observable. That Observable can use resource object during its execution. * Both functions passed to `using` will be called every time someone subscribes - neither an Observable nor * resource object will be shared in any way between subscriptions. * * When Observable returned by `using` is subscribed, Observable returned from the second function will be subscribed * as well. All its notifications (nexted values, completion and error events) will be emitted unchanged by the output * Observable. If however someone unsubscribes from the Observable or source Observable completes or errors by itself, * the `unsubscribe` method on resource object will be called. This can be used to do any necessary clean up, which * otherwise would have to be handled by hand. Note that complete or error notifications are not emitted when someone * cancels subscription to an Observable via `unsubscribe`, so `using` can be used as a hook, allowing you to make * sure that all resources which need to exist during an Observable execution will be disposed at appropriate time. * * @see {@link defer} * * @param {function(): ISubscription} resourceFactory A function which creates any resource object * that implements `unsubscribe` method. * @param {function(resource: ISubscription): Observable<T>} observableFactory A function which * creates an Observable, that can use injected resource object. * @return {Observable<T>} An Observable that behaves the same as Observable returned by `observableFactory`, but * which - when completed, errored or unsubscribed - will also call `unsubscribe` on created resource object. */ export function using<T>(resourceFactory: () => Unsubscribable | void, observableFactory: (resource: Unsubscribable | void) => ObservableInput<T> | void): Observable<T> { return new Observable<T>(subscriber => { let resource: Unsubscribable | void; try { resource = resourceFactory(); } catch (err) { subscriber.error(err); return undefined; } let result: ObservableInput<T> | void; try { result = observableFactory(resource); } catch (err) { subscriber.error(err); return undefined; } const source = result ? from(result) : EMPTY; const subscription = source.subscribe(subscriber); return () => { subscription.unsubscribe(); if (resource) { resource.unsubscribe(); } }; }); }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.ts | File | 5.42 KB | 0644 |
|
SubscribeOnObservable.ts | File | 1.59 KB | 0644 |
|
bindCallback.ts | File | 15.9 KB | 0644 |
|
bindNodeCallback.ts | File | 15.41 KB | 0644 |
|
combineLatest.ts | File | 21.95 KB | 0644 |
|
concat.ts | File | 9.43 KB | 0644 |
|
defer.ts | File | 2.53 KB | 0644 |
|
empty.ts | File | 2.21 KB | 0644 |
|
forkJoin.ts | File | 9.49 KB | 0644 |
|
from.ts | File | 3.06 KB | 0644 |
|
fromArray.ts | File | 423 B | 0644 |
|
fromEvent.ts | File | 10.38 KB | 0644 |
|
fromEventPattern.ts | File | 7.14 KB | 0644 |
|
fromIterable.ts | File | 511 B | 0644 |
|
fromObservable.ts | File | 485 B | 0644 |
|
fromPromise.ts | File | 439 B | 0644 |
|
generate.ts | File | 12.55 KB | 0644 |
|
iif.ts | File | 3.22 KB | 0644 |
|
interval.ts | File | 2.53 KB | 0644 |
|
merge.ts | File | 9.94 KB | 0644 |
|
never.ts | File | 1.16 KB | 0644 |
|
of.ts | File | 4.77 KB | 0644 |
|
onErrorResumeNext.ts | File | 3.83 KB | 0644 |
|
pairs.ts | File | 3.06 KB | 0644 |
|
partition.ts | File | 2.61 KB | 0644 |
|
race.ts | File | 5.03 KB | 0644 |
|
range.ts | File | 2.28 KB | 0644 |
|
throwError.ts | File | 2.2 KB | 0644 |
|
timer.ts | File | 3.32 KB | 0644 |
|
using.ts | File | 3.04 KB | 0644 |
|
zip.ts | File | 12.06 KB | 0644 |
|