groupBy.js
6.59 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/** PURE_IMPORTS_START tslib,_Subscriber,_Subscription,_Observable,_Subject PURE_IMPORTS_END */
import * as tslib_1 from "tslib";
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { Observable } from '../Observable';
import { Subject } from '../Subject';
export function groupBy(keySelector, elementSelector, durationSelector, subjectSelector) {
return function (source) {
return source.lift(new GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector));
};
}
var GroupByOperator = /*@__PURE__*/ (function () {
function GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector) {
this.keySelector = keySelector;
this.elementSelector = elementSelector;
this.durationSelector = durationSelector;
this.subjectSelector = subjectSelector;
}
GroupByOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new GroupBySubscriber(subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector));
};
return GroupByOperator;
}());
var GroupBySubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(GroupBySubscriber, _super);
function GroupBySubscriber(destination, keySelector, elementSelector, durationSelector, subjectSelector) {
var _this = _super.call(this, destination) || this;
_this.keySelector = keySelector;
_this.elementSelector = elementSelector;
_this.durationSelector = durationSelector;
_this.subjectSelector = subjectSelector;
_this.groups = null;
_this.attemptedToUnsubscribe = false;
_this.count = 0;
return _this;
}
GroupBySubscriber.prototype._next = function (value) {
var key;
try {
key = this.keySelector(value);
}
catch (err) {
this.error(err);
return;
}
this._group(value, key);
};
GroupBySubscriber.prototype._group = function (value, key) {
var groups = this.groups;
if (!groups) {
groups = this.groups = new Map();
}
var group = groups.get(key);
var element;
if (this.elementSelector) {
try {
element = this.elementSelector(value);
}
catch (err) {
this.error(err);
}
}
else {
element = value;
}
if (!group) {
group = (this.subjectSelector ? this.subjectSelector() : new Subject());
groups.set(key, group);
var groupedObservable = new GroupedObservable(key, group, this);
this.destination.next(groupedObservable);
if (this.durationSelector) {
var duration = void 0;
try {
duration = this.durationSelector(new GroupedObservable(key, group));
}
catch (err) {
this.error(err);
return;
}
this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
}
}
if (!group.closed) {
group.next(element);
}
};
GroupBySubscriber.prototype._error = function (err) {
var groups = this.groups;
if (groups) {
groups.forEach(function (group, key) {
group.error(err);
});
groups.clear();
}
this.destination.error(err);
};
GroupBySubscriber.prototype._complete = function () {
var groups = this.groups;
if (groups) {
groups.forEach(function (group, key) {
group.complete();
});
groups.clear();
}
this.destination.complete();
};
GroupBySubscriber.prototype.removeGroup = function (key) {
this.groups.delete(key);
};
GroupBySubscriber.prototype.unsubscribe = function () {
if (!this.closed) {
this.attemptedToUnsubscribe = true;
if (this.count === 0) {
_super.prototype.unsubscribe.call(this);
}
}
};
return GroupBySubscriber;
}(Subscriber));
var GroupDurationSubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(GroupDurationSubscriber, _super);
function GroupDurationSubscriber(key, group, parent) {
var _this = _super.call(this, group) || this;
_this.key = key;
_this.group = group;
_this.parent = parent;
return _this;
}
GroupDurationSubscriber.prototype._next = function (value) {
this.complete();
};
GroupDurationSubscriber.prototype._unsubscribe = function () {
var _a = this, parent = _a.parent, key = _a.key;
this.key = this.parent = null;
if (parent) {
parent.removeGroup(key);
}
};
return GroupDurationSubscriber;
}(Subscriber));
var GroupedObservable = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(GroupedObservable, _super);
function GroupedObservable(key, groupSubject, refCountSubscription) {
var _this = _super.call(this) || this;
_this.key = key;
_this.groupSubject = groupSubject;
_this.refCountSubscription = refCountSubscription;
return _this;
}
GroupedObservable.prototype._subscribe = function (subscriber) {
var subscription = new Subscription();
var _a = this, refCountSubscription = _a.refCountSubscription, groupSubject = _a.groupSubject;
if (refCountSubscription && !refCountSubscription.closed) {
subscription.add(new InnerRefCountSubscription(refCountSubscription));
}
subscription.add(groupSubject.subscribe(subscriber));
return subscription;
};
return GroupedObservable;
}(Observable));
export { GroupedObservable };
var InnerRefCountSubscription = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(InnerRefCountSubscription, _super);
function InnerRefCountSubscription(parent) {
var _this = _super.call(this) || this;
_this.parent = parent;
parent.count++;
return _this;
}
InnerRefCountSubscription.prototype.unsubscribe = function () {
var parent = this.parent;
if (!parent.closed && !this.closed) {
_super.prototype.unsubscribe.call(this);
parent.count -= 1;
if (parent.count === 0 && parent.attemptedToUnsubscribe) {
parent.unsubscribe();
}
}
};
return InnerRefCountSubscription;
}(Subscription));
//# sourceMappingURL=groupBy.js.map