Better entities caching on the frontend.

This commit is contained in:
Fabio Manganiello 2023-05-12 03:18:22 +02:00
parent 79871e0fa1
commit 6cd9cb6e76
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -252,9 +252,8 @@ export default {
await this.request('entities.scan', args) await this.request('entities.scan', args)
}, },
async sync(setLoading=true) { async sync() {
if (setLoading) this.loading = true
this.loading = true
try { try {
this.entities = (await this.request('entities.get')).reduce((obj, entity) => { this.entities = (await this.request('entities.get')).reduce((obj, entity) => {
@ -273,8 +272,7 @@ export default {
this.selector.selectedEntities = this.entityGroups.id this.selector.selectedEntities = this.entityGroups.id
this.refreshEntitiesCache() this.refreshEntitiesCache()
} finally { } finally {
if (setLoading) this.loading = false
this.loading = false
} }
}, },
@ -367,10 +365,14 @@ export default {
}, },
loadCachedEntities() { loadCachedEntities() {
const cachedEntities = localStorage.getItem('entities') const cachedEntities = window.localStorage.getItem('entities')
if (cachedEntities) { if (cachedEntities) {
this.entities = JSON.parse(cachedEntities) this.entities = JSON.parse(cachedEntities)
return true if (this.entities) {
Object.values(this.entities).forEach((entity) => this.onEntityUpdate({entity: entity}))
this.selector.selectedEntities = this.entityGroups.id
return true
}
} }
return false return false
@ -401,11 +403,9 @@ export default {
await this.sync() await this.sync()
await this.refresh() await this.refresh()
} else { } else {
this.refresh() await this.request('entities.scan')
this.sync(false).then(() => this.refresh())
} }
// Refresh the entities cache every 10 seconds
setInterval(() => this.refreshEntitiesCache(), 10000) setInterval(() => this.refreshEntitiesCache(), 10000)
}, },