es6/operator/delayWhen.js
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
/**
* 在给定的时间范围内,延迟源 Observable 所有数据项的发送,该时间段由另一个 Observable 的发送决定。
*
* <span class="informal">就像是{@link delay}, 但是延时的时间间隔由第二个Observable决定.</span>
*
* <img src="./img/delayWhen.png" width="100%">
*
* `delayWhen` 通过由另一个 Observable 决定的时间段来延迟源 Observable 的每个发出值。
* 当源发出一个数据,`delayDurationSelector` 函数将该源值当做参数, 返回一个被称为“持续”的 Observable。
* 当且仅当持续的 Observable 发出或完成时,源值才会在输出 Observable 上发出。
*
* 可选的, `delayWhen` 接受第二个参数, `subscriptionDelay`,它是一个 Observable。
* 当 `subscriptionDelay` 发出第一个值或者完成,源 Observable 被订阅并且开始像前一段描
* 述的一样。 如果 `subscriptionDelay` 没有提供,`delayWhen` 将会订阅源 Observable 只
* 要输出 Observable 被订阅。
*
* @example <caption>将每次点击延迟0到5秒的随机时间</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var delayedClicks = clicks.delayWhen(event =>
* Rx.Observable.interval(Math.random() * 5000)
* );
* delayedClicks.subscribe(x => console.log(x));
*
* @see {@link debounce}
* @see {@link delay}
*
* @param {function(value: T): Observable} delayDurationSelector 该函数为源 Observable
* 发出的每个值返回一个 Observable,用于延迟在输出 Observable 上的发送,直到返回的 Observable 发出值。
* @param {Observable} subscriptionDelay Observable,该 Observable 一旦发出任何值则触发源 Observable 的订阅。
* @return {Observable} Observable,延时源Observable的发出时间,该时间由`delayDurationSelector`
* 返回的Observable决定.
* @method delayWhen
* @owner Observable
*/
export function delayWhen(delayDurationSelector, subscriptionDelay) {
if (subscriptionDelay) {
return new SubscriptionDelayObservable(this, subscriptionDelay)
.lift(new DelayWhenOperator(delayDurationSelector));
}
return this.lift(new DelayWhenOperator(delayDurationSelector));
}
class DelayWhenOperator {
constructor(delayDurationSelector) {
this.delayDurationSelector = delayDurationSelector;
}
call(subscriber, source) {
return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class DelayWhenSubscriber extends OuterSubscriber {
constructor(destination, delayDurationSelector) {
super(destination);
this.delayDurationSelector = delayDurationSelector;
this.completed = false;
this.delayNotifierSubscriptions = [];
this.values = [];
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.destination.next(outerValue);
this.removeSubscription(innerSub);
this.tryComplete();
}
notifyError(error, innerSub) {
this._error(error);
}
notifyComplete(innerSub) {
const value = this.removeSubscription(innerSub);
if (value) {
this.destination.next(value);
}
this.tryComplete();
}
_next(value) {
try {
const delayNotifier = this.delayDurationSelector(value);
if (delayNotifier) {
this.tryDelay(delayNotifier, value);
}
}
catch (err) {
this.destination.error(err);
}
}
_complete() {
this.completed = true;
this.tryComplete();
}
removeSubscription(subscription) {
subscription.unsubscribe();
const subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
let value = null;
if (subscriptionIdx !== -1) {
value = this.values[subscriptionIdx];
this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
this.values.splice(subscriptionIdx, 1);
}
return value;
}
tryDelay(delayNotifier, value) {
const notifierSubscription = subscribeToResult(this, delayNotifier, value);
if (notifierSubscription && !notifierSubscription.closed) {
this.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
}
this.values.push(value);
}
tryComplete() {
if (this.completed && this.delayNotifierSubscriptions.length === 0) {
this.destination.complete();
}
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SubscriptionDelayObservable extends Observable {
constructor(source, subscriptionDelay) {
super();
this.source = source;
this.subscriptionDelay = subscriptionDelay;
}
_subscribe(subscriber) {
this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SubscriptionDelaySubscriber extends Subscriber {
constructor(parent, source) {
super();
this.parent = parent;
this.source = source;
this.sourceSubscribed = false;
}
_next(unused) {
this.subscribeToSource();
}
_error(err) {
this.unsubscribe();
this.parent.error(err);
}
_complete() {
this.subscribeToSource();
}
subscribeToSource() {
if (!this.sourceSubscribed) {
this.sourceSubscribed = true;
this.unsubscribe();
this.source.subscribe(this.parent);
}
}
}
//# sourceMappingURL=delayWhen.js.map