@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
146 lines • 5.57 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 Subject_1 = require("../Subject");
var OuterSubscriber_1 = require("../OuterSubscriber");
var subscribeToResult_1 = require("../util/subscribeToResult");
/**
* Branch out the source Observable values as a nested Observable using a
* factory function of closing Observables to determine when to start a new
* window.
*
* <span class="informal">It's like {@link bufferWhen}, but emits a nested
* Observable instead of an array.</span>
*
* 
*
* Returns an Observable that emits windows of items it collects from the source
* Observable. The output Observable emits connected, non-overlapping windows.
* It emits the current window and opens a new one whenever the Observable
* produced by the specified `closingSelector` function emits an item. The first
* window is opened immediately when subscribing to the output Observable.
*
* ## Example
* Emit only the first two clicks events in every window of [1-5] random seconds
* ```ts
* import { fromEvent, interval } from 'rxjs';
* import { windowWhen, map, mergeAll, take } from 'rxjs/operators';
*
* const clicks = fromEvent(document, 'click');
* const result = clicks.pipe(
* windowWhen(() => interval(1000 + Math.random() * 4000)),
* map(win => win.pipe(take(2))), // each window has at most 2 emissions
* mergeAll() // flatten the Observable-of-Observables
* );
* result.subscribe(x => console.log(x));
* ```
*
* @see {@link window}
* @see {@link windowCount}
* @see {@link windowTime}
* @see {@link windowToggle}
* @see {@link bufferWhen}
*
* @param {function(): Observable} closingSelector A function that takes no
* arguments and returns an Observable that signals (on either `next` or
* `complete`) when to close the previous window and start a new one.
* @return {Observable<Observable<T>>} An observable of windows, which in turn
* are Observables.
* @method windowWhen
* @owner Observable
*/
function windowWhen(closingSelector) {
return function windowWhenOperatorFunction(source) {
return source.lift(new WindowOperator(closingSelector));
};
}
exports.windowWhen = windowWhen;
var WindowOperator = /** @class */ (function () {
function WindowOperator(closingSelector) {
this.closingSelector = closingSelector;
}
WindowOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new WindowSubscriber(subscriber, this.closingSelector));
};
return WindowOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var WindowSubscriber = /** @class */ (function (_super) {
__extends(WindowSubscriber, _super);
function WindowSubscriber(destination, closingSelector) {
var _this = _super.call(this, destination) || this;
_this.destination = destination;
_this.closingSelector = closingSelector;
_this.openWindow();
return _this;
}
WindowSubscriber.prototype.notifyNext = function (_outerValue, _innerValue, _outerIndex, _innerIndex, innerSub) {
this.openWindow(innerSub);
};
WindowSubscriber.prototype.notifyError = function (error) {
this._error(error);
};
WindowSubscriber.prototype.notifyComplete = function (innerSub) {
this.openWindow(innerSub);
};
WindowSubscriber.prototype._next = function (value) {
this.window.next(value);
};
WindowSubscriber.prototype._error = function (err) {
this.window.error(err);
this.destination.error(err);
this.unsubscribeClosingNotification();
};
WindowSubscriber.prototype._complete = function () {
this.window.complete();
this.destination.complete();
this.unsubscribeClosingNotification();
};
WindowSubscriber.prototype.unsubscribeClosingNotification = function () {
if (this.closingNotification) {
this.closingNotification.unsubscribe();
}
};
WindowSubscriber.prototype.openWindow = function (innerSub) {
if (innerSub === void 0) { innerSub = null; }
if (innerSub) {
this.remove(innerSub);
innerSub.unsubscribe();
}
var prevWindow = this.window;
if (prevWindow) {
prevWindow.complete();
}
var window = this.window = new Subject_1.Subject();
this.destination.next(window);
var closingNotifier;
try {
var closingSelector = this.closingSelector;
closingNotifier = closingSelector();
}
catch (e) {
this.destination.error(e);
this.window.error(e);
return;
}
this.add(this.closingNotification = subscribeToResult_1.subscribeToResult(this, closingNotifier));
};
return WindowSubscriber;
}(OuterSubscriber_1.OuterSubscriber));
//# sourceMappingURL=windowWhen.js.map