Skip to content

Latest commit

 

History

History
87 lines (67 loc) · 2.48 KB

pairwise.md

File metadata and controls

87 lines (67 loc) · 2.48 KB

Rx.Observable.prototype.pairwise()

Triggers on the second and subsequent triggerings of the input observable. The Nth triggering of the input observable passes the arguments from the N-1th and Nth triggering as a pair.

Returns

(Observable): An observable that triggers on successive pairs of observations from the input observable as an array.

Example

var r = Rx.Observable.range(1, 4);

var source = r.pairwise();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + JSON.stringify(x));
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: [1,2]
// => Next: [2,3]
// => Next: [3,4]
// => Completed

Example (Draw line)

<canvas id="canvas" width="600" height="600"/>
var canvas = document.getElementById("canvas");
var g = canvas.getContext("2d");
g.rect(0, 0, canvas.width, canvas.height);
g.fillStyle = "rgb(0,0,0)";
g.fill();

var mouseMove = Rx.Observable.fromEvent(document, 'mousemove');
var mouseDown = Rx.Observable.fromEvent(document.getElementById('canvas'), 'mousedown');
var mouseUp = Rx.Observable.fromEvent(document.getElementById('canvas'), 'mouseup');

mouseDown.flatMap(function(ev) {  
  return mouseMove.map(function(ev) {
    return {
      x: ev.clientX,
      y: ev.clientY
    };
  }).pairwise().takeUntil(mouseUp);
  
}).subscribe(function(pos) {
  g.beginPath();
  g.lineWidth = 1;
  
  g.strokeStyle = "rgb(255, 0, 0)";
  
  g.moveTo(pos[0].x, pos[0].y);
  g.lineTo(pos[1].x, pos[1].y);
  
  g.stroke();
});

Location

File:

Dist:

NPM Packages:

NuGet Packages:

Unit Tests: