Skip to content

Commit e91f529

Browse files
Merge pull request #1370 from jharris4/remember-last-used-db
Use last db instead of default db where possible
2 parents 1cdb13f + d229187 commit e91f529

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/shared/modules/connections/connectionsDuck.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export type ConnectionReduxState = {
7373
connectionState: ConnectionState
7474
lastUpdate: number
7575
useDb: string | null
76+
lastUseDb: string | null
7677
}
7778
type ConnectionState =
7879
| typeof DISCONNECTED_STATE
@@ -103,7 +104,8 @@ const initialState: ConnectionReduxState = {
103104
activeConnection: null,
104105
connectionState: DISCONNECTED_STATE,
105106
lastUpdate: 0,
106-
useDb: null
107+
useDb: null,
108+
lastUseDb: null
107109
}
108110
/**
109111
* Selectors
@@ -119,6 +121,10 @@ export function getConnection(
119121
)
120122
}
121123

124+
export function getLastUseDb(state: GlobalState): string | null {
125+
return (state[NAME] || {}).lastUseDb
126+
}
127+
122128
export function getUseDb(state: GlobalState): string | null {
123129
return (state[NAME] || {}).useDb
124130
}
@@ -286,7 +292,12 @@ export default function(state = initialState, action: any) {
286292
case UPDATE_AUTH_ENABLED:
287293
return updateAuthEnabledHelper(state, action.authEnabled)
288294
case USE_DB:
289-
return { ...state, useDb: action.useDb }
295+
const { useDb } = action
296+
let lastUseDb = useDb
297+
if (useDb === null) {
298+
lastUseDb = state.useDb || state.lastUseDb
299+
}
300+
return { ...state, lastUseDb, useDb }
290301
case USER_CLEAR:
291302
return initialState
292303
default:
@@ -356,6 +367,8 @@ export const setAuthEnabled = (authEnabled: any) => {
356367

357368
export const useDb = (db: any = null) => ({ type: USE_DB, useDb: db })
358369

370+
export const resetUseDb = () => ({ type: USE_DB, useDb: null })
371+
359372
// Epics
360373
export const useDbEpic = (action$: any) => {
361374
return action$
@@ -449,7 +462,7 @@ function shouldTryAutoconnecting(conn: Connection | null): boolean {
449462
export const startupConnectEpic = (action$: any, store: any) => {
450463
return action$
451464
.ofType(discovery.DONE)
452-
.do(() => store.dispatch(useDb(null)))
465+
.do(() => store.dispatch(resetUseDb()))
453466
.mergeMap(async ({ discovered }: DiscoverDataAction) => {
454467
const connectionTimeout = getConnectionTimeout(store.getState())
455468
const savedConnection = getConnection(
@@ -559,7 +572,7 @@ export const disconnectEpic = (action$: any, store: any) => {
559572
.ofType(DISCONNECT)
560573
.merge(action$.ofType(USER_CLEAR))
561574
.do(() => bolt.closeConnection())
562-
.do(() => store.dispatch(useDb(null)))
575+
.do(() => store.dispatch(resetUseDb()))
563576
.do((action: any) =>
564577
store.dispatch(updateConnection({ id: action.id, password: '' }))
565578
)
@@ -569,7 +582,7 @@ export const silentDisconnectEpic = (action$: any, store: any) => {
569582
return action$
570583
.ofType(SILENT_DISCONNECT)
571584
.do(() => bolt.closeConnection())
572-
.do(() => store.dispatch(useDb(null)))
585+
.do(() => store.dispatch(resetUseDb()))
573586
.do(() => store.dispatch({ type: CLEAR_META }))
574587
.mapTo(setActiveConnection(null, true))
575588
}

src/shared/modules/dbMeta/dbMetaDuck.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
setAuthEnabled,
3535
onLostConnection,
3636
getUseDb,
37+
getLastUseDb,
3738
useDb,
3839
getActiveConnectionData,
3940
updateConnection
@@ -452,10 +453,15 @@ const switchToRequestedDb = (store: any) => {
452453
const activeConnection = getActiveConnectionData(store.getState())
453454
const requestedUseDb = activeConnection?.requestedUseDb
454455

455-
const switchToDefaultDb = () => {
456-
const defaultDb = databases.find((db: any) => db.default)
457-
if (defaultDb) {
458-
store.dispatch(useDb(defaultDb.name))
456+
const switchToLastUsedOrDefaultDb = () => {
457+
const lastUsedDb = getLastUseDb(store.getState())
458+
if (lastUsedDb && databases.some((db: any) => db.name === lastUsedDb)) {
459+
store.dispatch(useDb(lastUsedDb))
460+
} else {
461+
const defaultDb = databases.find((db: any) => db.default)
462+
if (defaultDb) {
463+
store.dispatch(useDb(defaultDb.name))
464+
}
459465
}
460466
}
461467

@@ -478,10 +484,10 @@ const switchToRequestedDb = (store: any) => {
478484
store.dispatch(executeCommand(`:use ${requestedUseDb}`), {
479485
source: commandSources.auto
480486
})
481-
switchToDefaultDb()
487+
switchToLastUsedOrDefaultDb()
482488
}
483489
} else {
484-
switchToDefaultDb()
490+
switchToLastUsedOrDefaultDb()
485491
}
486492
return Rx.Observable.of(null)
487493
}

0 commit comments

Comments
 (0)