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);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
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));
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;
33 GroupByOperator.prototype.call = function (subscriber, source) {
34 return source.subscribe(new GroupBySubscriber(subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector));
36 return GroupByOperator;
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;
47 _this.attemptedToUnsubscribe = false;
51 GroupBySubscriber.prototype._next = function (value) {
54 key = this.keySelector(value);
60 this._group(value, key);
62 GroupBySubscriber.prototype._group = function (value, key) {
63 var groups = this.groups;
65 groups = this.groups = new Map();
67 var group = groups.get(key);
69 if (this.elementSelector) {
71 element = this.elementSelector(value);
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;
88 duration = this.durationSelector(new GroupedObservable(key, group));
94 this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
101 GroupBySubscriber.prototype._error = function (err) {
102 var groups = this.groups;
104 groups.forEach(function (group, key) {
109 this.destination.error(err);
111 GroupBySubscriber.prototype._complete = function () {
112 var groups = this.groups;
114 groups.forEach(function (group, key) {
119 this.destination.complete();
121 GroupBySubscriber.prototype.removeGroup = function (key) {
122 this.groups.delete(key);
124 GroupBySubscriber.prototype.unsubscribe = function () {
126 this.attemptedToUnsubscribe = true;
127 if (this.count === 0) {
128 _super.prototype.unsubscribe.call(this);
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;
140 _this.parent = parent;
143 GroupDurationSubscriber.prototype._next = function (value) {
146 GroupDurationSubscriber.prototype._unsubscribe = function () {
147 var _a = this, parent = _a.parent, key = _a.key;
148 this.key = this.parent = null;
150 parent.removeGroup(key);
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;
160 _this.groupSubject = groupSubject;
161 _this.refCountSubscription = refCountSubscription;
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));
170 subscription.add(groupSubject.subscribe(subscriber));
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;
184 InnerRefCountSubscription.prototype.unsubscribe = function () {
185 var parent = this.parent;
186 if (!parent.closed && !this.closed) {
187 _super.prototype.unsubscribe.call(this);
189 if (parent.count === 0 && parent.attemptedToUnsubscribe) {
190 parent.unsubscribe();
194 return InnerRefCountSubscription;
195 }(Subscription_1.Subscription));
196 //# sourceMappingURL=groupBy.js.map