A less blocking implementation of the entities loading UI logic.

This commit is contained in:
Fabio Manganiello 2023-05-12 03:49:20 +02:00
parent 62d846ddda
commit d4f8e51caf
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -249,11 +249,12 @@ export default {
return obj return obj
}, {}) }, {})
await this.request('entities.scan', args) this.request('entities.scan', args)
}, },
async sync() { async sync(setLoading=true) {
this.loading = true if (setLoading)
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) => {
@ -272,7 +273,8 @@ export default {
this.selector.selectedEntities = this.entityGroups.id this.selector.selectedEntities = this.entityGroups.id
this.refreshEntitiesCache() this.refreshEntitiesCache()
} finally { } finally {
this.loading = false if (setLoading)
this.loading = false
} }
}, },
@ -367,12 +369,18 @@ export default {
loadCachedEntities() { loadCachedEntities() {
const cachedEntities = window.localStorage.getItem('entities') const cachedEntities = window.localStorage.getItem('entities')
if (cachedEntities) { if (cachedEntities) {
this.entities = JSON.parse(cachedEntities) try {
if (this.entities) { this.entities = JSON.parse(cachedEntities)
Object.values(this.entities).forEach((entity) => this.onEntityUpdate({entity: entity})) if (!this.entities)
this.selector.selectedEntities = this.entityGroups.id throw Error('The list of cached entities is null')
return true } catch (e) {
console.warning('Could not parse cached entities', e)
return false
} }
Object.values(this.entities).forEach((entity) => this.onEntityUpdate({entity: entity}))
this.selector.selectedEntities = this.entityGroups.id
return true
} }
return false return false
@ -401,9 +409,10 @@ export default {
if (!this.loadCachedEntities()) { if (!this.loadCachedEntities()) {
await this.sync() await this.sync()
await this.refresh() this.refresh()
} else { } else {
await this.request('entities.scan') await this.request('entities.scan')
this.sync()
} }
setInterval(() => this.refreshEntitiesCache(), 10000) setInterval(() => this.refreshEntitiesCache(), 10000)