pairwise.js 2.6 KB

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