UNPKG

rxjs-consecutive-operator

Version:

RxJS operator for mapping multiple consecutively emitted values at once

63 lines (62 loc) 2.53 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); exports.__esModule = true; var rxjs_1 = require("rxjs"); function consecutive(observable, operator, numberOfConsecutiveValues) { var n = getNumberOfConsecutiveValuesToObserve(operator, numberOfConsecutiveValues); return observable.lift(new ConsecutiveOperator(operator, n)); } exports.consecutive = consecutive; function getNumberOfConsecutiveValuesToObserve(operator, numberOfConsecutiveValues) { var n; if (numberOfConsecutiveValues != null) { n = numberOfConsecutiveValues; } else { if (operator.length === 0) { throw new Error('consecutive: could not determine how many values provided operator accepts'); } else { n = operator.length; } } return n; } var ConsecutiveOperator = (function () { function ConsecutiveOperator(operator, numberOfConsecutiveValues) { this.operator = operator; this.numberOfConsecutiveValues = numberOfConsecutiveValues; } ConsecutiveOperator.prototype.call = function (subscriber, source) { source.subscribe(new ConsecutiveSubscriber(subscriber, this.operator, this.numberOfConsecutiveValues)); }; return ConsecutiveOperator; }()); var ConsecutiveSubscriber = (function (_super) { __extends(ConsecutiveSubscriber, _super); function ConsecutiveSubscriber(destination, operator, numberOfConsecutiveValues) { var _this = _super.call(this, destination) || this; _this.destination = destination; _this.operator = operator; _this.numberOfConsecutiveValues = numberOfConsecutiveValues; _this.valuesBuffer = []; return _this; } ConsecutiveSubscriber.prototype._next = function (value) { this.valuesBuffer.push(value); if (this.valuesBuffer.length === this.numberOfConsecutiveValues) { this.destination.next(this.operator.apply(this, this.valuesBuffer)); this.valuesBuffer.shift(); } }; return ConsecutiveSubscriber; }(rxjs_1.Subscriber));