File size: 1,293 Bytes
4d70170 |
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 |
import { computed, ref } from 'vue'
import { useNow } from '@vueuse/core'
const isConnected = ref(false)
const isInitializing = ref(true)
const lastDisconnect = ref(0)
const reloadTimes = ref(0)
let reloadRegistered = false
export function useAppConnection() {
const now = useNow({
interval: 1000,
})
const showDisplayDisconnected = computed(() => {
if (isInitializing.value) {
return false
}
if (lastDisconnect.value === 0) {
return false
}
// Wait for 5 seconds before showing the disconnected message
return !isConnected && now.value.getTime() - lastDisconnect.value > 5000
})
return {
isConnected,
isInitializing,
lastDisconnect,
showDisplayDisconnected,
reloadTimes,
}
}
export function setAppConnected(value: boolean, force = false, fromReload = false) {
// We got disconnected from a page reload
if (!value) {
reloadRegistered = fromReload
}
if (force) {
lastDisconnect.value = 0
}
else if (!value && isConnected.value) {
lastDisconnect.value = Date.now()
}
isConnected.value = value
// We are reconnected after a page reload
if (value && reloadRegistered) {
reloadTimes.value++
}
}
export function setAppInitializing(value: boolean) {
isInitializing.value = value
}
|