3544880349788765f771d2ad0cf889fa04b71cabe877a6b1aa79c3758593ea844e704f4c1d0972d25e106fd8ef1fe3354b82d825be2c4bbed1057d5ca92c3c 2.8 KB

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