fix: handle frozen state (#1792)

This commit is contained in:
Joaquín Sánchez 2023-02-25 12:09:24 +01:00 committed by GitHub
parent d810b2de3a
commit 3596d8bec3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

2
page-lifecycle.d.ts vendored
View file

@ -1,5 +1,5 @@
declare module 'page-lifecycle/dist/lifecycle.mjs' {
type PageLifecycleState = 'pageshow' | 'resume' | 'focus' | 'blur' | 'pagehide' | 'unload' | 'visibilitychange' | 'freeze'
type PageLifecycleState = 'active' | 'passive' | 'hidden' | 'frozen' | 'terminated'
interface PageLifecycleEvent extends Event {
newState: PageLifecycleState

View file

@ -5,12 +5,22 @@ import { closeDatabases } from '~/utils/elk-idb'
export default defineNuxtPlugin(() => {
const state = ref(lifecycle.state)
const frozenListeners: (() => void)[] = []
const frozenState = useLocalStorage(ELK_PAGE_LIFECYCLE_FROZEN, false)
lifecycle.addEventListener('statechange', (evt) => {
if (evt.newState === 'freeze')
if (evt.newState === 'hidden' && evt.oldState === 'frozen') {
frozenState.value = false
nextTick().then(() => window.location.reload())
return
}
if (evt.newState === 'frozen') {
frozenState.value = true
frozenListeners.forEach(listener => listener())
else
}
else {
state.value = evt.newState
}
})
const addFrozenListener = (listener: () => void) => {