do.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Perform a side effect for every emission on the source Observable, but return
  7. * an Observable that is identical to the source.
  8. *
  9. * <span class="informal">Intercepts each emission on the source and runs a
  10. * function, but returns an output which is identical to the source as long as errors don't occur.</span>
  11. *
  12. * <img src="./img/do.png" width="100%">
  13. *
  14. * Returns a mirrored Observable of the source Observable, but modified so that
  15. * the provided Observer is called to perform a side effect for every value,
  16. * error, and completion emitted by the source. Any errors that are thrown in
  17. * the aforementioned Observer or handlers are safely sent down the error path
  18. * of the output Observable.
  19. *
  20. * This operator is useful for debugging your Observables for the correct values
  21. * or performing other side effects.
  22. *
  23. * Note: this is different to a `subscribe` on the Observable. If the Observable
  24. * returned by `do` is not subscribed, the side effects specified by the
  25. * Observer will never happen. `do` therefore simply spies on existing
  26. * execution, it does not trigger an execution to happen like `subscribe` does.
  27. *
  28. * @example <caption>Map every click to the clientX position of that click, while also logging the click event</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var positions = clicks
  31. * .do(ev => console.log(ev))
  32. * .map(ev => ev.clientX);
  33. * positions.subscribe(x => console.log(x));
  34. *
  35. * @see {@link map}
  36. * @see {@link subscribe}
  37. *
  38. * @param {Observer|function} [nextOrObserver] A normal Observer object or a
  39. * callback for `next`.
  40. * @param {function} [error] Callback for errors in the source.
  41. * @param {function} [complete] Callback for the completion of the source.
  42. * @return {Observable} An Observable identical to the source, but runs the
  43. * specified Observer or callback(s) for each item.
  44. * @method do
  45. * @name do
  46. * @owner Observable
  47. */
  48. function _do(nextOrObserver, error, complete) {
  49. return operators_1.tap(nextOrObserver, error, complete)(this);
  50. }
  51. exports._do = _do;
  52. //# sourceMappingURL=do.js.map