@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
171 lines • 6.78 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Subscription_1 = require("../Subscription");
var subscribeToResult_1 = require("../util/subscribeToResult");
var OuterSubscriber_1 = require("../OuterSubscriber");
/**
* Buffers the source Observable values starting from an emission from
* `openings` and ending when the output of `closingSelector` emits.
*
* <span class="informal">Collects values from the past as an array. Starts
* collecting only when `opening` emits, and calls the `closingSelector`
* function to get an Observable that tells when to close the buffer.</span>
*
* 
*
* Buffers values from the source by opening the buffer via signals from an
* Observable provided to `openings`, and closing and sending the buffers when
* a Subscribable or Promise returned by the `closingSelector` function emits.
*
* ## Example
*
* Every other second, emit the click events from the next 500ms
*
* ```ts
* import { fromEvent, interval, EMPTY } from 'rxjs';
* import { bufferToggle } from 'rxjs/operators';
*
* const clicks = fromEvent(document, 'click');
* const openings = interval(1000);
* const buffered = clicks.pipe(bufferToggle(openings, i =>
* i % 2 ? interval(500) : EMPTY
* ));
* buffered.subscribe(x => console.log(x));
* ```
*
* @see {@link buffer}
* @see {@link bufferCount}
* @see {@link bufferTime}
* @see {@link bufferWhen}
* @see {@link windowToggle}
*
* @param {SubscribableOrPromise<O>} openings A Subscribable or Promise of notifications to start new
* buffers.
* @param {function(value: O): SubscribableOrPromise} closingSelector A function that takes
* the value emitted by the `openings` observable and returns a Subscribable or Promise,
* which, when it emits, signals that the associated buffer should be emitted
* and cleared.
* @return {Observable<T[]>} An observable of arrays of buffered values.
* @method bufferToggle
* @owner Observable
*/
function bufferToggle(openings, closingSelector) {
return function bufferToggleOperatorFunction(source) {
return source.lift(new BufferToggleOperator(openings, closingSelector));
};
}
exports.bufferToggle = bufferToggle;
var BufferToggleOperator = /** @class */ (function () {
function BufferToggleOperator(openings, closingSelector) {
this.openings = openings;
this.closingSelector = closingSelector;
}
BufferToggleOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
};
return BufferToggleOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var BufferToggleSubscriber = /** @class */ (function (_super) {
__extends(BufferToggleSubscriber, _super);
function BufferToggleSubscriber(destination, openings, closingSelector) {
var _this = _super.call(this, destination) || this;
_this.closingSelector = closingSelector;
_this.contexts = [];
_this.add(subscribeToResult_1.subscribeToResult(_this, openings));
return _this;
}
BufferToggleSubscriber.prototype._next = function (value) {
var contexts = this.contexts;
var len = contexts.length;
for (var i = 0; i < len; i++) {
contexts[i].buffer.push(value);
}
};
BufferToggleSubscriber.prototype._error = function (err) {
var contexts = this.contexts;
while (contexts.length > 0) {
var context_1 = contexts.shift();
context_1.subscription.unsubscribe();
context_1.buffer = null;
context_1.subscription = null;
}
this.contexts = null;
_super.prototype._error.call(this, err);
};
BufferToggleSubscriber.prototype._complete = function () {
var contexts = this.contexts;
while (contexts.length > 0) {
var context_2 = contexts.shift();
this.destination.next(context_2.buffer);
context_2.subscription.unsubscribe();
context_2.buffer = null;
context_2.subscription = null;
}
this.contexts = null;
_super.prototype._complete.call(this);
};
BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue) {
outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
};
BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
this.closeBuffer(innerSub.context);
};
BufferToggleSubscriber.prototype.openBuffer = function (value) {
try {
var closingSelector = this.closingSelector;
var closingNotifier = closingSelector.call(this, value);
if (closingNotifier) {
this.trySubscribe(closingNotifier);
}
}
catch (err) {
this._error(err);
}
};
BufferToggleSubscriber.prototype.closeBuffer = function (context) {
var contexts = this.contexts;
if (contexts && context) {
var buffer = context.buffer, subscription = context.subscription;
this.destination.next(buffer);
contexts.splice(contexts.indexOf(context), 1);
this.remove(subscription);
subscription.unsubscribe();
}
};
BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
var contexts = this.contexts;
var buffer = [];
var subscription = new Subscription_1.Subscription();
var context = { buffer: buffer, subscription: subscription };
contexts.push(context);
var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context);
if (!innerSubscription || innerSubscription.closed) {
this.closeBuffer(context);
}
else {
innerSubscription.context = context;
this.add(innerSubscription);
subscription.add(innerSubscription);
}
};
return BufferToggleSubscriber;
}(OuterSubscriber_1.OuterSubscriber));
//# sourceMappingURL=bufferToggle.js.map