tap.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** PURE_IMPORTS_START .._Subscriber PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscriber } from '../Subscriber';
  10. /* tslint:enable:max-line-length */
  11. /**
  12. * Perform a side effect for every emission on the source Observable, but return
  13. * an Observable that is identical to the source.
  14. *
  15. * <span class="informal">Intercepts each emission on the source and runs a
  16. * function, but returns an output which is identical to the source as long as errors don't occur.</span>
  17. *
  18. * <img src="./img/do.png" width="100%">
  19. *
  20. * Returns a mirrored Observable of the source Observable, but modified so that
  21. * the provided Observer is called to perform a side effect for every value,
  22. * error, and completion emitted by the source. Any errors that are thrown in
  23. * the aforementioned Observer or handlers are safely sent down the error path
  24. * of the output Observable.
  25. *
  26. * This operator is useful for debugging your Observables for the correct values
  27. * or performing other side effects.
  28. *
  29. * Note: this is different to a `subscribe` on the Observable. If the Observable
  30. * returned by `do` is not subscribed, the side effects specified by the
  31. * Observer will never happen. `do` therefore simply spies on existing
  32. * execution, it does not trigger an execution to happen like `subscribe` does.
  33. *
  34. * @example <caption>Map every click to the clientX position of that click, while also logging the click event</caption>
  35. * var clicks = Rx.Observable.fromEvent(document, 'click');
  36. * var positions = clicks
  37. * .do(ev => console.log(ev))
  38. * .map(ev => ev.clientX);
  39. * positions.subscribe(x => console.log(x));
  40. *
  41. * @see {@link map}
  42. * @see {@link subscribe}
  43. *
  44. * @param {Observer|function} [nextOrObserver] A normal Observer object or a
  45. * callback for `next`.
  46. * @param {function} [error] Callback for errors in the source.
  47. * @param {function} [complete] Callback for the completion of the source.
  48. * @return {Observable} An Observable identical to the source, but runs the
  49. * specified Observer or callback(s) for each item.
  50. * @name tap
  51. */
  52. export function tap(nextOrObserver, error, complete) {
  53. return function tapOperatorFunction(source) {
  54. return source.lift(new DoOperator(nextOrObserver, error, complete));
  55. };
  56. }
  57. var DoOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  58. function DoOperator(nextOrObserver, error, complete) {
  59. this.nextOrObserver = nextOrObserver;
  60. this.error = error;
  61. this.complete = complete;
  62. }
  63. DoOperator.prototype.call = function (subscriber, source) {
  64. return source.subscribe(new DoSubscriber(subscriber, this.nextOrObserver, this.error, this.complete));
  65. };
  66. return DoOperator;
  67. }());
  68. /**
  69. * We need this JSDoc comment for affecting ESDoc.
  70. * @ignore
  71. * @extends {Ignored}
  72. */
  73. var DoSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  74. __extends(DoSubscriber, _super);
  75. function DoSubscriber(destination, nextOrObserver, error, complete) {
  76. _super.call(this, destination);
  77. var safeSubscriber = new Subscriber(nextOrObserver, error, complete);
  78. safeSubscriber.syncErrorThrowable = true;
  79. this.add(safeSubscriber);
  80. this.safeSubscriber = safeSubscriber;
  81. }
  82. DoSubscriber.prototype._next = function (value) {
  83. var safeSubscriber = this.safeSubscriber;
  84. safeSubscriber.next(value);
  85. if (safeSubscriber.syncErrorThrown) {
  86. this.destination.error(safeSubscriber.syncErrorValue);
  87. }
  88. else {
  89. this.destination.next(value);
  90. }
  91. };
  92. DoSubscriber.prototype._error = function (err) {
  93. var safeSubscriber = this.safeSubscriber;
  94. safeSubscriber.error(err);
  95. if (safeSubscriber.syncErrorThrown) {
  96. this.destination.error(safeSubscriber.syncErrorValue);
  97. }
  98. else {
  99. this.destination.error(err);
  100. }
  101. };
  102. DoSubscriber.prototype._complete = function () {
  103. var safeSubscriber = this.safeSubscriber;
  104. safeSubscriber.complete();
  105. if (safeSubscriber.syncErrorThrown) {
  106. this.destination.error(safeSubscriber.syncErrorValue);
  107. }
  108. else {
  109. this.destination.complete();
  110. }
  111. };
  112. return DoSubscriber;
  113. }(Subscriber));
  114. //# sourceMappingURL=tap.js.map