using.d.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Observable } from '../Observable';
  2. import { Unsubscribable, ObservableInput } from '../types';
  3. /**
  4. * Creates an Observable that uses a resource which will be disposed at the same time as the Observable.
  5. *
  6. * <span class="informal">Use it when you catch yourself cleaning up after an Observable.</span>
  7. *
  8. * `using` is a factory operator, which accepts two functions. First function returns a disposable resource.
  9. * It can be an arbitrary object that implements `unsubscribe` method. Second function will be injected with
  10. * that object and should return an Observable. That Observable can use resource object during its execution.
  11. * Both functions passed to `using` will be called every time someone subscribes - neither an Observable nor
  12. * resource object will be shared in any way between subscriptions.
  13. *
  14. * When Observable returned by `using` is subscribed, Observable returned from the second function will be subscribed
  15. * as well. All its notifications (nexted values, completion and error events) will be emitted unchanged by the output
  16. * Observable. If however someone unsubscribes from the Observable or source Observable completes or errors by itself,
  17. * the `unsubscribe` method on resource object will be called. This can be used to do any necessary clean up, which
  18. * otherwise would have to be handled by hand. Note that complete or error notifications are not emitted when someone
  19. * cancels subscription to an Observable via `unsubscribe`, so `using` can be used as a hook, allowing you to make
  20. * sure that all resources which need to exist during an Observable execution will be disposed at appropriate time.
  21. *
  22. * @see {@link defer}
  23. *
  24. * @param {function(): ISubscription} resourceFactory A function which creates any resource object
  25. * that implements `unsubscribe` method.
  26. * @param {function(resource: ISubscription): Observable<T>} observableFactory A function which
  27. * creates an Observable, that can use injected resource object.
  28. * @return {Observable<T>} 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 declare function using<T>(resourceFactory: () => Unsubscribable | void, observableFactory: (resource: Unsubscribable | void) => ObservableInput<T> | void): Observable<T>;