tap.js 4.4 KB

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