| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Unsubscribable, ObservableInput, ObservedValueOf } from '../types'; |
| 3 |
import { innerFrom } from './innerFrom'; |
| 4 |
import { 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 resourceFactory A function which creates any resource object that implements `unsubscribe` method. |
| 28 |
* @param observableFactory A function which creates an Observable, that can use injected resource object. |
| 29 |
* @return An Observable that behaves the same as Observable returned by `observableFactory`, but |
| 30 |
* which - when completed, errored or unsubscribed - will also call `unsubscribe` on created resource object. |
| 31 |
*/ |
| 32 |
export function using<T extends ObservableInput<any>>( |
| 33 |
resourceFactory: () => Unsubscribable | void, |
| 34 |
observableFactory: (resource: Unsubscribable | void) => T | void |
| 35 |
): Observable<ObservedValueOf<T>> { |
| 36 |
return new Observable<ObservedValueOf<T>>((subscriber) => { |
| 37 |
const resource = resourceFactory(); |
| 38 |
const result = observableFactory(resource); |
| 39 |
const source = result ? innerFrom(result) : EMPTY; |
| 40 |
source.subscribe(subscriber); |
| 41 |
return () => { |
| 42 |
// NOTE: Optional chaining did not work here. |
| 43 |
// Related TS Issue: https://github.com/microsoft/TypeScript/issues/40818 |
| 44 |
if (resource) { |
| 45 |
resource.unsubscribe(); |
| 46 |
} |
| 47 |
}; |
| 48 |
}); |
| 49 |
} |