[6a3a178] | 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));
|
---|
| 10 | };
|
---|
| 11 | }
|
---|
| 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;
|
---|
| 18 | }
|
---|
| 19 | GroupByOperator.prototype.call = function (subscriber, source) {
|
---|
| 20 | return source.subscribe(new GroupBySubscriber(subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector));
|
---|
| 21 | };
|
---|
| 22 | return GroupByOperator;
|
---|
| 23 | }());
|
---|
| 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;
|
---|
| 32 | _this.groups = null;
|
---|
| 33 | _this.attemptedToUnsubscribe = false;
|
---|
| 34 | _this.count = 0;
|
---|
| 35 | return _this;
|
---|
| 36 | }
|
---|
| 37 | GroupBySubscriber.prototype._next = function (value) {
|
---|
| 38 | var key;
|
---|
| 39 | try {
|
---|
| 40 | key = this.keySelector(value);
|
---|
| 41 | }
|
---|
| 42 | catch (err) {
|
---|
| 43 | this.error(err);
|
---|
| 44 | return;
|
---|
| 45 | }
|
---|
| 46 | this._group(value, key);
|
---|
| 47 | };
|
---|
| 48 | GroupBySubscriber.prototype._group = function (value, key) {
|
---|
| 49 | var groups = this.groups;
|
---|
| 50 | if (!groups) {
|
---|
| 51 | groups = this.groups = new Map();
|
---|
| 52 | }
|
---|
| 53 | var group = groups.get(key);
|
---|
| 54 | var element;
|
---|
| 55 | if (this.elementSelector) {
|
---|
| 56 | try {
|
---|
| 57 | element = this.elementSelector(value);
|
---|
| 58 | }
|
---|
| 59 | catch (err) {
|
---|
| 60 | this.error(err);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | else {
|
---|
| 64 | element = value;
|
---|
| 65 | }
|
---|
| 66 | if (!group) {
|
---|
| 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;
|
---|
| 73 | try {
|
---|
| 74 | duration = this.durationSelector(new GroupedObservable(key, group));
|
---|
| 75 | }
|
---|
| 76 | catch (err) {
|
---|
| 77 | this.error(err);
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 | this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | if (!group.closed) {
|
---|
| 84 | group.next(element);
|
---|
| 85 | }
|
---|
| 86 | };
|
---|
| 87 | GroupBySubscriber.prototype._error = function (err) {
|
---|
| 88 | var groups = this.groups;
|
---|
| 89 | if (groups) {
|
---|
| 90 | groups.forEach(function (group, key) {
|
---|
| 91 | group.error(err);
|
---|
| 92 | });
|
---|
| 93 | groups.clear();
|
---|
| 94 | }
|
---|
| 95 | this.destination.error(err);
|
---|
| 96 | };
|
---|
| 97 | GroupBySubscriber.prototype._complete = function () {
|
---|
| 98 | var groups = this.groups;
|
---|
| 99 | if (groups) {
|
---|
| 100 | groups.forEach(function (group, key) {
|
---|
| 101 | group.complete();
|
---|
| 102 | });
|
---|
| 103 | groups.clear();
|
---|
| 104 | }
|
---|
| 105 | this.destination.complete();
|
---|
| 106 | };
|
---|
| 107 | GroupBySubscriber.prototype.removeGroup = function (key) {
|
---|
| 108 | this.groups.delete(key);
|
---|
| 109 | };
|
---|
| 110 | GroupBySubscriber.prototype.unsubscribe = function () {
|
---|
| 111 | if (!this.closed) {
|
---|
| 112 | this.attemptedToUnsubscribe = true;
|
---|
| 113 | if (this.count === 0) {
|
---|
| 114 | _super.prototype.unsubscribe.call(this);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | };
|
---|
| 118 | return GroupBySubscriber;
|
---|
| 119 | }(Subscriber));
|
---|
| 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;
|
---|
| 124 | _this.key = key;
|
---|
| 125 | _this.group = group;
|
---|
| 126 | _this.parent = parent;
|
---|
| 127 | return _this;
|
---|
| 128 | }
|
---|
| 129 | GroupDurationSubscriber.prototype._next = function (value) {
|
---|
| 130 | this.complete();
|
---|
| 131 | };
|
---|
| 132 | GroupDurationSubscriber.prototype._unsubscribe = function () {
|
---|
| 133 | var _a = this, parent = _a.parent, key = _a.key;
|
---|
| 134 | this.key = this.parent = null;
|
---|
| 135 | if (parent) {
|
---|
| 136 | parent.removeGroup(key);
|
---|
| 137 | }
|
---|
| 138 | };
|
---|
| 139 | return GroupDurationSubscriber;
|
---|
| 140 | }(Subscriber));
|
---|
| 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;
|
---|
| 145 | _this.key = key;
|
---|
| 146 | _this.groupSubject = groupSubject;
|
---|
| 147 | _this.refCountSubscription = refCountSubscription;
|
---|
| 148 | return _this;
|
---|
| 149 | }
|
---|
| 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));
|
---|
| 155 | }
|
---|
| 156 | subscription.add(groupSubject.subscribe(subscriber));
|
---|
| 157 | return subscription;
|
---|
| 158 | };
|
---|
| 159 | return GroupedObservable;
|
---|
| 160 | }(Observable));
|
---|
| 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;
|
---|
| 167 | parent.count++;
|
---|
| 168 | return _this;
|
---|
| 169 | }
|
---|
| 170 | InnerRefCountSubscription.prototype.unsubscribe = function () {
|
---|
| 171 | var parent = this.parent;
|
---|
| 172 | if (!parent.closed && !this.closed) {
|
---|
| 173 | _super.prototype.unsubscribe.call(this);
|
---|
| 174 | parent.count -= 1;
|
---|
| 175 | if (parent.count === 0 && parent.attemptedToUnsubscribe) {
|
---|
| 176 | parent.unsubscribe();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | };
|
---|
| 180 | return InnerRefCountSubscription;
|
---|
| 181 | }(Subscription));
|
---|
| 182 | //# sourceMappingURL=groupBy.js.map
|
---|