-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
rateLimit.ts
89 lines (81 loc) · 2.35 KB
/
rateLimit.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
/**
* @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
*/
import {
asapScheduler,
MonoTypeOperatorFunction,
Observable,
of,
SchedulerLike,
} from "rxjs";
import { concatMap, delay, map, scan } from "rxjs/operators";
export function rateLimit<T>(
period: number,
scheduler?: SchedulerLike
): MonoTypeOperatorFunction<T>;
export function rateLimit<T>(
period: number,
count: number,
scheduler?: SchedulerLike
): MonoTypeOperatorFunction<T>;
export function rateLimit<T>(
period: number,
...args: (number | SchedulerLike | undefined)[]
): MonoTypeOperatorFunction<T> {
let count = 1;
let scheduler: SchedulerLike = asapScheduler;
if (args.length === 1) {
if (typeof args[0] === "number") {
count = args[0] as number;
} else {
scheduler = args[0] as SchedulerLike;
}
} else if (args.length === 2) {
count = args[0] as number;
scheduler = args[1] as SchedulerLike;
}
interface Emission<T> {
delay: number;
until: number;
value: T;
}
const definedCount = count || 1;
return (source: Observable<T>) =>
source.pipe(
scan((emissions: Emission<T>[], value: T) => {
const now = scheduler.now();
const since = now - period;
emissions = emissions.filter((emission) => emission.until > since);
if (emissions.length >= definedCount) {
const leastRecentEmission = emissions[0];
const mostRecentEmission = emissions[emissions.length - 1];
const until =
leastRecentEmission.until +
period * Math.floor(emissions.length / definedCount);
emissions.push({
delay:
mostRecentEmission.until < now
? until - now
: until - mostRecentEmission.until,
until,
value,
});
} else {
emissions.push({
delay: 0,
until: now,
value,
});
}
return emissions;
}, []),
map((emissions: Emission<T>[]) => emissions[emissions.length - 1]),
concatMap((emission: Emission<T>) => {
const observable = of(emission.value);
return emission.delay
? observable.pipe(delay(emission.delay, scheduler))
: observable;
})
);
}