1 /** PURE_IMPORTS_START tslib,_Subscriber,_Subscription,_Observable,_Subject PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { Observable } from '../Observable';
6 import { Subject } from '../Subject';
7 export function groupBy(keySelector, elementSelector, durationSelector, subjectSelector) {
8 return function (source) {
9 return source.lift(new GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector));
12 var GroupByOperator = /*@__PURE__*/ (function () {
13 function GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector) {
14 this.keySelector = keySelector;
15 this.elementSelector = elementSelector;
16 this.durationSelector = durationSelector;
17 this.subjectSelector = subjectSelector;
19 GroupByOperator.prototype.call = function (subscriber, source) {
20 return source.subscribe(new GroupBySubscriber(subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector));
22 return GroupByOperator;
24 var GroupBySubscriber = /*@__PURE__*/ (function (_super) {
25 tslib_1.__extends(GroupBySubscriber, _super);
26 function GroupBySubscriber(destination, keySelector, elementSelector, durationSelector, subjectSelector) {
27 var _this = _super.call(this, destination) || this;
28 _this.keySelector = keySelector;
29 _this.elementSelector = elementSelector;
30 _this.durationSelector = durationSelector;
31 _this.subjectSelector = subjectSelector;
33 _this.attemptedToUnsubscribe = false;
37 GroupBySubscriber.prototype._next = function (value) {
40 key = this.keySelector(value);
46 this._group(value, key);
48 GroupBySubscriber.prototype._group = function (value, key) {
49 var groups = this.groups;
51 groups = this.groups = new Map();
53 var group = groups.get(key);
55 if (this.elementSelector) {
57 element = this.elementSelector(value);
67 group = (this.subjectSelector ? this.subjectSelector() : new Subject());
68 groups.set(key, group);
69 var groupedObservable = new GroupedObservable(key, group, this);
70 this.destination.next(groupedObservable);
71 if (this.durationSelector) {
72 var duration = void 0;
74 duration = this.durationSelector(new GroupedObservable(key, group));
80 this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
87 GroupBySubscriber.prototype._error = function (err) {
88 var groups = this.groups;
90 groups.forEach(function (group, key) {
95 this.destination.error(err);
97 GroupBySubscriber.prototype._complete = function () {
98 var groups = this.groups;
100 groups.forEach(function (group, key) {
105 this.destination.complete();
107 GroupBySubscriber.prototype.removeGroup = function (key) {
108 this.groups.delete(key);
110 GroupBySubscriber.prototype.unsubscribe = function () {
112 this.attemptedToUnsubscribe = true;
113 if (this.count === 0) {
114 _super.prototype.unsubscribe.call(this);
118 return GroupBySubscriber;
120 var GroupDurationSubscriber = /*@__PURE__*/ (function (_super) {
121 tslib_1.__extends(GroupDurationSubscriber, _super);
122 function GroupDurationSubscriber(key, group, parent) {
123 var _this = _super.call(this, group) || this;
126 _this.parent = parent;
129 GroupDurationSubscriber.prototype._next = function (value) {
132 GroupDurationSubscriber.prototype._unsubscribe = function () {
133 var _a = this, parent = _a.parent, key = _a.key;
134 this.key = this.parent = null;
136 parent.removeGroup(key);
139 return GroupDurationSubscriber;
141 var GroupedObservable = /*@__PURE__*/ (function (_super) {
142 tslib_1.__extends(GroupedObservable, _super);
143 function GroupedObservable(key, groupSubject, refCountSubscription) {
144 var _this = _super.call(this) || this;
146 _this.groupSubject = groupSubject;
147 _this.refCountSubscription = refCountSubscription;
150 GroupedObservable.prototype._subscribe = function (subscriber) {
151 var subscription = new Subscription();
152 var _a = this, refCountSubscription = _a.refCountSubscription, groupSubject = _a.groupSubject;
153 if (refCountSubscription && !refCountSubscription.closed) {
154 subscription.add(new InnerRefCountSubscription(refCountSubscription));
156 subscription.add(groupSubject.subscribe(subscriber));
159 return GroupedObservable;
161 export { GroupedObservable };
162 var InnerRefCountSubscription = /*@__PURE__*/ (function (_super) {
163 tslib_1.__extends(InnerRefCountSubscription, _super);
164 function InnerRefCountSubscription(parent) {
165 var _this = _super.call(this) || this;
166 _this.parent = parent;
170 InnerRefCountSubscription.prototype.unsubscribe = function () {
171 var parent = this.parent;
172 if (!parent.closed && !this.closed) {
173 _super.prototype.unsubscribe.call(this);
175 if (parent.count === 0 && parent.attemptedToUnsubscribe) {
176 parent.unsubscribe();
180 return InnerRefCountSubscription;
182 //# sourceMappingURL=groupBy.js.map