[6a3a178] | 1 | "use strict";
|
---|
| 2 | var __extends = (this && this.__extends) || (function () {
|
---|
| 3 | var extendStatics = function (d, b) {
|
---|
| 4 | extendStatics = Object.setPrototypeOf ||
|
---|
| 5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
---|
| 6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
---|
| 7 | return extendStatics(d, b);
|
---|
| 8 | }
|
---|
| 9 | return function (d, b) {
|
---|
| 10 | extendStatics(d, b);
|
---|
| 11 | function __() { this.constructor = d; }
|
---|
| 12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
| 13 | };
|
---|
| 14 | })();
|
---|
| 15 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 16 | var Subscriber_1 = require("../Subscriber");
|
---|
| 17 | var Subscription_1 = require("../Subscription");
|
---|
| 18 | var Observable_1 = require("../Observable");
|
---|
| 19 | var Subject_1 = require("../Subject");
|
---|
| 20 | function groupBy(keySelector, elementSelector, durationSelector, subjectSelector) {
|
---|
| 21 | return function (source) {
|
---|
| 22 | return source.lift(new GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector));
|
---|
| 23 | };
|
---|
| 24 | }
|
---|
| 25 | exports.groupBy = groupBy;
|
---|
| 26 | var GroupByOperator = (function () {
|
---|
| 27 | function GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector) {
|
---|
| 28 | this.keySelector = keySelector;
|
---|
| 29 | this.elementSelector = elementSelector;
|
---|
| 30 | this.durationSelector = durationSelector;
|
---|
| 31 | this.subjectSelector = subjectSelector;
|
---|
| 32 | }
|
---|
| 33 | GroupByOperator.prototype.call = function (subscriber, source) {
|
---|
| 34 | return source.subscribe(new GroupBySubscriber(subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector));
|
---|
| 35 | };
|
---|
| 36 | return GroupByOperator;
|
---|
| 37 | }());
|
---|
| 38 | var GroupBySubscriber = (function (_super) {
|
---|
| 39 | __extends(GroupBySubscriber, _super);
|
---|
| 40 | function GroupBySubscriber(destination, keySelector, elementSelector, durationSelector, subjectSelector) {
|
---|
| 41 | var _this = _super.call(this, destination) || this;
|
---|
| 42 | _this.keySelector = keySelector;
|
---|
| 43 | _this.elementSelector = elementSelector;
|
---|
| 44 | _this.durationSelector = durationSelector;
|
---|
| 45 | _this.subjectSelector = subjectSelector;
|
---|
| 46 | _this.groups = null;
|
---|
| 47 | _this.attemptedToUnsubscribe = false;
|
---|
| 48 | _this.count = 0;
|
---|
| 49 | return _this;
|
---|
| 50 | }
|
---|
| 51 | GroupBySubscriber.prototype._next = function (value) {
|
---|
| 52 | var key;
|
---|
| 53 | try {
|
---|
| 54 | key = this.keySelector(value);
|
---|
| 55 | }
|
---|
| 56 | catch (err) {
|
---|
| 57 | this.error(err);
|
---|
| 58 | return;
|
---|
| 59 | }
|
---|
| 60 | this._group(value, key);
|
---|
| 61 | };
|
---|
| 62 | GroupBySubscriber.prototype._group = function (value, key) {
|
---|
| 63 | var groups = this.groups;
|
---|
| 64 | if (!groups) {
|
---|
| 65 | groups = this.groups = new Map();
|
---|
| 66 | }
|
---|
| 67 | var group = groups.get(key);
|
---|
| 68 | var element;
|
---|
| 69 | if (this.elementSelector) {
|
---|
| 70 | try {
|
---|
| 71 | element = this.elementSelector(value);
|
---|
| 72 | }
|
---|
| 73 | catch (err) {
|
---|
| 74 | this.error(err);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | else {
|
---|
| 78 | element = value;
|
---|
| 79 | }
|
---|
| 80 | if (!group) {
|
---|
| 81 | group = (this.subjectSelector ? this.subjectSelector() : new Subject_1.Subject());
|
---|
| 82 | groups.set(key, group);
|
---|
| 83 | var groupedObservable = new GroupedObservable(key, group, this);
|
---|
| 84 | this.destination.next(groupedObservable);
|
---|
| 85 | if (this.durationSelector) {
|
---|
| 86 | var duration = void 0;
|
---|
| 87 | try {
|
---|
| 88 | duration = this.durationSelector(new GroupedObservable(key, group));
|
---|
| 89 | }
|
---|
| 90 | catch (err) {
|
---|
| 91 | this.error(err);
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
| 94 | this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | if (!group.closed) {
|
---|
| 98 | group.next(element);
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 | GroupBySubscriber.prototype._error = function (err) {
|
---|
| 102 | var groups = this.groups;
|
---|
| 103 | if (groups) {
|
---|
| 104 | groups.forEach(function (group, key) {
|
---|
| 105 | group.error(err);
|
---|
| 106 | });
|
---|
| 107 | groups.clear();
|
---|
| 108 | }
|
---|
| 109 | this.destination.error(err);
|
---|
| 110 | };
|
---|
| 111 | GroupBySubscriber.prototype._complete = function () {
|
---|
| 112 | var groups = this.groups;
|
---|
| 113 | if (groups) {
|
---|
| 114 | groups.forEach(function (group, key) {
|
---|
| 115 | group.complete();
|
---|
| 116 | });
|
---|
| 117 | groups.clear();
|
---|
| 118 | }
|
---|
| 119 | this.destination.complete();
|
---|
| 120 | };
|
---|
| 121 | GroupBySubscriber.prototype.removeGroup = function (key) {
|
---|
| 122 | this.groups.delete(key);
|
---|
| 123 | };
|
---|
| 124 | GroupBySubscriber.prototype.unsubscribe = function () {
|
---|
| 125 | if (!this.closed) {
|
---|
| 126 | this.attemptedToUnsubscribe = true;
|
---|
| 127 | if (this.count === 0) {
|
---|
| 128 | _super.prototype.unsubscribe.call(this);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | };
|
---|
| 132 | return GroupBySubscriber;
|
---|
| 133 | }(Subscriber_1.Subscriber));
|
---|
| 134 | var GroupDurationSubscriber = (function (_super) {
|
---|
| 135 | __extends(GroupDurationSubscriber, _super);
|
---|
| 136 | function GroupDurationSubscriber(key, group, parent) {
|
---|
| 137 | var _this = _super.call(this, group) || this;
|
---|
| 138 | _this.key = key;
|
---|
| 139 | _this.group = group;
|
---|
| 140 | _this.parent = parent;
|
---|
| 141 | return _this;
|
---|
| 142 | }
|
---|
| 143 | GroupDurationSubscriber.prototype._next = function (value) {
|
---|
| 144 | this.complete();
|
---|
| 145 | };
|
---|
| 146 | GroupDurationSubscriber.prototype._unsubscribe = function () {
|
---|
| 147 | var _a = this, parent = _a.parent, key = _a.key;
|
---|
| 148 | this.key = this.parent = null;
|
---|
| 149 | if (parent) {
|
---|
| 150 | parent.removeGroup(key);
|
---|
| 151 | }
|
---|
| 152 | };
|
---|
| 153 | return GroupDurationSubscriber;
|
---|
| 154 | }(Subscriber_1.Subscriber));
|
---|
| 155 | var GroupedObservable = (function (_super) {
|
---|
| 156 | __extends(GroupedObservable, _super);
|
---|
| 157 | function GroupedObservable(key, groupSubject, refCountSubscription) {
|
---|
| 158 | var _this = _super.call(this) || this;
|
---|
| 159 | _this.key = key;
|
---|
| 160 | _this.groupSubject = groupSubject;
|
---|
| 161 | _this.refCountSubscription = refCountSubscription;
|
---|
| 162 | return _this;
|
---|
| 163 | }
|
---|
| 164 | GroupedObservable.prototype._subscribe = function (subscriber) {
|
---|
| 165 | var subscription = new Subscription_1.Subscription();
|
---|
| 166 | var _a = this, refCountSubscription = _a.refCountSubscription, groupSubject = _a.groupSubject;
|
---|
| 167 | if (refCountSubscription && !refCountSubscription.closed) {
|
---|
| 168 | subscription.add(new InnerRefCountSubscription(refCountSubscription));
|
---|
| 169 | }
|
---|
| 170 | subscription.add(groupSubject.subscribe(subscriber));
|
---|
| 171 | return subscription;
|
---|
| 172 | };
|
---|
| 173 | return GroupedObservable;
|
---|
| 174 | }(Observable_1.Observable));
|
---|
| 175 | exports.GroupedObservable = GroupedObservable;
|
---|
| 176 | var InnerRefCountSubscription = (function (_super) {
|
---|
| 177 | __extends(InnerRefCountSubscription, _super);
|
---|
| 178 | function InnerRefCountSubscription(parent) {
|
---|
| 179 | var _this = _super.call(this) || this;
|
---|
| 180 | _this.parent = parent;
|
---|
| 181 | parent.count++;
|
---|
| 182 | return _this;
|
---|
| 183 | }
|
---|
| 184 | InnerRefCountSubscription.prototype.unsubscribe = function () {
|
---|
| 185 | var parent = this.parent;
|
---|
| 186 | if (!parent.closed && !this.closed) {
|
---|
| 187 | _super.prototype.unsubscribe.call(this);
|
---|
| 188 | parent.count -= 1;
|
---|
| 189 | if (parent.count === 0 && parent.attemptedToUnsubscribe) {
|
---|
| 190 | parent.unsubscribe();
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | };
|
---|
| 194 | return InnerRefCountSubscription;
|
---|
| 195 | }(Subscription_1.Subscription));
|
---|
| 196 | //# sourceMappingURL=groupBy.js.map |
---|