delayWhen.js
5.81 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
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 Subscriber_1 = require("../Subscriber");
var Observable_1 = require("../Observable");
var OuterSubscriber_1 = require("../OuterSubscriber");
var subscribeToResult_1 = require("../util/subscribeToResult");
function delayWhen(delayDurationSelector, subscriptionDelay) {
if (subscriptionDelay) {
return function (source) {
return new SubscriptionDelayObservable(source, subscriptionDelay)
.lift(new DelayWhenOperator(delayDurationSelector));
};
}
return function (source) { return source.lift(new DelayWhenOperator(delayDurationSelector)); };
}
exports.delayWhen = delayWhen;
var DelayWhenOperator = (function () {
function DelayWhenOperator(delayDurationSelector) {
this.delayDurationSelector = delayDurationSelector;
}
DelayWhenOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
};
return DelayWhenOperator;
}());
var DelayWhenSubscriber = (function (_super) {
__extends(DelayWhenSubscriber, _super);
function DelayWhenSubscriber(destination, delayDurationSelector) {
var _this = _super.call(this, destination) || this;
_this.delayDurationSelector = delayDurationSelector;
_this.completed = false;
_this.delayNotifierSubscriptions = [];
_this.index = 0;
return _this;
}
DelayWhenSubscriber.prototype.notifyNext = function (outerValue, _innerValue, _outerIndex, _innerIndex, innerSub) {
this.destination.next(outerValue);
this.removeSubscription(innerSub);
this.tryComplete();
};
DelayWhenSubscriber.prototype.notifyError = function (error, innerSub) {
this._error(error);
};
DelayWhenSubscriber.prototype.notifyComplete = function (innerSub) {
var value = this.removeSubscription(innerSub);
if (value) {
this.destination.next(value);
}
this.tryComplete();
};
DelayWhenSubscriber.prototype._next = function (value) {
var index = this.index++;
try {
var delayNotifier = this.delayDurationSelector(value, index);
if (delayNotifier) {
this.tryDelay(delayNotifier, value);
}
}
catch (err) {
this.destination.error(err);
}
};
DelayWhenSubscriber.prototype._complete = function () {
this.completed = true;
this.tryComplete();
this.unsubscribe();
};
DelayWhenSubscriber.prototype.removeSubscription = function (subscription) {
subscription.unsubscribe();
var subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
if (subscriptionIdx !== -1) {
this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
}
return subscription.outerValue;
};
DelayWhenSubscriber.prototype.tryDelay = function (delayNotifier, value) {
var notifierSubscription = subscribeToResult_1.subscribeToResult(this, delayNotifier, value);
if (notifierSubscription && !notifierSubscription.closed) {
var destination = this.destination;
destination.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
}
};
DelayWhenSubscriber.prototype.tryComplete = function () {
if (this.completed && this.delayNotifierSubscriptions.length === 0) {
this.destination.complete();
}
};
return DelayWhenSubscriber;
}(OuterSubscriber_1.OuterSubscriber));
var SubscriptionDelayObservable = (function (_super) {
__extends(SubscriptionDelayObservable, _super);
function SubscriptionDelayObservable(source, subscriptionDelay) {
var _this = _super.call(this) || this;
_this.source = source;
_this.subscriptionDelay = subscriptionDelay;
return _this;
}
SubscriptionDelayObservable.prototype._subscribe = function (subscriber) {
this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
};
return SubscriptionDelayObservable;
}(Observable_1.Observable));
var SubscriptionDelaySubscriber = (function (_super) {
__extends(SubscriptionDelaySubscriber, _super);
function SubscriptionDelaySubscriber(parent, source) {
var _this = _super.call(this) || this;
_this.parent = parent;
_this.source = source;
_this.sourceSubscribed = false;
return _this;
}
SubscriptionDelaySubscriber.prototype._next = function (unused) {
this.subscribeToSource();
};
SubscriptionDelaySubscriber.prototype._error = function (err) {
this.unsubscribe();
this.parent.error(err);
};
SubscriptionDelaySubscriber.prototype._complete = function () {
this.unsubscribe();
this.subscribeToSource();
};
SubscriptionDelaySubscriber.prototype.subscribeToSource = function () {
if (!this.sourceSubscribed) {
this.sourceSubscribed = true;
this.unsubscribe();
this.source.subscribe(this.parent);
}
};
return SubscriptionDelaySubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=delayWhen.js.map