Line data Source code
1 : import { useDocumentVisibility, useTimeoutFn } from '@vueuse/core'
2 : import { ref, watch } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { useToast } from '@/components/ui/toast/toastStore'
6 : import { createToastId } from '@/types/toastId'
7 : import { useSettingStore } from '@/platform/settings/settingStore'
8 :
9 2355 : const RECONNECT_TOAST_DELAY_MS = 2000
10 : // Backgrounded-tab timer throttling can let disconnect and reconnect both fire right as the tab regains focus, racing past RECONNECT_TOAST_DELAY_MS, so use a longer delay during the grace window below.
11 2355 : const VISIBILITY_REGAINED_TOAST_DELAY_MS = 5000
12 2355 : const VISIBILITY_REGAINED_GRACE_MS = 10000
13 :
14 2355 : export function useReconnectingNotification() {
15 2355 : const { t } = useI18n()
16 2355 : const toast = useToast()
17 2355 : const settingStore = useSettingStore()
18 :
19 2355 : const toastDelayMs = ref(RECONNECT_TOAST_DELAY_MS)
20 2355 : const reconnectingToastId = createToastId()
21 :
22 2355 : const { start, stop, isPending } = useTimeoutFn(
23 : () => {
24 0 : toast.error(t('g.reconnecting'), { id: reconnectingToastId })
25 : },
26 : toastDelayMs,
27 : { immediate: false }
28 : )
29 :
30 2355 : const { start: startGracePeriod } = useTimeoutFn(
31 : () => {
32 0 : toastDelayMs.value = RECONNECT_TOAST_DELAY_MS
33 : },
34 : VISIBILITY_REGAINED_GRACE_MS,
35 : { immediate: false }
36 : )
37 :
38 2355 : const visibility = useDocumentVisibility()
39 2355 : watch(visibility, (state) => {
40 100 : if (state !== 'visible') return
41 :
42 1 : toastDelayMs.value = VISIBILITY_REGAINED_TOAST_DELAY_MS
43 1 : startGracePeriod()
44 :
45 9 : if (isPending.value) start()
46 : })
47 :
48 11 : function onReconnecting() {
49 11 : if (settingStore.get('Comfy.Toast.DisableReconnectingToast')) return
50 11 : start()
51 : }
52 :
53 8 : function onReconnected() {
54 8 : stop()
55 :
56 8 : if (toast.dismiss(reconnectingToastId)) {
57 0 : toast.success(t('g.reconnected'), { duration: 2000 })
58 : }
59 : }
60 :
61 2355 : return { onReconnecting, onReconnected }
62 : }
|