xstream
Version:
An extremely intuitive, small, and fast functional reactive stream library for JavaScript
86 lines • 1.93 kB
JavaScript
"use strict";
var core_1 = require('../core');
var PairwiseOperator = (function () {
function PairwiseOperator(ins) {
this.ins = ins;
this.type = 'pairwise';
this.val = null;
this.has = false;
this.out = null;
}
PairwiseOperator.prototype._start = function (out) {
this.out = out;
this.ins._add(this);
};
PairwiseOperator.prototype._stop = function () {
this.ins._remove(this);
this.has = false;
this.out = null;
this.val = null;
};
PairwiseOperator.prototype._n = function (t) {
var u = this.out;
if (!u)
return;
if (this.has) {
u._n([this.val, t]);
}
this.val = t;
this.has = true;
};
PairwiseOperator.prototype._e = function (err) {
var u = this.out;
if (!u)
return;
u._e(err);
};
PairwiseOperator.prototype._c = function () {
var u = this.out;
if (!u)
return;
u._c();
};
return PairwiseOperator;
}());
/**
* Group consecutive pairs of events as arrays. Each array has two items.
*
* Marble diagram:
*
* ```text
* ---1---2-----3-----4-----5--------|
* pairwise
* -------[1,2]-[2,3]-[3,4]-[4,5]----|
* ```
*
* Example:
*
* ```js
* import pairwise from 'xstream/extra/pairwise'
*
* const stream = xs.of(1, 2, 3, 4, 5, 6).compose(pairwise)
*
* stream.addListener({
* next: i => console.log(i),
* error: err => console.error(err),
* complete: () => console.log('completed')
* })
* ```
*
* ```text
* > [1,2]
* > [2,3]
* > [3,4]
* > [4,5]
* > [5,6]
* > completed
* ```
*
* @return {Stream}
*/
function pairwise(ins) {
return new core_1.Stream(new PairwiseOperator(ins));
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = pairwise;
//# sourceMappingURL=pairwise.js.map