-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (49 loc) · 1.32 KB
/
index.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
let delay: number | null = null;
let nextPush: [any, string | URL | null | undefined] | null = null;
let nextReplace: [any, string | URL | null | undefined] | null = null;
let lastUpdate = -Infinity;
let isScheduled = false;
const replaceState: typeof history.replaceState = (state, _, url) => {
nextReplace = [state, url];
schedule();
};
const pushState: typeof history.pushState = (state, _, url) => {
nextPush = [state, url];
nextReplace = null;
schedule();
};
function schedule() {
if (isScheduled) {
return;
}
if (delay === null) {
// Check if we're on Safari, either desktop or mobile.
// https://stackoverflow.com/a/31732310
delay = navigator.vendor === "Apple Computer, Inc." ? 310 : 52;
}
let delta = performance.now() - lastUpdate;
if (delta >= delay) {
doWork();
} else {
setTimeout(doWork, delay - delta);
isScheduled = true;
}
}
function doWork() {
isScheduled = false;
lastUpdate = performance.now();
if (nextPush !== null) {
history.pushState(nextPush[0], "", nextPush[1]);
nextPush = null;
if (nextReplace !== null) {
schedule();
}
} else {
history.replaceState(nextReplace![0], "", nextReplace![1]);
nextReplace = null;
}
}
function setDelay(newDelay: number | null) {
delay = newDelay;
}
export { replaceState, pushState, setDelay };