2023-02-06 22:22:56 +00:00
|
|
|
import lifecycle from 'page-lifecycle/dist/lifecycle.mjs'
|
|
|
|
import { ELK_PAGE_LIFECYCLE_FROZEN } from '~/constants'
|
|
|
|
import { closeDatabases } from '~/utils/elk-idb'
|
|
|
|
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
|
|
const state = ref(lifecycle.state)
|
|
|
|
const frozenListeners: (() => void)[] = []
|
2023-02-25 11:09:24 +00:00
|
|
|
const frozenState = useLocalStorage(ELK_PAGE_LIFECYCLE_FROZEN, false)
|
2023-02-06 22:22:56 +00:00
|
|
|
|
|
|
|
lifecycle.addEventListener('statechange', (evt) => {
|
2023-02-25 11:09:24 +00:00
|
|
|
if (evt.newState === 'hidden' && evt.oldState === 'frozen') {
|
|
|
|
frozenState.value = false
|
|
|
|
nextTick().then(() => window.location.reload())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evt.newState === 'frozen') {
|
|
|
|
frozenState.value = true
|
2023-02-06 22:22:56 +00:00
|
|
|
frozenListeners.forEach(listener => listener())
|
2023-02-25 11:09:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-02-06 22:22:56 +00:00
|
|
|
state.value = evt.newState
|
2023-02-25 11:09:24 +00:00
|
|
|
}
|
2023-02-06 22:22:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const addFrozenListener = (listener: () => void) => {
|
|
|
|
frozenListeners.push(listener)
|
|
|
|
}
|
|
|
|
|
2023-02-06 23:01:30 +00:00
|
|
|
addFrozenListener(() => {
|
|
|
|
if (useAppConfig().pwaEnabled && navigator.serviceWorker.controller)
|
|
|
|
navigator.serviceWorker.controller.postMessage(ELK_PAGE_LIFECYCLE_FROZEN)
|
2023-02-06 22:22:56 +00:00
|
|
|
|
2023-02-06 23:01:30 +00:00
|
|
|
closeDatabases()
|
|
|
|
})
|
2023-02-06 22:22:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
provide: {
|
|
|
|
pageLifecycle: reactive({
|
|
|
|
state,
|
|
|
|
addFrozenListener,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|