|
1 | 1 | <script>
|
2 | 2 | import { base } from '$app/paths';
|
3 |
| - import { SvelteMap } from 'svelte/reactivity'; |
4 | 3 | import FassungenContent from './FassungenContent.svelte';
|
5 | 4 |
|
6 | 5 | /** @type {{data: import('./$types').PageData}} */
|
7 | 6 | let { data } = $props();
|
| 7 | + class localPageClass { |
| 8 | + /** |
| 9 | + * @type {[[number, string][],[number, string][],[number, string][],[number, string][]]} |
| 10 | + */ |
| 11 | + pages = $state([[], [], [], []]); |
| 12 | + /** |
| 13 | + * @type {string[]} |
| 14 | + */ |
| 15 | + thirties = []; |
8 | 16 |
|
9 |
| - let localPages = $state([new SvelteMap(), new SvelteMap(), new SvelteMap(), new SvelteMap()]); |
| 17 | + fetchPage = async (/** @type {string} */ page) => { |
| 18 | + // console.log('fetching', page); |
| 19 | + // console.log(Number(page), !this.thirties.length); |
| 20 | + if (!this.thirties.length) { |
| 21 | + let [d, m, G, T] = await fetch(`${base}/fassungen/data/${Number(page) - 1}`).then((r) => |
| 22 | + r.json() |
| 23 | + ); |
| 24 | + this.pages[0].push([Number(page) - 1, d]); |
| 25 | + this.pages[1].push([Number(page) - 1, m]); |
| 26 | + this.pages[2].push([Number(page) - 1, G]); |
| 27 | + this.pages[3].push([Number(page) - 1, T]); |
| 28 | + this.thirties.push(String(Number(page) - 1)); |
| 29 | + [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json()); |
| 30 | + this.pages[0].push([Number(page), d]); |
| 31 | + this.pages[1].push([Number(page), m]); |
| 32 | + this.pages[2].push([Number(page), G]); |
| 33 | + this.pages[3].push([Number(page), T]); |
| 34 | + this.thirties.push(page); |
| 35 | + [d, m, G, T] = await fetch(`${base}/fassungen/data/${Number(page) + 1}`).then((r) => |
| 36 | + r.json() |
| 37 | + ); |
| 38 | + this.pages[0].push([Number(page) + 1, d]); |
| 39 | + this.pages[1].push([Number(page) + 1, m]); |
| 40 | + this.pages[2].push([Number(page) + 1, G]); |
| 41 | + this.pages[3].push([Number(page) + 1, T]); |
| 42 | + this.thirties.push(String(Number(page) + 1)); |
| 43 | + } else { |
| 44 | + if (Number(page) < Number(this.thirties[0])) { |
| 45 | + // console.log('fetching before'); |
| 46 | + let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json()); |
| 47 | + this.pages[0].unshift([Number(page), d]); |
| 48 | + this.pages[1].unshift([Number(page), m]); |
| 49 | + this.pages[2].unshift([Number(page), G]); |
| 50 | + this.pages[3].unshift([Number(page), T]); |
| 51 | + this.thirties.unshift(page); |
| 52 | + } else if (Number(page) > Number(this.thirties[this.thirties.length - 1])) { |
| 53 | + let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json()); |
| 54 | + this.pages[0].push([Number(page), d]); |
| 55 | + this.pages[1].push([Number(page), m]); |
| 56 | + this.pages[2].push([Number(page), G]); |
| 57 | + this.pages[3].push([Number(page), T]); |
| 58 | + this.thirties.push(page); |
| 59 | + } else if (Number(page) === Number(this.thirties[0])) { |
| 60 | + this.fetchPage(String(Number(page) - 1)); |
| 61 | + } else if (Number(page) === Number(this.thirties[this.thirties.length - 1])) { |
| 62 | + this.fetchPage(String(Number(page) + 1)); |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
10 | 67 |
|
11 |
| - const checkAndFetch = async (/** @type {string} */ page) => { |
12 |
| - if (!localPages[0].has(page)) { |
13 |
| - let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json()); |
14 |
| - localPages[0].set(page, d); |
15 |
| - localPages[1].set(page, m); |
16 |
| - localPages[2].set(page, G); |
17 |
| - localPages[3].set(page, T); |
18 |
| - } |
19 |
| - }; |
| 68 | + let localPages = new localPageClass(); |
20 | 69 |
|
21 |
| - $effect(async () => { |
22 |
| - // need to await the fetches because of race condition: random order of thirties |
23 |
| - await checkAndFetch(String(Number(data.thirties) - 1)); |
24 |
| - await checkAndFetch(data.thirties); |
25 |
| - await checkAndFetch(String(Number(data.thirties) + 1)); |
| 70 | + $effect(() => { |
| 71 | + localPages.fetchPage(data.thirties); |
26 | 72 | });
|
27 | 73 | </script>
|
28 | 74 |
|
|
34 | 80 | <div
|
35 | 81 | class="grid grid-cols-[repeat(auto-fit,minmax(400px,1fr))] gap-4 bg-surface-active-token my-4 py-4 px-8 rounded-xl"
|
36 | 82 | >
|
37 |
| - {#each localPages as pages} |
38 |
| - {#if pages.size >= 3} |
| 83 | + {#each localPages.pages as pages} |
| 84 | + {#if pages.length >= 3} |
39 | 85 | <!-- when at least 3 pages are loaded, the one for the currect thirties should be loaded aswell -->
|
40 | 86 | <FassungenContent {pages} />
|
41 | 87 | {/if}
|
|
0 commit comments