Skip to content

Commit 7d21717

Browse files
committed
chore: throttle the pushState
1 parent 16cd312 commit 7d21717

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

web/src/Playground.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import { ApplyOverlay, CalculateOverlay, GetInfo } from "./bridge.ts";
66
import { Alert } from "@speakeasy-api/moonshine";
77
import { blankOverlay, petstore } from "./defaults.ts";
88
import { useAtom } from "jotai";
9-
import { atomWithHash } from "jotai-location";
9+
import { throttledPushState } from "./url.ts";
1010
import speakeasyWhiteLogo from "./assets/speakeasy-white.svg";
1111
import openapiLogo from "./assets/openapi.svg";
12+
import { atomWithHash } from "jotai-location";
1213

1314
const originalOpenAPI = atomWithHash("originalOpenAPI", petstore, {
14-
setHash: "replaceState",
15+
setHash: throttledPushState,
1516
});
1617
const changedOpenAPI = atomWithHash("changedOpenAPI", petstore, {
17-
setHash: "replaceState",
18+
setHash: throttledPushState,
1819
});
1920
const overlay = atomWithHash("overlay", blankOverlay, {
20-
setHash: "replaceState",
21+
setHash: throttledPushState,
2122
});
2223

2324
function Playground() {

web/src/url.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let timer: any = undefined;
2+
3+
export function push() {
4+
clearTimeout(timer);
5+
timer = null;
6+
window.history.pushState(
7+
window.history.state,
8+
"",
9+
`${window.location.pathname}${window.location.search}#${window.location.hash}`,
10+
);
11+
}
12+
export function throttledPushState(searchParams: string) {
13+
if (!timer) {
14+
timer = setTimeout(push, 60000);
15+
}
16+
location.replace(`#${searchParams}`);
17+
}

0 commit comments

Comments
 (0)