Skip to content

Commit

Permalink
feat(frontend): auto-refresh status
Browse files Browse the repository at this point in the history
  • Loading branch information
Data5tream committed Jul 7, 2023
1 parent dace937 commit 4fe5642
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
20 changes: 19 additions & 1 deletion frontend/src/lib/components/StatusList.svelte
@@ -1,8 +1,26 @@
<script lang="ts">
import type { Watchpoint } from '$lib/dataprovider';
import StatusEntry from '$lib/components/StatusEntry.svelte';
import { invalidate } from '$app/navigation';
import { onDestroy, onMount } from 'svelte';
import { env } from '$env/dynamic/public';
export let data: Array<Watchpoint> | undefined;
let interval;
const reload = () => {
invalidate('app:statusList');
};
onMount(() => {
const refreshRate = Number(env.PUBLIC_STATUS_REFRESH_RATE);
interval = setInterval(reload, refreshRate > 100 ? refreshRate : 2000);
});
onDestroy(() => {
clearInterval(interval);
});
</script>

<div class="list-container">
Expand All @@ -15,7 +33,7 @@
{/if}
</div>

<style lang='scss'>
<style lang="scss">
.list-container {
display: grid;
grid-template-columns: 1fr;
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/lib/dataprovider.ts
Expand Up @@ -15,7 +15,10 @@ export interface StatusData {
watchpoints?: Array<Watchpoint>;
}

export const loadStatus = async (): Promise<StatusData> => {
export const loadStatus = async (fetch: {
(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
}): Promise<StatusData> => {
try {
const res = await fetch(`${PUBLIC_API_URL}/status`);
if (res.status !== 200) {
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/routes/+page.ts
@@ -1,8 +1,10 @@
import type { PageLoad } from './$types';

import { loadStatus } from '$lib/dataprovider';

/** @type {import('./$types').PageLoad} */
export const load = async () => {
export const load = (async ({ fetch, depends }) => {
depends('app:statusList');
return {
status: await loadStatus()
status: await loadStatus(fetch)
};
};
}) satisfies PageLoad;

0 comments on commit 4fe5642

Please sign in to comment.