mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-17 00:01:16 +02:00
Fix home page shelves refreshing on network changes
This commit is contained in:
parent
2de81130a1
commit
2b9f5c866b
1 changed files with 61 additions and 17 deletions
|
@ -38,23 +38,34 @@ export default {
|
||||||
return {
|
return {
|
||||||
shelves: [],
|
shelves: [],
|
||||||
loading: false,
|
loading: false,
|
||||||
|
isFirstNetworkConnection: true,
|
||||||
lastServerFetch: 0,
|
lastServerFetch: 0,
|
||||||
|
lastServerFetchLibraryId: null,
|
||||||
|
lastLocalFetch: 0,
|
||||||
localLibraryItems: []
|
localLibraryItems: []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
networkConnected(newVal) {
|
networkConnected(newVal) {
|
||||||
// Update shelves when network connect status changes
|
// Update shelves when network connect status changes
|
||||||
console.log(`[categories] Network changed to ${newVal} - fetch categories`)
|
console.log(`[categories] Network changed to ${newVal} - fetch categories. ${this.lastServerFetch}/${this.lastLocalFetch}`)
|
||||||
|
|
||||||
if (newVal) {
|
if (newVal) {
|
||||||
|
// Fetch right away the first time network connects
|
||||||
|
if (this.isFirstNetworkConnection) {
|
||||||
|
this.isFirstNetworkConnection = false
|
||||||
|
console.log(`[categories] networkConnected true first network connection. lastServerFetch=${this.lastServerFetch}`)
|
||||||
|
this.fetchCategories()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// Using timeout because making this fetch as soon as network gets connected will often fail on Android
|
// Using timeout because making this fetch as soon as network gets connected will often fail on Android
|
||||||
if (!this.lastServerFetch || Date.now() - this.lastServerFetch < 4000) {
|
console.log(`[categories] networkConnected true so fetching categories. lastServerFetch=${this.lastServerFetch}`)
|
||||||
this.fetchCategories()
|
this.fetchCategories()
|
||||||
}
|
|
||||||
}, 4000)
|
}, 4000)
|
||||||
} else {
|
} else {
|
||||||
|
console.log(`[categories] networkConnected false so fetching categories`)
|
||||||
this.fetchCategories()
|
this.fetchCategories()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,25 +185,53 @@ export default {
|
||||||
return categories
|
return categories
|
||||||
},
|
},
|
||||||
async fetchCategories() {
|
async fetchCategories() {
|
||||||
if (this.loading) {
|
console.log(`[categories] fetchCategories networkConnected=${this.networkConnected}, lastServerFetch=${this.lastServerFetch}, lastLocalFetch=${this.lastLocalFetch}`)
|
||||||
console.log('[categories] Already loading categories')
|
|
||||||
|
// TODO: Find a better way to keep the shelf up-to-date with local vs server library because this is a disaster
|
||||||
|
if (this.user && this.currentLibraryId && this.networkConnected) {
|
||||||
|
if (this.lastServerFetch && Date.now() - this.lastServerFetch < 5000 && this.lastServerFetchLibraryId == this.currentLibraryId) {
|
||||||
|
console.log(`[categories] fetchCategories server fetch was ${Date.now() - this.lastServerFetch}ms ago so not doing it.`)
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
console.log(`[categories] fetchCategories fetching from server. Last was ${this.lastServerFetch ? Date.now() - this.lastServerFetch + 'ms' : 'Never'} ago. lastServerFetchLibraryId=${this.lastServerFetchLibraryId} and currentLibraryId=${this.currentLibraryId}`)
|
||||||
|
this.lastServerFetchLibraryId = this.currentLibraryId
|
||||||
|
this.lastServerFetch = Date.now()
|
||||||
|
this.lastLocalFetch = 0
|
||||||
}
|
}
|
||||||
console.log(`[categories] fetchCategories networkConnected=${this.networkConnected}`)
|
} else {
|
||||||
|
if (this.lastLocalFetch && Date.now() - this.lastLocalFetch < 5000) {
|
||||||
|
console.log(`[categories] fetchCategories local fetch was ${Date.now() - this.lastLocalFetch}ms ago so not doing it.`)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
console.log(`[categories] fetchCategories fetching from local. Last was ${this.lastLocalFetch ? Date.now() - this.lastLocalFetch + 'ms' : 'Never'} ago`)
|
||||||
|
this.lastServerFetchLibraryId = null
|
||||||
|
this.lastServerFetch = 0
|
||||||
|
this.lastLocalFetch = Date.now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.loading = true
|
this.loading = true
|
||||||
this.shelves = []
|
this.shelves = []
|
||||||
|
|
||||||
|
if (this.user && this.currentLibraryId && this.networkConnected) {
|
||||||
this.localLibraryItems = await this.$db.getLocalLibraryItems()
|
this.localLibraryItems = await this.$db.getLocalLibraryItems()
|
||||||
const localCategories = await this.getLocalMediaItemCategories()
|
const localCategories = await this.getLocalMediaItemCategories()
|
||||||
|
|
||||||
if (this.user && this.currentLibraryId && this.networkConnected) {
|
|
||||||
this.lastServerFetch = Date.now()
|
|
||||||
const categories = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`).catch((error) => {
|
const categories = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`).catch((error) => {
|
||||||
console.error('Failed to fetch categories', error)
|
console.error('[categories] Failed to fetch categories', error)
|
||||||
return []
|
return []
|
||||||
})
|
})
|
||||||
|
if (!categories.length) {
|
||||||
|
// Failed to load categories so use local shelves
|
||||||
|
console.warn(`[categories] Failed to get server categories so using local categories`)
|
||||||
|
this.shelves = localCategories
|
||||||
|
this.lastServerFetch = 0
|
||||||
|
this.lastLocalFetch = Date.now()
|
||||||
|
this.loading = false
|
||||||
|
console.log('[categories] Local shelves set from failure', this.shelves.length, this.lastLocalFetch)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.shelves = categories.map((cat) => {
|
this.shelves = categories.map((cat) => {
|
||||||
console.log('[categories] Personalized category from server', cat.type)
|
|
||||||
if (cat.type == 'book' || cat.type == 'podcast' || cat.type == 'episode') {
|
if (cat.type == 'book' || cat.type == 'podcast' || cat.type == 'episode') {
|
||||||
// Map localLibraryItem to entities
|
// Map localLibraryItem to entities
|
||||||
cat.entities = cat.entities.map((entity) => {
|
cat.entities = cat.entities.map((entity) => {
|
||||||
|
@ -211,17 +250,21 @@ export default {
|
||||||
// Only add the local shelf with the same media type
|
// Only add the local shelf with the same media type
|
||||||
const localShelves = localCategories.filter((cat) => cat.type === this.currentLibraryMediaType && !cat.localOnly)
|
const localShelves = localCategories.filter((cat) => cat.type === this.currentLibraryMediaType && !cat.localOnly)
|
||||||
this.shelves.push(...localShelves)
|
this.shelves.push(...localShelves)
|
||||||
|
console.log('[categories] Server shelves set', this.shelves.length, this.lastServerFetch)
|
||||||
} else {
|
} else {
|
||||||
// Offline only local
|
// Offline only local
|
||||||
|
this.localLibraryItems = await this.$db.getLocalLibraryItems()
|
||||||
|
const localCategories = await this.getLocalMediaItemCategories()
|
||||||
this.shelves = localCategories
|
this.shelves = localCategories
|
||||||
this.lastServerFetch = 0
|
console.log('[categories] Local shelves set', this.shelves.length, this.lastLocalFetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.loading = false
|
this.loading = false
|
||||||
},
|
},
|
||||||
async libraryChanged() {
|
libraryChanged() {
|
||||||
if (this.currentLibraryId) {
|
if (this.currentLibraryId) {
|
||||||
await this.fetchCategories()
|
console.log(`[categories] libraryChanged so fetching categories`)
|
||||||
|
this.fetchCategories()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
audiobookAdded(audiobook) {
|
audiobookAdded(audiobook) {
|
||||||
|
@ -273,6 +316,7 @@ export default {
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.initListeners()
|
this.initListeners()
|
||||||
|
console.log(`[categories] mounted so fetching categories`)
|
||||||
this.fetchCategories()
|
this.fetchCategories()
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue