pairwise.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  11. * Groups pairs of consecutive emissions together and emits them as an array of
  12. * two values.
  13. *
  14. * <span class="informal">Puts the current value and previous value together as
  15. * an array, and emits that.</span>
  16. *
  17. * <img src="./img/pairwise.png" width="100%">
  18. *
  19. * The Nth emission from the source Observable will cause the output Observable
  20. * to emit an array [(N-1)th, Nth] of the previous and the current value, as a
  21. * pair. For this reason, `pairwise` emits on the second and subsequent
  22. * emissions from the source Observable, but not on the first emission, because
  23. * there is no previous value in that case.
  24. *
  25. * @example <caption>On every click (starting from the second), emit the relative distance to the previous click</caption>
  26. * var clicks = Rx.Observable.fromEvent(document, 'click');
  27. * var pairs = clicks.pairwise();
  28. * var distance = pairs.map(pair => {
  29. * var x0 = pair[0].clientX;
  30. * var y0 = pair[0].clientY;
  31. * var x1 = pair[1].clientX;
  32. * var y1 = pair[1].clientY;
  33. * return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
  34. * });
  35. * distance.subscribe(x => console.log(x));
  36. *
  37. * @see {@link buffer}
  38. * @see {@link bufferCount}
  39. *
  40. * @return {Observable<Array<T>>} An Observable of pairs (as arrays) of
  41. * consecutive values from the source Observable.
  42. * @method pairwise
  43. * @owner Observable
  44. */
  45. export function pairwise() {
  46. return function (source) { return source.lift(new PairwiseOperator()); };
  47. }
  48. var PairwiseOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  49. function PairwiseOperator() {
  50. }
  51. PairwiseOperator.prototype.call = function (subscriber, source) {
  52. return source.subscribe(new PairwiseSubscriber(subscriber));
  53. };
  54. return PairwiseOperator;
  55. }());
  56. /**
  57. * We need this JSDoc comment for affecting ESDoc.
  58. * @ignore
  59. * @extends {Ignored}
  60. */
  61. var PairwiseSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  62. __extends(PairwiseSubscriber, _super);
  63. function PairwiseSubscriber(destination) {
  64. _super.call(this, destination);
  65. this.hasPrev = false;
  66. }
  67. PairwiseSubscriber.prototype._next = function (value) {
  68. if (this.hasPrev) {
  69. this.destination.next([this.prev, value]);
  70. }
  71. else {
  72. this.hasPrev = true;
  73. }
  74. this.prev = value;
  75. };
  76. return PairwiseSubscriber;
  77. }(Subscriber));
  78. //# sourceMappingURL=pairwise.js.map