fix: switching between databases might trigger errors #105

This commit is contained in:
Lykin 2023-12-29 00:14:56 +08:00
parent 10ec866037
commit df3865dc7d
1 changed files with 14 additions and 20 deletions

View File

@ -226,33 +226,27 @@ const useBrowserStore = defineStore('browser', {
if (isEmpty(db)) { if (isEmpty(db)) {
throw new Error('no db loaded') throw new Error('no db loaded')
} }
const dbs = []
let containLastDB = false
const serverInst = new RedisServerState({ const serverInst = new RedisServerState({
name, name,
separator: this.getSeparator(name), separator: this.getSeparator(name),
db: -1,
}) })
/** @type RedisDatabaseItem[] **/ /** @type {Object.<number,RedisDatabaseItem>} **/
const databases = [] const databases = {}
for (const dbItem of db) {
for (let i = 0; i < db.length; i++) { databases[dbItem.index] = new RedisDatabaseItem({
databases.push( db: dbItem.index,
new RedisDatabaseItem({ maxKeys: dbItem.maxKeys,
db: db[i].index, })
maxKeys: db[i].maxKeys, if (dbItem.index === lastDB) {
}), // set last opened database as default
) serverInst.db = dbItem.index
if (db[i].index === lastDB) { } else if (serverInst.db === -1) {
containLastDB = true // set the first database as default
serverInst.db = dbItem.index
} }
} }
serverInst.databases = databases serverInst.databases = databases
// get last selected db
if (containLastDB) {
serverInst.db = lastDB
} else {
serverInst.db = get(dbs, '0.db', 0)
}
this.servers[name] = serverInst this.servers[name] = serverInst
}, },