delayWhen.js
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
export function delayWhen(delayDurationSelector, subscriptionDelay) {
if (subscriptionDelay) {
return (source) => new SubscriptionDelayObservable(source, subscriptionDelay)
.lift(new DelayWhenOperator(delayDurationSelector));
}
return (source) => source.lift(new DelayWhenOperator(delayDurationSelector));
}
class DelayWhenOperator {
constructor(delayDurationSelector) {
this.delayDurationSelector = delayDurationSelector;
}
call(subscriber, source) {
return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
}
}
class DelayWhenSubscriber extends OuterSubscriber {
constructor(destination, delayDurationSelector) {
super(destination);
this.delayDurationSelector = delayDurationSelector;
this.completed = false;
this.delayNotifierSubscriptions = [];
this.index = 0;
}
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) {
const index = this.index++;
try {
const delayNotifier = this.delayDurationSelector(value, index);
if (delayNotifier) {
this.tryDelay(delayNotifier, value);
}
}
catch (err) {
this.destination.error(err);
}
}
_complete() {
this.completed = true;
this.tryComplete();
this.unsubscribe();
}
removeSubscription(subscription) {
subscription.unsubscribe();
const subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
if (subscriptionIdx !== -1) {
this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
}
return subscription.outerValue;
}
tryDelay(delayNotifier, value) {
const notifierSubscription = subscribeToResult(this, delayNotifier, value);
if (notifierSubscription && !notifierSubscription.closed) {
const destination = this.destination;
destination.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
}
}
tryComplete() {
if (this.completed && this.delayNotifierSubscriptions.length === 0) {
this.destination.complete();
}
}
}
class SubscriptionDelayObservable extends Observable {
constructor(source, subscriptionDelay) {
super();
this.source = source;
this.subscriptionDelay = subscriptionDelay;
}
_subscribe(subscriber) {
this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
}
}
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.unsubscribe();
this.subscribeToSource();
}
subscribeToSource() {
if (!this.sourceSubscribed) {
this.sourceSubscribed = true;
this.unsubscribe();
this.source.subscribe(this.parent);
}
}
}
//# sourceMappingURL=delayWhen.js.map