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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 | 1
1
1
1
1
1
1
1
1
37
1
37
37
37
37
37
1
37
37
37
37
1
1
37
1
37
37
37
37
37
37
37
1
273
273
2
271
271
271
271
34
271
271
112
112
112
59
59
1
58
112
271
144
144
2
142
127
11
11
11
10
29
29
11
19
19
19
18
42
42
19
1
150
1
1
58
58
58
58
1
26
26
1
14
14
1
39
39
1
1
37
37
1
37
1
37
1
39
36
36
33
33
1
1
171
171
171
171
1
150
150
84
150
150
1
1
84
84
84
1
84
84
84
84
3
3
1
| import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {Observable} from '../Observable';
import {Subject} from '../Subject';
import {Map} from '../util/Map';
import {FastMap} from '../util/FastMap';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
/**
* Groups the items emitted by an Observable according to a specified criterion,
* and emits these grouped items as `GroupedObservables`, one `GroupedObservable` per group.
*
* <img src="./img/groupBy.png" width="100%">
*
* @param {Function} keySelector - a function that extracts the key for each item
* @param {Function} elementSelector - a function that extracts the return element for each item
* @returns {Observable} an Observable that emits GroupedObservables, each of which corresponds
* to a unique key value and each of which emits those items from the source Observable that share
* that key value.
*/
export function groupBy<T, K, R>(keySelector: (value: T) => K,
elementSelector?: (value: T) => R,
durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>): GroupByObservable<T, K, R> {
return new GroupByObservable(this, keySelector, elementSelector, durationSelector);
}
export class GroupByObservable<T, K, R> extends Observable<GroupedObservable<K, R>> {
constructor(public source: Observable<T>,
private keySelector: (value: T) => K,
private elementSelector?: (value: T) => R,
private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>) {
super();
}
protected _subscribe(subscriber: Subscriber<any>): Subscription {
const refCountSubscription = new RefCountSubscription();
const groupBySubscriber = new GroupBySubscriber(
subscriber, refCountSubscription, this.keySelector, this.elementSelector, this.durationSelector
);
refCountSubscription.setPrimary(this.source.subscribe(groupBySubscriber));
return refCountSubscription;
}
}
class GroupBySubscriber<T, K, R> extends Subscriber<T> {
private groups: Map<K, Subject<T|R>> = null;
constructor(destination: Subscriber<R>,
private refCountSubscription: RefCountSubscription,
private keySelector: (value: T) => K,
private elementSelector?: (value: T) => R,
private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>) {
super();
this.destination = destination;
this.add(destination);
}
protected _next(x: T): void {
let key = tryCatch(this.keySelector)(x);
if (key === errorObject) {
this.error(errorObject.e);
} else {
let groups = this.groups;
const elementSelector = this.elementSelector;
const durationSelector = this.durationSelector;
if (!groups) {
groups = this.groups = typeof key === 'string' ? new FastMap() : new Map();
}
let group = groups.get(key);
if (!group) {
groups.set(key, group = new Subject<T|R>());
let groupedObservable = new GroupedObservable(key, group, this.refCountSubscription);
if (durationSelector) {
let duration = tryCatch(durationSelector)(new GroupedObservable<K, R>(key, <any>group));
if (duration === errorObject) {
this.error(errorObject.e);
} else {
this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
}
}
this.destination.next(groupedObservable);
}
if (elementSelector) {
let value = tryCatch(elementSelector)(x);
if (value === errorObject) {
this.error(errorObject.e);
} else {
group.next(value);
}
} else {
group.next(x);
}
}
}
protected _error(err: any): void {
const groups = this.groups;
if (groups) {
groups.forEach((group, key) => {
group.error(err);
this.removeGroup(key);
});
}
this.destination.error(err);
}
protected _complete(): void {
const groups = this.groups;
if (groups) {
groups.forEach((group, key) => {
group.complete();
this.removeGroup(key);
});
}
this.destination.complete();
}
removeGroup(key: K): void {
this.groups.delete(key);
}
}
class GroupDurationSubscriber<K, T> extends Subscriber<T> {
constructor(private key: K,
private group: Subject<T>,
private parent: GroupBySubscriber<any, K, T>) {
super();
}
protected _next(value: T): void {
this.group.complete();
this.parent.removeGroup(this.key);
}
protected _error(err: any): void {
this.group.error(err);
this.parent.removeGroup(this.key);
}
protected _complete(): void {
this.group.complete();
this.parent.removeGroup(this.key);
}
}
export class RefCountSubscription extends Subscription {
primary: Subscription;
attemptedToUnsubscribePrimary: boolean = false;
count: number = 0;
constructor() {
super();
}
setPrimary(subscription: Subscription) {
this.primary = subscription;
}
unsubscribe() {
if (!this.isUnsubscribed && !this.attemptedToUnsubscribePrimary) {
this.attemptedToUnsubscribePrimary = true;
if (this.count === 0) {
super.unsubscribe();
this.primary.unsubscribe();
}
}
}
}
export class GroupedObservable<K, T> extends Observable<T> {
constructor(public key: K,
private groupSubject: Subject<T>,
private refCountSubscription?: RefCountSubscription) {
super();
}
protected _subscribe(subscriber: Subscriber<T>) {
const subscription = new Subscription();
if (this.refCountSubscription && !this.refCountSubscription.isUnsubscribed) {
subscription.add(new InnerRefCountSubscription(this.refCountSubscription));
}
subscription.add(this.groupSubject.subscribe(subscriber));
return subscription;
}
}
export class InnerRefCountSubscription extends Subscription {
constructor(private parent: RefCountSubscription) {
super();
parent.count++;
}
unsubscribe() {
Eif (!this.parent.isUnsubscribed && !this.isUnsubscribed) {
super.unsubscribe();
this.parent.count--;
if (this.parent.count === 0 && this.parent.attemptedToUnsubscribePrimary) {
this.parent.unsubscribe();
this.parent.primary.unsubscribe();
}
}
}
}
|