decrease refresh interval if app is hidden

This commit is contained in:
korelstar 2021-05-02 10:44:33 +02:00
parent c0459e576d
commit a771b7a71a
3 changed files with 45 additions and 2 deletions

View File

@ -77,6 +77,7 @@ export default {
undoNotification: null, undoNotification: null,
undoTimer: null, undoTimer: null,
deletedNotes: [], deletedNotes: [],
refreshTimer: null,
} }
}, },
@ -118,9 +119,15 @@ export default {
created() { created() {
store.commit('setDocumentTitle', document.title) store.commit('setDocumentTitle', document.title)
window.addEventListener('beforeunload', this.onClose) window.addEventListener('beforeunload', this.onClose)
document.addEventListener('visibilitychange', this.onVisibilityChange)
this.loadNotes() this.loadNotes()
}, },
destroyed() {
document.removeEventListener('visibilitychange', this.onVisibilityChange)
this.stopRefreshTimer()
},
methods: { methods: {
loadNotes() { loadNotes() {
fetchNotes() fetchNotes()
@ -147,10 +154,34 @@ export default {
}) })
.then(() => { .then(() => {
this.loading.notes = false this.loading.notes = false
setTimeout(this.loadNotes, config.interval.notes.refresh * 1000) this.startRefreshTimer(config.interval.notes.refresh)
}) })
}, },
startRefreshTimer(seconds) {
if (this.refreshTimer === null && document.visibilityState === 'visible') {
this.refreshTimer = setTimeout(() => {
this.refreshTimer = null
this.loadNotes()
}, seconds * 1000)
}
},
stopRefreshTimer() {
if (this.refreshTimer !== null) {
clearTimeout(this.refreshTimer)
this.refreshTimer = null
}
},
onVisibilityChange() {
if (document.visibilityState === 'visible') {
this.startRefreshTimer(config.interval.notes.refreshAfterHidden)
} else {
this.stopRefreshTimer()
}
},
reloadNotes() { reloadNotes() {
if (this.$route.path !== '/') { if (this.$route.path !== '/') {
this.$router.push('/') this.$router.push('/')

View File

@ -174,6 +174,7 @@ export default {
document.addEventListener('mozfullscreenchange', this.onDetectFullscreen) document.addEventListener('mozfullscreenchange', this.onDetectFullscreen)
document.addEventListener('fullscreenchange', this.onDetectFullscreen) document.addEventListener('fullscreenchange', this.onDetectFullscreen)
document.addEventListener('keydown', this.onKeyPress) document.addEventListener('keydown', this.onKeyPress)
document.addEventListener('visibilitychange', this.onVisibilityChange)
}, },
destroyed() { destroyed() {
@ -182,6 +183,7 @@ export default {
document.removeEventListener('mozfullscreenchange', this.onDetectFullscreen) document.removeEventListener('mozfullscreenchange', this.onDetectFullscreen)
document.removeEventListener('fullscreenchange', this.onDetectFullscreen) document.removeEventListener('fullscreenchange', this.onDetectFullscreen)
document.removeEventListener('keydown', this.onKeyPress) document.removeEventListener('keydown', this.onKeyPress)
document.removeEventListener('visibilitychange', this.onVisibilityChange)
store.commit('setSidebarOpen', false) store.commit('setSidebarOpen', false)
this.onUpdateTitle(null) this.onUpdateTitle(null)
}, },
@ -268,6 +270,13 @@ export default {
this.actionsOpen = false this.actionsOpen = false
}, },
onVisibilityChange() {
if (document.visibilityState === 'visible') {
this.stopRefreshTimer()
this.refreshNote()
}
},
stopRefreshTimer() { stopRefreshTimer() {
if (this.refreshTimer !== null) { if (this.refreshTimer !== null) {
clearTimeout(this.refreshTimer) clearTimeout(this.refreshTimer)
@ -277,10 +286,11 @@ export default {
startRefreshTimer() { startRefreshTimer() {
this.stopRefreshTimer() this.stopRefreshTimer()
const interval = document.visibilityState === 'visible' ? config.interval.note.refresh : config.interval.note.refreshHidden
this.refreshTimer = setTimeout(() => { this.refreshTimer = setTimeout(() => {
this.refreshTimer = null this.refreshTimer = null
this.refreshNote() this.refreshNote()
}, config.interval.note.refresh * 1000) }, interval * 1000)
}, },
refreshNote() { refreshNote() {

View File

@ -4,9 +4,11 @@ export const config = {
autosave: 1, autosave: 1,
autotitle: 2, autotitle: 2,
refresh: 10, refresh: 10,
refreshHidden: 120,
}, },
notes: { notes: {
refresh: 25, refresh: 25,
refreshAfterHidden: 2,
}, },
}, },
} }