-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
72 lines (58 loc) · 1.36 KB
/
app.vue
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<template>
<NuxtLoadingIndicator :color="colorMode.value === 'dark' ? '#fff' : '#000'" />
<main class="font-monaspace-xenon font-variable min-h-screen min-w-[100vw]">
<NuxtLayout v-if="isReady === true">
<NuxtPage />
</NuxtLayout>
<ServerUnavailable v-else-if="isReady === false" />
</main>
</template>
<script setup lang="ts">
const { pingServer } = useServerStatus();
const colorMode = useColorMode();
const route = useRoute();
const timeout = ref<number | NodeJS.Timeout>();
const retry = ref<number>(0);
const isReady = ref<boolean | null>(route.path === "/embed" ? true : null);
function callback() {
pingServer()
.then((res) => {
isReady.value = res;
if (!res) {
beforeBackoff();
}
})
.catch(() => {
beforeBackoff();
});
}
function beforeBackoff() {
if (timeout.value) {
clearTimeout(timeout.value);
}
retry.value += 1;
if (retry.value > 10) {
// Reset retry
retry.value = 0;
}
backoff();
}
function backoff() {
if (timeout.value) {
clearTimeout(timeout.value);
}
// Backoff exponentially
const backoff = Math.pow(2, retry.value);
timeout.value = setTimeout(callback, 1000 * backoff);
}
onMounted(() => {
if (route.path !== "/embed") {
callback();
}
});
onBeforeUnmount(() => {
if (timeout.value) {
clearTimeout(timeout.value);
}
});
</script>