1import { Observable } from '../Observable'; 2import { Unsubscribable, ObservableInput } from '../types'; 3import { from } from './from'; // from from from! LAWL 4import { EMPTY } from './empty'; 5 6/** 7 * Creates an Observable that uses a resource which will be disposed at the same time as the Observable. 8 * 9 * <span class="informal">Use it when you catch yourself cleaning up after an Observable.</span> 10 * 11 * `using` is a factory operator, which accepts two functions. First function returns a disposable resource. 12 * It can be an arbitrary object that implements `unsubscribe` method. Second function will be injected with 13 * that object and should return an Observable. That Observable can use resource object during its execution. 14 * Both functions passed to `using` will be called every time someone subscribes - neither an Observable nor 15 * resource object will be shared in any way between subscriptions. 16 * 17 * When Observable returned by `using` is subscribed, Observable returned from the second function will be subscribed 18 * as well. All its notifications (nexted values, completion and error events) will be emitted unchanged by the output 19 * Observable. If however someone unsubscribes from the Observable or source Observable completes or errors by itself, 20 * the `unsubscribe` method on resource object will be called. This can be used to do any necessary clean up, which 21 * otherwise would have to be handled by hand. Note that complete or error notifications are not emitted when someone 22 * cancels subscription to an Observable via `unsubscribe`, so `using` can be used as a hook, allowing you to make 23 * sure that all resources which need to exist during an Observable execution will be disposed at appropriate time. 24 * 25 * @see {@link defer} 26 * 27 * @param {function(): ISubscription} resourceFactory A function which creates any resource object 28 * that implements `unsubscribe` method. 29 * @param {function(resource: ISubscription): Observable<T>} observableFactory A function which 30 * creates an Observable, that can use injected resource object. 31 * @return {Observable<T>} An Observable that behaves the same as Observable returned by `observableFactory`, but 32 * which - when completed, errored or unsubscribed - will also call `unsubscribe` on created resource object. 33 */ 34export function using<T>(resourceFactory: () => Unsubscribable | void, 35 observableFactory: (resource: Unsubscribable | void) => ObservableInput<T> | void): Observable<T> { 36 return new Observable<T>(subscriber => { 37 let resource: Unsubscribable | void; 38 39 try { 40 resource = resourceFactory(); 41 } catch (err) { 42 subscriber.error(err); 43 return undefined; 44 } 45 46 let result: ObservableInput<T> | void; 47 try { 48 result = observableFactory(resource); 49 } catch (err) { 50 subscriber.error(err); 51 return undefined; 52 } 53 54 const source = result ? from(result) : EMPTY; 55 const subscription = source.subscribe(subscriber); 56 return () => { 57 subscription.unsubscribe(); 58 if (resource) { 59 resource.unsubscribe(); 60 } 61 }; 62 }); 63} 64