-
Notifications
You must be signed in to change notification settings - Fork 3
/
batcher-js-tests.ts
53 lines (41 loc) · 1.02 KB
/
batcher-js-tests.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
// This code does not run, but it is type-checked.
import Batcher from ".";
{
const myMethod = (payloads: any[]) => undefined;
const batch = Batcher(myMethod);
batch(); // No callback, e.g. batch MobX action (synchronous).
batch(undefined, () => 123);
batch({}, () => 123);
}
{
const myMethod = (
payloads: Array<{ id: number }>,
callback: () => undefined,
) => undefined;
const batch = Batcher(myMethod);
batch({ id: 1 }, () => undefined);
}
{
const myMethod = (payloads: Array<{ id: number }>) => undefined;
Batcher(myMethod, 10);
Batcher(myMethod, { maximum: 2 });
Batcher(myMethod, { maximum: 2, interval: 1 });
}
{
let loading = 10;
let stop = () => {
loading--;
};
/**
* Reduces flicker on front end.
*/
const lazyStop = () => {
stop = Batcher(decrease, 500);
};
const decrease = (calls: any[]) => {
loading -= calls.length;
};
if (typeof window !== "undefined") {
lazyStop();
}
}