From df3865dc7db56de15fc4b2af3f7d26d97854dda8 Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Fri, 29 Dec 2023 00:14:56 +0800 Subject: [PATCH] fix: switching between databases might trigger errors #105 --- frontend/src/stores/browser.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/frontend/src/stores/browser.js b/frontend/src/stores/browser.js index 54dc5e4..f47baf0 100644 --- a/frontend/src/stores/browser.js +++ b/frontend/src/stores/browser.js @@ -226,33 +226,27 @@ const useBrowserStore = defineStore('browser', { if (isEmpty(db)) { throw new Error('no db loaded') } - const dbs = [] - let containLastDB = false const serverInst = new RedisServerState({ name, separator: this.getSeparator(name), + db: -1, }) - /** @type RedisDatabaseItem[] **/ - const databases = [] - - for (let i = 0; i < db.length; i++) { - databases.push( - new RedisDatabaseItem({ - db: db[i].index, - maxKeys: db[i].maxKeys, - }), - ) - if (db[i].index === lastDB) { - containLastDB = true + /** @type {Object.} **/ + const databases = {} + for (const dbItem of db) { + databases[dbItem.index] = new RedisDatabaseItem({ + db: dbItem.index, + maxKeys: dbItem.maxKeys, + }) + if (dbItem.index === lastDB) { + // set last opened database as default + serverInst.db = dbItem.index + } else if (serverInst.db === -1) { + // set the first database as default + serverInst.db = dbItem.index } } serverInst.databases = databases - // get last selected db - if (containLastDB) { - serverInst.db = lastDB - } else { - serverInst.db = get(dbs, '0.db', 0) - } this.servers[name] = serverInst },