2023-02-07 01:22:56 +03: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)[] = []
|
|
|
|
|
|
|
|
lifecycle.addEventListener('statechange', (evt) => {
|
|
|
|
if (evt.newState === 'freeze')
|
|
|
|
frozenListeners.forEach(listener => listener())
|
|
|
|
else
|
|
|
|
state.value = evt.newState
|
|
|
|
})
|
|
|
|
|
|
|
|
const addFrozenListener = (listener: () => void) => {
|
|
|
|
frozenListeners.push(listener)
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:01:30 +03:00
|
|
|
addFrozenListener(() => {
|
|
|
|
if (useAppConfig().pwaEnabled && navigator.serviceWorker.controller)
|
|
|
|
navigator.serviceWorker.controller.postMessage(ELK_PAGE_LIFECYCLE_FROZEN)
|
2023-02-07 01:22:56 +03:00
|
|
|
|
2023-02-07 02:01:30 +03:00
|
|
|
closeDatabases()
|
|
|
|
})
|
2023-02-07 01:22:56 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
provide: {
|
|
|
|
pageLifecycle: reactive({
|
|
|
|
state,
|
|
|
|
addFrozenListener,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|