-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathgroupBy.ts
More file actions
140 lines (121 loc) · 4.2 KB
/
groupBy.ts
File metadata and controls
140 lines (121 loc) · 4.2 KB
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
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 {RefCountSubscription, GroupedObservable} from './groupBy-support';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
export function groupBy<T, R>(keySelector: (value: T) => string,
elementSelector?: (value: T) => R,
durationSelector?: (grouped: GroupedObservable<R>) => Observable<any>): GroupByObservable<T, R> {
return new GroupByObservable<T, R>(this, keySelector, elementSelector, durationSelector);
}
export class GroupByObservable<T, R> extends Observable<GroupedObservable<R>> {
constructor(public source: Observable<T>,
private keySelector: (value: T) => string,
private elementSelector?: (value: T) => R,
private durationSelector?: (grouped: GroupedObservable<R>) => Observable<any>) {
super();
}
_subscribe(subscriber: Subscriber<any>): Subscription<T> | Function | void {
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, R> extends Subscriber<T> {
private groups = null;
constructor(destination: Subscriber<R>,
private refCountSubscription: RefCountSubscription<T>,
private keySelector: (value: T) => string,
private elementSelector?: (value: T) => R,
private durationSelector?: (grouped: GroupedObservable<R>) => Observable<any>) {
super();
this.destination = destination;
this.add(destination);
}
_next(x: T): void {
let key = tryCatch(this.keySelector)(x);
if (key === errorObject) {
this.error(key.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: Subject<T|R> = groups.get(key);
if (!group) {
groups.set(key, group = new Subject());
let groupedObservable = new GroupedObservable<R>(key, group, this.refCountSubscription);
if (durationSelector) {
let duration = tryCatch(durationSelector)(new GroupedObservable<R>(key, group));
if (duration === errorObject) {
this.error(duration.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(value.e);
} else {
group.next(value);
}
} else {
group.next(x);
}
}
}
_error(err: any): void {
const groups = this.groups;
if (groups) {
groups.forEach((group, key) => {
group.error(err);
this.removeGroup(key);
});
}
this.destination.error(err);
}
_complete(): void {
const groups = this.groups;
if (groups) {
groups.forEach((group, key) => {
group.complete();
this.removeGroup(group);
});
}
this.destination.complete();
}
removeGroup(key: string): void {
this.groups.delete(key);
}
}
class GroupDurationSubscriber<T> extends Subscriber<T> {
constructor(private key: string,
private group: Subject<T>,
private parent: GroupBySubscriber<any, T>) {
super(null);
}
_next(value: T): void {
this.group.complete();
this.parent.removeGroup(this.key);
}
_error(err: any): void {
this.group.error(err);
this.parent.removeGroup(this.key);
}
_complete(): void {
this.group.complete();
this.parent.removeGroup(this.key);
}
}