Fall back to memory if indexedDB is unavailable
This commit is contained in:
parent
cb5e6f1848
commit
4cd7ca2cb4
|
@ -86,7 +86,7 @@ front/tests/e2e/reports
|
||||||
front/selenium-debug.log
|
front/selenium-debug.log
|
||||||
docs/_build
|
docs/_build
|
||||||
|
|
||||||
data/
|
/data/
|
||||||
.env
|
.env
|
||||||
|
|
||||||
po/*.po
|
po/*.po
|
||||||
|
|
|
@ -3,9 +3,9 @@ import type { Track, Upload } from '~/types'
|
||||||
import { createGlobalState, useNow, useStorage, useTimeAgo, whenever } from '@vueuse/core'
|
import { createGlobalState, useNow, useStorage, useTimeAgo, whenever } from '@vueuse/core'
|
||||||
import { shuffle as shuffleArray, sum } from 'lodash-es'
|
import { shuffle as shuffleArray, sum } from 'lodash-es'
|
||||||
import { computed, ref, shallowReactive, watchEffect } from 'vue'
|
import { computed, ref, shallowReactive, watchEffect } from 'vue'
|
||||||
import { delMany, getMany, setMany } from 'idb-keyval'
|
|
||||||
import { useClamp } from '@vueuse/math'
|
import { useClamp } from '@vueuse/math'
|
||||||
|
|
||||||
|
import { delMany, getMany, setMany } from '~/composables/data/indexedDB'
|
||||||
import { looping, LoopingMode, isPlaying } from '~/composables/audio/player'
|
import { looping, LoopingMode, isPlaying } from '~/composables/audio/player'
|
||||||
import { useStore } from '~/store'
|
import { useStore } from '~/store'
|
||||||
|
|
||||||
|
@ -106,6 +106,7 @@ export const useQueue = createGlobalState(() => {
|
||||||
|
|
||||||
const createQueueTrack = async (track: Track): Promise<QueueTrack> => {
|
const createQueueTrack = async (track: Track): Promise<QueueTrack> => {
|
||||||
const { $pgettext } = gettext
|
const { $pgettext } = gettext
|
||||||
|
const { default: store } = await import('~/store')
|
||||||
|
|
||||||
if (track.uploads.length === 0) {
|
if (track.uploads.length === 0) {
|
||||||
// we don't have any information for this track, we need to fetch it
|
// we don't have any information for this track, we need to fetch it
|
||||||
|
@ -124,13 +125,13 @@ export const useQueue = createGlobalState(() => {
|
||||||
artistId: track.artist?.id ?? -1,
|
artistId: track.artist?.id ?? -1,
|
||||||
albumId: track.album?.id ?? -1,
|
albumId: track.album?.id ?? -1,
|
||||||
coverUrl: (track.cover?.urls ?? track.album?.cover?.urls ?? track.artist?.cover?.urls)?.original
|
coverUrl: (track.cover?.urls ?? track.album?.cover?.urls ?? track.artist?.cover?.urls)?.original
|
||||||
?? new URL('~/assets/audio/default-cover.png', import.meta.url).href,
|
?? new URL('../../assets/audio/default-cover.png', import.meta.url).href,
|
||||||
sources: track.uploads.map(upload => ({
|
sources: track.uploads.map(upload => ({
|
||||||
uuid: upload.uuid,
|
uuid: upload.uuid,
|
||||||
duration: upload.duration,
|
duration: upload.duration,
|
||||||
mimetype: upload.mimetype,
|
mimetype: upload.mimetype,
|
||||||
bitrate: upload.bitrate,
|
bitrate: upload.bitrate,
|
||||||
url: upload.listen_url
|
url: store.getters['instance/absoluteUrl'](upload.listen_url)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { usePlayer } from '~/composables/audio/player'
|
||||||
import { useQueue } from '~/composables/audio/queue'
|
import { useQueue } from '~/composables/audio/queue'
|
||||||
import { soundImplementation } from '~/api/player'
|
import { soundImplementation } from '~/api/player'
|
||||||
|
|
||||||
import useLRUCache from '~/composables/useLRUCache'
|
import useLRUCache from '~/composables/data/useLRUCache'
|
||||||
import store from '~/store'
|
import store from '~/store'
|
||||||
|
|
||||||
const ALLOWED_PLAY_TYPES: (CanPlayTypeResult | undefined)[] = ['maybe', 'probably']
|
const ALLOWED_PLAY_TYPES: (CanPlayTypeResult | undefined)[] = ['maybe', 'probably']
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
import {
|
||||||
|
setMany as idbSetMany,
|
||||||
|
getMany as idbGetMany,
|
||||||
|
delMany as idbDelMany
|
||||||
|
} from 'idb-keyval'
|
||||||
|
|
||||||
|
const inMemoryKeyValueStore = new Map<IDBValidKey, any>()
|
||||||
|
const canUseIndexedDB = new Promise<boolean>(resolve => {
|
||||||
|
if (!window.indexedDB) return resolve(false)
|
||||||
|
|
||||||
|
const request = indexedDB.open('indexed-db-available', 3)
|
||||||
|
request.onsuccess = () => resolve(true)
|
||||||
|
request.onerror = () => resolve(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const setMany = async (entries: [IDBValidKey, any][]) => {
|
||||||
|
if (await canUseIndexedDB) {
|
||||||
|
return idbSetMany(entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const entry of entries) {
|
||||||
|
inMemoryKeyValueStore.set(...entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getMany = async (keys: IDBValidKey[]) => {
|
||||||
|
if (await canUseIndexedDB) {
|
||||||
|
return idbGetMany(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys.map(key => inMemoryKeyValueStore.get(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const delMany = async (keys: IDBValidKey[]) => {
|
||||||
|
if (await canUseIndexedDB) {
|
||||||
|
return idbDelMany(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const key of keys) {
|
||||||
|
inMemoryKeyValueStore.delete(key)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue