2022-11-27 18:34:45 +01:00
|
|
|
import type { ComponentInternalInstance } from 'vue'
|
|
|
|
import { onActivated, onDeactivated, ref } from 'vue'
|
2023-01-06 19:40:15 +01:00
|
|
|
|
|
|
|
export const isHydrated = ref(false)
|
2022-11-27 18:34:45 +01:00
|
|
|
|
2023-03-30 21:01:24 +02:00
|
|
|
export function onHydrated(cb: () => unknown) {
|
2023-01-15 09:38:02 +01:00
|
|
|
watchOnce(isHydrated, () => cb(), { immediate: isHydrated.value })
|
|
|
|
}
|
|
|
|
|
2022-11-27 18:34:45 +01:00
|
|
|
/**
|
|
|
|
* ### Whether the current component is running in the background
|
|
|
|
*
|
|
|
|
* for handling problems caused by the keepalive function
|
|
|
|
*/
|
|
|
|
export function useDeactivated() {
|
|
|
|
const deactivated = ref(false)
|
|
|
|
onActivated(() => deactivated.value = false)
|
|
|
|
onDeactivated(() => deactivated.value = true)
|
|
|
|
|
|
|
|
return deactivated
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### When the component is restored from the background
|
|
|
|
*
|
|
|
|
* for handling problems caused by the keepalive function
|
|
|
|
*/
|
|
|
|
export function onReactivated(hook: Function, target?: ComponentInternalInstance | null): void {
|
|
|
|
const initial = ref(true)
|
|
|
|
onActivated(() => {
|
|
|
|
if (initial.value)
|
|
|
|
return
|
|
|
|
hook()
|
|
|
|
}, target)
|
|
|
|
onDeactivated(() => initial.value = false)
|
|
|
|
}
|