es6/operator/windowWhen.js
import { Subject } from '../Subject';
import { tryCatch } from '../util/tryCatch';
import { errorObject } from '../util/errorObject';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
/**
* 将源 Observable 的值分支成嵌套的 Observable ,通过使用关闭 Observable 的工厂函数来决定何时开启新的窗口。
*
* <span class="informal">就像是 {@link bufferWhen}, 但是发出的是嵌套的 Observable
* 而不是数组。</span>
*
* <img src="./img/windowWhen.png" width="100%">
*
* 返回的 Observable 发出从源 Observable 收集到的项的窗口。 输出 Observable 发出连接的,非重叠的窗口。
* 每当由指定的 closingSelector 函数产生的 Observable 发出项,它会发出当前窗口并开启一个新窗口。
* 当输出 Observable 被订阅的时候立马开启第一个窗口。
*
* @example <caption>在每个秒速随机(1-5秒)的窗口中,只发出最开始的两次点击事件</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks
* .windowWhen(() => Rx.Observable.interval(1000 + Math.random() * 4000))
* .map(win => win.take(2)) // 每个窗口最多两个发送
* .mergeAll(); // 打平高阶Observable
* 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 函数,不接受参数并且返回 Observable,
* 该 Observable 发出信号(`next` 或者 `complete`)以决定何时关闭前一个窗口,开启新一个窗口。
* @return {Observable<Observable<T>>} 窗口的 Observable,每个窗口又是值的 Observable(译者注:其实就是高阶 Observable )。
* @method windowWhen
* @owner Observable
*/
export function windowWhen(closingSelector) {
return this.lift(new WindowOperator(closingSelector));
}
class WindowOperator {
constructor(closingSelector) {
this.closingSelector = closingSelector;
}
call(subscriber, source) {
return source.subscribe(new WindowSubscriber(subscriber, this.closingSelector));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class WindowSubscriber extends OuterSubscriber {
constructor(destination, closingSelector) {
super(destination);
this.destination = destination;
this.closingSelector = closingSelector;
this.openWindow();
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.openWindow(innerSub);
}
notifyError(error, innerSub) {
this._error(error);
}
notifyComplete(innerSub) {
this.openWindow(innerSub);
}
_next(value) {
this.window.next(value);
}
_error(err) {
this.window.error(err);
this.destination.error(err);
this.unsubscribeClosingNotification();
}
_complete() {
this.window.complete();
this.destination.complete();
this.unsubscribeClosingNotification();
}
unsubscribeClosingNotification() {
if (this.closingNotification) {
this.closingNotification.unsubscribe();
}
}
openWindow(innerSub = null) {
if (innerSub) {
this.remove(innerSub);
innerSub.unsubscribe();
}
const prevWindow = this.window;
if (prevWindow) {
prevWindow.complete();
}
const window = this.window = new Subject();
this.destination.next(window);
const closingNotifier = tryCatch(this.closingSelector)();
if (closingNotifier === errorObject) {
const err = errorObject.e;
this.destination.error(err);
this.window.error(err);
}
else {
this.add(this.closingNotification = subscribeToResult(this, closingNotifier));
}
}
}
//# sourceMappingURL=windowWhen.js.map