-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathdispatch-series.ts
More file actions
52 lines (46 loc) · 1.49 KB
/
dispatch-series.ts
File metadata and controls
52 lines (46 loc) · 1.49 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
import { isApplePlatform, type DeviceInfo } from '../utils/device.ts';
import { sleep } from '../utils/timeouts.ts';
export { requireIntInRange } from '../utils/validation.ts';
const DETERMINISTIC_JITTER_PATTERN: ReadonlyArray<readonly [number, number]> = [
[0, 0],
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
[1, 1],
[-1, 1],
[1, -1],
[-1, -1],
];
export function clampIosSwipeDuration(durationMs: number): number {
// Keep iOS swipes stable while allowing explicit fast durations for scroll-heavy flows.
return Math.min(60, Math.max(16, Math.round(durationMs)));
}
export function shouldUseIosTapSeries(
device: DeviceInfo,
count: number,
holdMs: number,
jitterPx: number,
): boolean {
return isApplePlatform(device.platform) && count > 1 && holdMs === 0 && jitterPx === 0;
}
export function shouldUseIosDragSeries(device: DeviceInfo, count: number): boolean {
return isApplePlatform(device.platform) && count > 1;
}
export function computeDeterministicJitter(index: number, jitterPx: number): [number, number] {
if (jitterPx <= 0) return [0, 0];
const [dx, dy] = DETERMINISTIC_JITTER_PATTERN[index % DETERMINISTIC_JITTER_PATTERN.length]!;
return [dx * jitterPx, dy * jitterPx];
}
export async function runRepeatedSeries(
count: number,
pauseMs: number,
operation: (index: number) => Promise<void>,
): Promise<void> {
for (let index = 0; index < count; index += 1) {
await operation(index);
if (index < count - 1 && pauseMs > 0) {
await sleep(pauseMs);
}
}
}