pairwise.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. * Groups pairs of consecutive emissions together and emits them as an array of
  6. * two values.
  7. *
  8. * <span class="informal">Puts the current value and previous value together as
  9. * an array, and emits that.</span>
  10. *
  11. * <img src="./img/pairwise.png" width="100%">
  12. *
  13. * The Nth emission from the source Observable will cause the output Observable
  14. * to emit an array [(N-1)th, Nth] of the previous and the current value, as a
  15. * pair. For this reason, `pairwise` emits on the second and subsequent
  16. * emissions from the source Observable, but not on the first emission, because
  17. * there is no previous value in that case.
  18. *
  19. * @example <caption>On every click (starting from the second), emit the relative distance to the previous click</caption>
  20. * var clicks = Rx.Observable.fromEvent(document, 'click');
  21. * var pairs = clicks.pairwise();
  22. * var distance = pairs.map(pair => {
  23. * var x0 = pair[0].clientX;
  24. * var y0 = pair[0].clientY;
  25. * var x1 = pair[1].clientX;
  26. * var y1 = pair[1].clientY;
  27. * return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
  28. * });
  29. * distance.subscribe(x => console.log(x));
  30. *
  31. * @see {@link buffer}
  32. * @see {@link bufferCount}
  33. *
  34. * @return {Observable<Array<T>>} An Observable of pairs (as arrays) of
  35. * consecutive values from the source Observable.
  36. * @method pairwise
  37. * @owner Observable
  38. */
  39. function pairwise() {
  40. return operators_1.pairwise()(this);
  41. }
  42. exports.pairwise = pairwise;
  43. //# sourceMappingURL=pairwise.js.map