finally.d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Observable } from 'rxjs';
  2. /**
  3. * Returns an Observable that mirrors the source Observable, but will call a specified function when
  4. * the source terminates on complete, error or unsubscribe.
  5. *
  6. * <span class="informal">Ensure a given function will be called when a stream ends, no matter why it ended.</span>
  7. *
  8. * `finally` method accepts as a single parameter a function. This function does not accept any parameters and
  9. * should not return anything. It will be called whenever source Observable completes, errors or is unsubscribed,
  10. * which makes it good candidate to perform any necessary clean up or side effects when Observable terminates,
  11. * no matter how or why it terminated.
  12. *
  13. * Observable returned by `finally` will simply mirror source Observable - each time it is subscribed, source
  14. * Observable will be subscribed underneath.
  15. *
  16. * Note that behavior of `finally` will be repeated per every subscription, so if resulting Observable has
  17. * many subscribers, function passed to `finally` might be potentially called multiple times.
  18. *
  19. * Remember also that `finally` differs quite a lot from passing complete or error handler to {@link subscribe}. It will
  20. * return an Observable which can be further chained, while `subscribe` returns Subscription, basically ending Observable
  21. * chain. Function passed to `finally` will be called also when consumer of resulting Observable unsubscribes from it,
  22. * while handlers passed to `subscribe` will not (even complete handler). But most importantly, `finally` does not start
  23. * an execution of source Observable, like `subscribe` does, allowing you to set up all necessary hooks before
  24. * passing Observable further, even without specific knowledge how or when it will be used.
  25. *
  26. *
  27. * @example <caption>Call finally after complete notification</caption>
  28. * Rx.Observable.of(1, 2, 3)
  29. * .finally(() => console.log('I was finalized!'))
  30. * .map(x => x * 2) // `finally` returns an Observable, so we still can chain operators.
  31. * .subscribe(
  32. * val => console.log(val),
  33. * err => {},
  34. * () => console.log('I completed!')
  35. * );
  36. *
  37. * // Logs:
  38. * // 1
  39. * // 2
  40. * // 3
  41. * // "I completed!"
  42. * // "I was finalized!"
  43. *
  44. *
  45. *
  46. * @example <caption>Call finally after consumer unsubscribes</caption>
  47. * const o = Rx.Observable.interval(1000)
  48. * .finally(() => console.log('Timer stopped'));
  49. *
  50. * const subscription = o.subscribe(
  51. * val => console.log(val),
  52. * err => {},
  53. * () => console.log('Complete!') // Will not be called, since complete handler
  54. * ); // does not react to unsubscription, just to
  55. * // complete notification sent by the Observable itself.
  56. *
  57. * setTimeout(() => subscription.unsubscribe(), 2500);
  58. *
  59. * // Logs:
  60. * // 0 after 1s
  61. * // 1 after 2s
  62. * // "Timer stopped" after 2.5s
  63. *
  64. * @see {@link using}
  65. *
  66. * @param {function} callback Function to be called when source terminates (completes, errors or is unsubscribed).
  67. * @return {Observable} An Observable that mirrors the source, but will call the specified function on termination.
  68. * @method finally
  69. * @name finally
  70. * @owner Observable
  71. */
  72. export declare function _finally<T>(this: Observable<T>, callback: () => void): Observable<T>;