/
bucketBy.ts
96 lines (88 loc) · 2.5 KB
/
bucketBy.ts
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
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/
/*tslint:disable:no-use-before-declare*/
import {
Observable,
Operator,
OperatorFunction,
Subject,
Subscriber,
TeardownLogic,
} from "rxjs";
export function bucketBy<T>(
count: number,
hashSelector: (value: T, index: number) => number,
subjectSelector: () => Subject<T> = () => new Subject<T>()
): OperatorFunction<T, Observable<T>[]> {
return (source) =>
source.lift(new BucketByOperator<T>(count, hashSelector, subjectSelector));
}
/*tslint:disable-next-line:no-unused-declaration*/
class BucketByOperator<T> implements Operator<T, Observable<T>[]> {
constructor(
private count: number,
private hashSelector: (value: T, index: number) => number,
private subjectSelector: () => Subject<T>
) {}
call(subscriber: Subscriber<Observable<T>[]>, source: any): TeardownLogic {
return source.subscribe(
new BucketBySubscriber(
subscriber,
this.count,
this.hashSelector,
this.subjectSelector
)
);
}
}
/*tslint:disable-next-line:no-unused-declaration*/
class BucketBySubscriber<T> extends Subscriber<T> {
private buckets: Subject<T>[];
private index = 0;
constructor(
destination: Subscriber<Observable<T>[]>,
private count: number,
private hashSelector: (value: T, index: number) => number,
private subjectSelector: () => Subject<T>
) {
super(destination);
const buckets = (this.buckets = new Array(count));
for (let i = 0; i < count; ++i) {
buckets[i] = subjectSelector();
}
destination.next!(buckets.map((subject) => subject.asObservable()));
}
protected _next(value: T): void {
const { buckets, closed, count, hashSelector } = this;
if (closed) {
return;
}
let index: number;
try {
const hash = hashSelector(value, this.index++);
index = Math.abs(Math.floor(hash)) % count;
} catch (error: unknown) {
this.error(error);
return;
}
buckets[index].next(value);
}
protected _error(error: any): void {
const { buckets, closed, destination } = this;
if (closed) {
return;
}
buckets.forEach((bucket) => bucket.error(error));
destination.error!(error);
}
protected _complete(): void {
const { buckets, closed, destination } = this;
if (closed) {
return;
}
buckets.forEach((bucket) => bucket.complete());
destination.complete!();
}
}