finally.js 3.3 KB

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