forked from platypush/platypush
[#260] A simple entities caching mechanism using the browser storage.
This commit is contained in:
parent
74ab884b7a
commit
78c12212c6
1 changed files with 33 additions and 5 deletions
|
@ -252,7 +252,8 @@ export default {
|
|||
await this.request('entities.scan', args)
|
||||
},
|
||||
|
||||
async sync() {
|
||||
async sync(setLoading=true) {
|
||||
if (setLoading)
|
||||
this.loading = true
|
||||
|
||||
try {
|
||||
|
@ -270,7 +271,9 @@ export default {
|
|||
}, {})
|
||||
|
||||
this.selector.selectedEntities = this.entityGroups.id
|
||||
this.refreshEntitiesCache()
|
||||
} finally {
|
||||
if (setLoading)
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
@ -362,6 +365,23 @@ export default {
|
|||
this.modalVisible = false
|
||||
}
|
||||
},
|
||||
|
||||
loadCachedEntities() {
|
||||
const cachedEntities = localStorage.getItem('entities')
|
||||
if (cachedEntities) {
|
||||
this.entities = JSON.parse(cachedEntities)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
},
|
||||
|
||||
refreshEntitiesCache() {
|
||||
if (this.loading)
|
||||
return
|
||||
|
||||
window.localStorage.setItem('entities', JSON.stringify(this.entities))
|
||||
},
|
||||
},
|
||||
|
||||
async mounted() {
|
||||
|
@ -377,8 +397,16 @@ export default {
|
|||
'platypush.message.event.entities.EntityDeleteEvent'
|
||||
)
|
||||
|
||||
if (!this.loadCachedEntities()) {
|
||||
await this.sync()
|
||||
await this.refresh()
|
||||
} else {
|
||||
this.refresh()
|
||||
this.sync(false).then(() => this.refresh())
|
||||
}
|
||||
|
||||
// Refresh the entities cache every 10 seconds
|
||||
setInterval(() => this.refreshEntitiesCache(), 10000)
|
||||
},
|
||||
|
||||
unmounted() {
|
||||
|
|
Loading…
Reference in a new issue