deno.land / x / froebel@v0.23.2 / throttle.test.ts

throttle.test.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
import throttle from "./throttle.ts";import { assert, assertEquals } from "testing/asserts.ts";
const expectTimes = (times: number[], ...expected: number[]) => { assertEquals(times.length, expected.length); const margin = 32; for (let i = 0; i < times.length; i++) { assert(times[i] > expected[i] - margin); assert(times[i] < expected[i] + margin); }};
const runTest = ( interval: number, { leading = false, trailing = false, }: { leading?: boolean; trailing?: boolean; }, ...expected: number[]) => new Promise<void>((done) => { const t0 = performance.now(); const invocations: number[] = [];
const fun = () => { invocations.push(performance.now() - t0); };
const throttled = throttle(fun, 50, { leading, trailing });
const iid = setInterval(throttled, interval);
setTimeout(() => { clearInterval(iid); setTimeout(() => { try { expectTimes(invocations, ...expected); } finally { done(); } }, 100); }, 150); });
Deno.test("throttle (leading + trailing)", () => runTest(10, { leading: true, trailing: true }, 10, 60, 110, 160));
Deno.test("throttle (trailing)", () => runTest(10, { leading: false, trailing: true }, 60, 110, 160));
Deno.test("throttle (leading)", () => runTest(16, { leading: true, trailing: false }, 16, 80, 144));
froebel

Version Info

Tagged at
a year ago