Skip to content

Commit 2095d5c

Browse files
committed
clean the code a bit
1 parent 73f2b62 commit 2095d5c

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

routes/stage-select/+page.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import "@/ui-components/menu/menu.css";
55
import { goto } from "$app/navigation";
66
import { MAX_WORLD, SearchParamState } from "./params.svelte.ts";
77
8-
const search = $state(new SearchParamState());
9-
onMount(() => {
10-
const params = new URLSearchParams(window.location.search);
11-
search.world = Number(params.get("world") || "1");
12-
search.selected = Number(params.get("selected") || "1");
13-
});
8+
const search = new SearchParamState();
149
1510
const blocks = $derived(
1611
new Array(search.maxStage).fill(null).map((_, idx) => ({

routes/stage-select/params.svelte.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { browser } from "$app/environment";
22
import { replaceState } from "$app/navigation";
3+
import { onMount } from "svelte";
34

45
export const MAX_WORLD = 4;
56
const WORLD_STAGES_MAP = new Map([
@@ -16,6 +17,12 @@ export class SearchParamState {
1617
constructor() {
1718
this.#world = null;
1819
this.#selected = null;
20+
21+
onMount(() => {
22+
const params = new URLSearchParams(window.location.search);
23+
this.world = Number(params.get("world") || "1");
24+
this.selected = Number(params.get("selected") || "1");
25+
});
1926
}
2027

2128
get world(): number | null {
@@ -82,11 +89,13 @@ export class SearchParamState {
8289
const url = new URL(window.location.href);
8390
url.searchParams.set("world", String(this.#world));
8491
url.searchParams.set("selected", String(this.#selected));
85-
try {
86-
replaceState(url.toString(), {});
87-
} catch (e) {
88-
// what do you mean "Cannot call replaceState(...) before router is initialized"
89-
console.warn("Failed to update URL:", e);
90-
}
92+
setTimeout(() => {
93+
try {
94+
replaceState(url.toString(), {});
95+
} catch (e) {
96+
// what do you mean "Cannot call replaceState(...) before router is initialized"
97+
console.warn("Failed to update URL:", e);
98+
}
99+
}, 0);
91100
}
92101
}

0 commit comments

Comments
 (0)