mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-04 10:04:39 +02:00
Remove old code for downloads, user progress, sql, server config. Add web plugin for DbManager
This commit is contained in:
parent
9fd3dc6978
commit
4b834cb5c1
25 changed files with 106 additions and 2901 deletions
63
plugins/capacitor/DbManager.js
Normal file
63
plugins/capacitor/DbManager.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { registerPlugin, Capacitor, WebPlugin } from '@capacitor/core';
|
||||
|
||||
class DbWeb extends WebPlugin {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async getDeviceData_WV() {
|
||||
var dd = localStorage.getItem('device')
|
||||
if (dd) {
|
||||
return JSON.parse(dd)
|
||||
}
|
||||
const deviceData = {
|
||||
serverConnectionConfigs: [],
|
||||
lastServerConnectionConfigId: null,
|
||||
localLibraryItemIdPlaying: null
|
||||
}
|
||||
return deviceData
|
||||
}
|
||||
|
||||
async setCurrentServerConnectionConfig_WV(serverConnectionConfig) {
|
||||
var deviceData = await this.getDeviceData_WV()
|
||||
|
||||
var ssc = deviceData.serverConnectionConfigs.find(_ssc => _ssc.id == serverConnectionConfig.id)
|
||||
if (ssc) {
|
||||
deviceData.lastServerConnectionConfigId = ssc.id
|
||||
ssc.name = `${ssc.address} (${serverConnectionConfig.username})`
|
||||
ssc.token = serverConnectionConfig.token
|
||||
ssc.username = serverConnectionConfig.username
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
} else {
|
||||
ssc = {
|
||||
id: encodeURIComponent(Buffer.from(`${serverConnectionConfig.address}@${serverConnectionConfig.username}`).toString('base64')),
|
||||
index: deviceData.serverConnectionConfigs.length,
|
||||
name: `${serverConnectionConfig.address} (${serverConnectionConfig.username})`,
|
||||
username: serverConnectionConfig.username,
|
||||
address: serverConnectionConfig.address,
|
||||
token: serverConnectionConfig.token
|
||||
}
|
||||
deviceData.serverConnectionConfigs.push(ssc)
|
||||
deviceData.lastServerConnectionConfigId = ssc.id
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
}
|
||||
return ssc
|
||||
}
|
||||
|
||||
async removeServerConnectionConfig_WV(serverConnectionConfigCallObject) {
|
||||
var serverConnectionConfigId = serverConnectionConfigCallObject.serverConnectionConfigId
|
||||
var deviceData = await this.getDeviceData_WV()
|
||||
deviceData.serverConnectionConfigs = deviceData.serverConnectionConfigs.filter(ssc => ssc.id == serverConnectionConfigId)
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
}
|
||||
|
||||
logout_WV() {
|
||||
// Nothing to do on web
|
||||
}
|
||||
}
|
||||
|
||||
const DbManager = registerPlugin('DbManager', {
|
||||
web: () => new DbWeb()
|
||||
})
|
||||
|
||||
export { DbManager }
|
|
@ -1,7 +1,7 @@
|
|||
import { registerPlugin, Capacitor } from '@capacitor/core';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { DbManager } from './capacitor/DbManager'
|
||||
|
||||
const isWeb = Capacitor.getPlatform() == 'web'
|
||||
const DbManager = registerPlugin('DbManager')
|
||||
|
||||
class DbService {
|
||||
constructor() { }
|
||||
|
@ -27,7 +27,6 @@ class DbService {
|
|||
}
|
||||
|
||||
getDeviceData() {
|
||||
if (isWeb) return {}
|
||||
return DbManager.getDeviceData_WV().then((data) => {
|
||||
console.log('Loaded device data', JSON.stringify(data))
|
||||
return data
|
||||
|
@ -35,7 +34,6 @@ class DbService {
|
|||
}
|
||||
|
||||
setServerConnectionConfig(serverConnectionConfig) {
|
||||
if (isWeb) return null
|
||||
return DbManager.setCurrentServerConnectionConfig_WV(serverConnectionConfig).then((data) => {
|
||||
console.log('Set server connection config', JSON.stringify(data))
|
||||
return data
|
||||
|
@ -43,7 +41,6 @@ class DbService {
|
|||
}
|
||||
|
||||
removeServerConnectionConfig(serverConnectionConfigId) {
|
||||
if (isWeb) return null
|
||||
return DbManager.removeServerConnectionConfig_WV({ serverConnectionConfigId }).then((data) => {
|
||||
console.log('Removed server connection config', serverConnectionConfigId)
|
||||
return true
|
||||
|
@ -51,7 +48,6 @@ class DbService {
|
|||
}
|
||||
|
||||
logout() {
|
||||
if (isWeb) return null
|
||||
return DbManager.logout_WV()
|
||||
}
|
||||
|
||||
|
|
|
@ -3,39 +3,6 @@ import { Storage } from '@capacitor/storage'
|
|||
class LocalStorage {
|
||||
constructor(vuexStore) {
|
||||
this.vuexStore = vuexStore
|
||||
|
||||
this.userAudiobooksLoaded = false
|
||||
this.downloadFolder = null
|
||||
}
|
||||
|
||||
async setDownloadFolder(folderObj) {
|
||||
try {
|
||||
if (folderObj) {
|
||||
await Storage.set({ key: 'downloadFolder', value: JSON.stringify(folderObj) })
|
||||
this.downloadFolder = folderObj
|
||||
this.vuexStore.commit('setDownloadFolder', { ...this.downloadFolder })
|
||||
} else {
|
||||
await Storage.remove({ key: 'downloadFolder' })
|
||||
this.downloadFolder = null
|
||||
this.vuexStore.commit('setDownloadFolder', null)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to set download folder', error)
|
||||
}
|
||||
}
|
||||
|
||||
async getDownloadFolder() {
|
||||
try {
|
||||
var _value = (await Storage.get({ key: 'downloadFolder' }) || {}).value || null
|
||||
if (!_value) return null
|
||||
this.downloadFolder = JSON.parse(_value)
|
||||
this.vuexStore.commit('setDownloadFolder', { ...this.downloadFolder })
|
||||
return this.downloadFolder
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to get download folder', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async setUserSettings(settings) {
|
||||
|
@ -75,46 +42,6 @@ class LocalStorage {
|
|||
}
|
||||
}
|
||||
|
||||
async setCurrent(current) {
|
||||
try {
|
||||
if (current) {
|
||||
await Storage.set({ key: 'current', value: JSON.stringify(current) })
|
||||
} else {
|
||||
await Storage.remove({ key: 'current' })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to set current', error)
|
||||
}
|
||||
}
|
||||
|
||||
async getCurrent() {
|
||||
try {
|
||||
var currentObj = await Storage.get({ key: 'current' }) || {}
|
||||
return currentObj.value ? JSON.parse(currentObj.value) : null
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to get current', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async setBookshelfView(view) {
|
||||
try {
|
||||
await Storage.set({ key: 'bookshelfView', value: view })
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to set bookshelf view', error)
|
||||
}
|
||||
}
|
||||
|
||||
async getBookshelfView() {
|
||||
try {
|
||||
var view = await Storage.get({ key: 'bookshelfView' }) || {}
|
||||
return view.value || null
|
||||
} catch (error) {
|
||||
console.error('[LocalStorage] Failed to get bookshelf view', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async setUseChapterTrack(useChapterTrack) {
|
||||
try {
|
||||
await Storage.set({ key: 'useChapterTrack', value: useChapterTrack ? '1' : '0' })
|
||||
|
|
|
@ -37,6 +37,11 @@ class ServerSocket extends EventEmitter {
|
|||
this.socket.on('connect', this.onConnect.bind(this))
|
||||
this.socket.on('disconnect', this.onDisconnect.bind(this))
|
||||
this.socket.on('init', this.onInit.bind(this))
|
||||
|
||||
this.socket.onAny((evt, args) => {
|
||||
console.log(`[SOCKET] ${this.socket.id}: ${evt} ${JSON.stringify(args)}`)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
onConnect() {
|
||||
|
|
|
@ -1,555 +0,0 @@
|
|||
import { Capacitor } from '@capacitor/core';
|
||||
import { CapacitorDataStorageSqlite } from 'capacitor-data-storage-sqlite';
|
||||
|
||||
class StoreService {
|
||||
store
|
||||
platform
|
||||
isOpen = false
|
||||
|
||||
constructor(vuexStore) {
|
||||
this.vuexStore = vuexStore
|
||||
this.currentTable = null
|
||||
|
||||
this.lockWaitQueue = []
|
||||
this.isLocked = false
|
||||
this.lockedFor = null
|
||||
|
||||
this.init()
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin Initialization
|
||||
*/
|
||||
init() {
|
||||
this.platform = Capacitor.getPlatform()
|
||||
this.store = CapacitorDataStorageSqlite
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a Store
|
||||
* @param _dbName string optional
|
||||
* @param _table string optional
|
||||
* @param _encrypted boolean optional
|
||||
* @param _mode string optional
|
||||
*/
|
||||
async openStore(_dbName, _table, _encrypted, _mode) {
|
||||
if (this.store != null) {
|
||||
const database = _dbName ? _dbName : "storage"
|
||||
const table = _table ? _table : "storage_table"
|
||||
const encrypted = _encrypted ? _encrypted : false
|
||||
const mode = _mode ? _mode : "no-encryption"
|
||||
|
||||
this.isOpen = false
|
||||
try {
|
||||
await this.store.openStore({ database, table, encrypted, mode })
|
||||
// return Promise.resolve()
|
||||
this.currentTable = table
|
||||
this.isOpen = true
|
||||
return true
|
||||
} catch (err) {
|
||||
// return Promise.reject(err)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
// return Promise.reject(new Error("openStore: Store not opened"))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a store
|
||||
* @param dbName
|
||||
* @returns
|
||||
*/
|
||||
async closeStore(dbName) {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
await this.store.closeStore({ database: dbName })
|
||||
return Promise.resolve()
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("close: Store not opened"))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a store is opened
|
||||
* @param dbName
|
||||
* @returns
|
||||
*/
|
||||
async isStoreOpen(dbName) {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
const ret = await this.store.isStoreOpen({ database: dbName })
|
||||
return Promise.resolve(ret)
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isStoreOpen: Store not opened"))
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Check if a store already exists
|
||||
* @param dbName
|
||||
* @returns
|
||||
*/
|
||||
async isStoreExists(dbName) {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
const ret = await this.store.isStoreExists({ database: dbName })
|
||||
return Promise.resolve(ret)
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isStoreExists: Store not opened"))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/Set a Table
|
||||
* @param table string
|
||||
*/
|
||||
async setTable(table) {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
await this.store.setTable({ table })
|
||||
this.currentTable = table
|
||||
return Promise.resolve()
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("setTable: Store not opened"))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set of Key
|
||||
* @param key string
|
||||
* @param value string
|
||||
*/
|
||||
async setItem(key, value) {
|
||||
if (this.store != null) {
|
||||
if (key.length > 0) {
|
||||
try {
|
||||
await this.store.set({ key, value });
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("setItem: Must give a key"));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("setItem: Store not opened"));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get the Value for a given Key
|
||||
* @param key string
|
||||
*/
|
||||
async getItem(key) {
|
||||
if (this.store != null) {
|
||||
if (key.length > 0) {
|
||||
try {
|
||||
const { value } = await this.store.get({ key });
|
||||
console.log("in getItem value ", value)
|
||||
return Promise.resolve(value);
|
||||
} catch (err) {
|
||||
console.error(`in getItem key: ${key} err: ${JSON.stringify(err)}`)
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("getItem: Must give a key"));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("getItem: Store not opened"));
|
||||
}
|
||||
|
||||
}
|
||||
async isKey(key) {
|
||||
if (this.store != null) {
|
||||
if (key.length > 0) {
|
||||
try {
|
||||
const { result } = await this.store.iskey({ key });
|
||||
return Promise.resolve(result);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isKey: Must give a key"));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isKey: Store not opened"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async getAllKeysValues() {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
const { keysvalues } = await this.store.keysvalues();
|
||||
return Promise.resolve(keysvalues);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("getAllKeysValues: Store not opened"));
|
||||
}
|
||||
}
|
||||
async removeItem(key) {
|
||||
if (this.store != null) {
|
||||
if (key.length > 0) {
|
||||
try {
|
||||
await this.store.remove({ key });
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("removeItem: Must give a key"));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("removeItem: Store not opened"));
|
||||
}
|
||||
}
|
||||
async clear() {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
await this.store.clear()
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('[SqlStore] Failed to clear table', err.message)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
console.error('[SqlStore] Clear: Store not opened')
|
||||
return false
|
||||
}
|
||||
}
|
||||
async deleteStore(_dbName) {
|
||||
const database = _dbName ? _dbName : "storage"
|
||||
|
||||
if (this.store != null) {
|
||||
try {
|
||||
await this.store.deleteStore({ database })
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
return Promise.reject(err.message)
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("deleteStore: Store not opened"));
|
||||
}
|
||||
}
|
||||
async isTable(table) {
|
||||
if (this.store != null) {
|
||||
if (table.length > 0) {
|
||||
try {
|
||||
const { result } = await this.store.isTable({ table });
|
||||
return Promise.resolve(result);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isTable: Must give a table"));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("isTable: Store not opened"));
|
||||
}
|
||||
}
|
||||
async getAllTables() {
|
||||
if (this.store != null) {
|
||||
try {
|
||||
const { tables } = await this.store.tables();
|
||||
return Promise.resolve(tables);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error("getAllTables: Store not opened"));
|
||||
}
|
||||
}
|
||||
|
||||
getLockId(prefix) {
|
||||
return prefix + '-' + Math.floor(Math.random() * 100000000).toString(32)
|
||||
}
|
||||
|
||||
waitForLock(id, count = 0) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
if (!this.lockWaitQueue.includes(id)) {
|
||||
resolve(true)
|
||||
} else {
|
||||
if (count > 200) {
|
||||
console.error('[SqlStore] Lock was never released', id)
|
||||
resolve(false)
|
||||
} else {
|
||||
resolve(this.waitForLock(id, ++count))
|
||||
}
|
||||
}
|
||||
}, 50)
|
||||
})
|
||||
}
|
||||
|
||||
setLock(prefix) {
|
||||
this.lockedFor = prefix
|
||||
this.isLocked = true
|
||||
console.log('[SqlStore] Locked for', this.lockedFor)
|
||||
}
|
||||
|
||||
initWaitLock(prefix) {
|
||||
var lockId = this.getLockId(prefix)
|
||||
this.lockWaitQueue.push(lockId)
|
||||
console.log('[SqlStore] Waiting for lock', lockId, 'In queue', this.lockWaitQueue.length)
|
||||
return this.waitForLock(lockId)
|
||||
}
|
||||
|
||||
releaseLock() {
|
||||
console.log('[SqlStore] Releasing lock', this.lockedFor)
|
||||
if (!this.lockWaitQueue.length) {
|
||||
console.log('[SqlStore] Release Lock no queue')
|
||||
this.isLocked = false
|
||||
}
|
||||
else {
|
||||
console.log('[SqlStore] Release Lock Queue:', this.lockWaitQueue.length)
|
||||
var task = this.lockWaitQueue.shift()
|
||||
console.log('[SqlStore] Released lock next task', task)
|
||||
}
|
||||
}
|
||||
|
||||
async ensureTable(tablename) {
|
||||
if (!this.isOpen) {
|
||||
var success = await this.openStore('storage', tablename)
|
||||
if (!success) {
|
||||
console.error('Store failed to open')
|
||||
return false
|
||||
}
|
||||
}
|
||||
try {
|
||||
await this.setTable(tablename)
|
||||
console.log('[SqlStore] Set Table ' + this.currentTable)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('Failed to set table', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async setDownload(download) {
|
||||
if (!download) return false
|
||||
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('setdl')
|
||||
} else {
|
||||
this.setLock('setdl')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('downloads'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
if (!download.id) {
|
||||
console.error(`[SqlStore] set download invalid download ${download ? JSON.stringify(download) : 'null'}`)
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var success = false
|
||||
try {
|
||||
await this.setItem(download.id, JSON.stringify(download))
|
||||
console.log(`[STORE] Set Download ${download.id}`)
|
||||
success = true
|
||||
} catch (error) {
|
||||
console.error('Failed to set download in store', error)
|
||||
}
|
||||
this.releaseLock()
|
||||
return success
|
||||
}
|
||||
|
||||
async removeDownload(id) {
|
||||
if (!id) return false
|
||||
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('remdl')
|
||||
} else {
|
||||
this.setLock('remdl')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('downloads'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var success = false
|
||||
try {
|
||||
await this.removeItem(id)
|
||||
console.log(`[STORE] Removed download ${id}`)
|
||||
success = true
|
||||
} catch (error) {
|
||||
console.error('Failed to remove download in store', error)
|
||||
}
|
||||
this.releaseLock()
|
||||
return success
|
||||
}
|
||||
|
||||
async getAllDownloads() {
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('alldl')
|
||||
} else {
|
||||
this.setLock('alldl')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('downloads'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var keysvalues = await this.getAllKeysValues()
|
||||
var downloads = []
|
||||
|
||||
for (let i = 0; i < keysvalues.length; i++) {
|
||||
try {
|
||||
var download = JSON.parse(keysvalues[i].value)
|
||||
if (!download.id) {
|
||||
console.error('[SqlStore] Removing invalid download', JSON.stringify(download))
|
||||
await this.removeItem(keysvalues[i].key)
|
||||
} else {
|
||||
downloads.push(download)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to parse download', error)
|
||||
await this.removeItem(keysvalues[i].key)
|
||||
}
|
||||
}
|
||||
|
||||
this.releaseLock()
|
||||
return downloads
|
||||
}
|
||||
|
||||
async setUserAudiobookData(userAudiobookData) {
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('setuad')
|
||||
} else {
|
||||
this.setLock('setuad')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('userAudiobookData'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var success = false
|
||||
try {
|
||||
await this.setItem(userAudiobookData.audiobookId, JSON.stringify(userAudiobookData))
|
||||
this.vuexStore.commit('user/setUserAudiobookData', userAudiobookData)
|
||||
|
||||
console.log(`[STORE] Set UserAudiobookData ${userAudiobookData.audiobookId}`)
|
||||
success = true
|
||||
} catch (error) {
|
||||
console.error('Failed to set UserAudiobookData in store', error)
|
||||
}
|
||||
this.releaseLock()
|
||||
return success
|
||||
}
|
||||
|
||||
async removeUserAudiobookData(audiobookId) {
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('remuad')
|
||||
} else {
|
||||
this.setLock('remuad')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('userAudiobookData'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var success = false
|
||||
try {
|
||||
await this.removeItem(audiobookId)
|
||||
this.vuexStore.commit('user/removeUserAudiobookData', audiobookId)
|
||||
|
||||
console.log(`[STORE] Removed userAudiobookData ${id}`)
|
||||
success = true
|
||||
} catch (error) {
|
||||
console.error('Failed to remove userAudiobookData in store', error)
|
||||
}
|
||||
this.releaseLock()
|
||||
return success
|
||||
}
|
||||
|
||||
async getAllUserAudiobookData() {
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('alluad')
|
||||
} else {
|
||||
this.setLock('alluad')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('userAudiobookData'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
var keysvalues = await this.getAllKeysValues()
|
||||
var data = []
|
||||
|
||||
for (let i = 0; i < keysvalues.length; i++) {
|
||||
try {
|
||||
var abdata = JSON.parse(keysvalues[i].value)
|
||||
if (!abdata.audiobookId) {
|
||||
console.error('[SqlStore] Removing invalid user audiobook data')
|
||||
await this.removeItem(keysvalues[i].key)
|
||||
} else {
|
||||
data.push(abdata)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to parse userAudiobookData', error)
|
||||
await this.removeItem(keysvalues[i].key)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[SqlStore] All UAD finished')
|
||||
this.releaseLock()
|
||||
return data
|
||||
}
|
||||
|
||||
async setAllUserAudiobookData(userAbData) {
|
||||
if (this.isLocked) {
|
||||
await this.initWaitLock('setuad')
|
||||
} else {
|
||||
this.setLock('setuad')
|
||||
}
|
||||
|
||||
if (!(await this.ensureTable('userAudiobookData'))) {
|
||||
this.releaseLock()
|
||||
return false
|
||||
}
|
||||
|
||||
console.log('[SqlStore] Setting all user audiobook data ' + userAbData.length)
|
||||
|
||||
var success = await this.clear()
|
||||
if (!success) {
|
||||
console.error('[SqlStore] Did not clear old user ab data, overwriting')
|
||||
}
|
||||
|
||||
for (let i = 0; i < userAbData.length; i++) {
|
||||
try {
|
||||
var abdata = userAbData[i]
|
||||
await this.setItem(abdata.audiobookId, JSON.stringify(abdata))
|
||||
} catch (error) {
|
||||
console.error('[SqlStore] Failed to set userAudiobookData', error)
|
||||
}
|
||||
}
|
||||
|
||||
this.vuexStore.commit('user/setAllUserAudiobookData', userAbData)
|
||||
this.releaseLock()
|
||||
}
|
||||
}
|
||||
|
||||
export default ({ app, store }, inject) => {
|
||||
inject('sqlStore', new StoreService(store))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue