diff --git a/src/components/SettingsPage/sections/Profile.js b/src/components/SettingsPage/sections/Profile.js index 70f59d2a..7522ad3b 100644 --- a/src/components/SettingsPage/sections/Profile.js +++ b/src/components/SettingsPage/sections/Profile.js @@ -5,11 +5,11 @@ import { connect } from 'react-redux' import { remote } from 'electron' import bcrypt from 'bcryptjs' -import libcoreHardReset from 'commands/libcoreHardReset' import { cleanAccountsCache } from 'actions/accounts' import { unlock } from 'reducers/application' // FIXME should be in actions import db, { setEncryptionKey } from 'helpers/db' import { delay } from 'helpers/promise' +import hardReset from 'helpers/hardReset' import type { SettingsState } from 'reducers/settings' import type { T } from 'types/common' @@ -94,12 +94,8 @@ class TabProfile extends PureComponent { handleHardReset = async () => { this.setState({ isHardResetting: true }) try { - // TODO: wait for the libcoreHardReset to be finished - // actually, libcore doesnt goes back to js thread - await Promise.race([libcoreHardReset.send().toPromise(), delay(500)]) - db.resetAll() - await delay(500) - remote.getCurrentWindow().webContents.reload() + await hardReset() + remote.getCurrentWindow().webContents.reloadIgnoringCache() } catch (err) { this.setState({ isHardResetting: false }) } diff --git a/src/helpers/hardReset.js b/src/helpers/hardReset.js new file mode 100644 index 00000000..24477872 --- /dev/null +++ b/src/helpers/hardReset.js @@ -0,0 +1,14 @@ +import libcoreHardReset from 'commands/libcoreHardReset' +import { disable as disableDBMiddleware } from 'middlewares/db' + +import db from 'helpers/db' +import { delay } from 'helpers/promise' + +export default async function hardReset() { + // TODO: wait for the libcoreHardReset to be finished + // actually, libcore doesnt goes back to js thread + await Promise.race([libcoreHardReset.send().toPromise(), delay(500)]) + disableDBMiddleware() + db.resetAll() + await delay(500) +} diff --git a/src/middlewares/db.js b/src/middlewares/db.js index 0af57202..d6b92b5f 100644 --- a/src/middlewares/db.js +++ b/src/middlewares/db.js @@ -6,8 +6,16 @@ import { accountsSelector } from 'reducers/accounts' import { settingsExportSelector, areSettingsLoaded } from 'reducers/settings' import CounterValues from 'helpers/countervalues' +let DB_MIDDLEWARE_ENABLED = true + +// ability to temporary disable the db middleware from outside +export const disable = (ms = 1000) => { + DB_MIDDLEWARE_ENABLED = false + setTimeout(() => (DB_MIDDLEWARE_ENABLED = true), ms) +} + export default store => next => action => { - if (action.type.startsWith('DB:')) { + if (DB_MIDDLEWARE_ENABLED && action.type.startsWith('DB:')) { const [, type] = action.type.split(':') store.dispatch({ type, payload: action.payload }) const state = store.getState() diff --git a/src/renderer/init.js b/src/renderer/init.js index a7db960b..6ef76a10 100644 --- a/src/renderer/init.js +++ b/src/renderer/init.js @@ -20,43 +20,39 @@ import libcoreGetVersion from 'commands/libcoreGetVersion' import db from 'helpers/db' import dbMiddleware from 'middlewares/db' import CounterValues from 'helpers/countervalues' +import hardReset from 'helpers/hardReset' import App from 'components/App' import 'styles/global' -if (process.env.LEDGER_RESET_ALL) { - db.resetAll() -} - -// Init db with defaults if needed -db.init('settings', {}) - -const history = createHistory() -const store = createStore({ history, dbMiddleware }) const rootNode = document.getElementById('app') -const settings = db.get('settings') -store.dispatch(fetchSettings(settings)) +async function init() { + if (process.env.LEDGER_RESET_ALL) { + await hardReset() + } -const countervaluesData = db.get('countervalues') -if (countervaluesData) { - store.dispatch(CounterValues.importAction(countervaluesData)) -} + // Init db with defaults if needed + db.init('settings', {}) -const state = store.getState() -const language = getLanguage(state) -const locked = isLocked(state) + const history = createHistory() + const store = createStore({ history, dbMiddleware }) -moment.locale(language) + const settings = db.get('settings') + store.dispatch(fetchSettings(settings)) -function r(Comp) { - if (rootNode) { - render({Comp}, rootNode) + const countervaluesData = db.get('countervalues') + if (countervaluesData) { + store.dispatch(CounterValues.importAction(countervaluesData)) } -} -async function init() { + const state = store.getState() + const language = getLanguage(state) + const locked = isLocked(state) + + moment.locale(language) + // FIXME IMO init() really should only be for window. any other case is a hack! const isMainWindow = remote.getCurrentWindow().name === 'MainWindow' @@ -92,6 +88,12 @@ async function init() { } } +function r(Comp) { + if (rootNode) { + render({Comp}, rootNode) + } +} + init().catch(e => { // for now we make the app crash instead of pending forever. later we can render the error OR try to recover, but probably this is unrecoverable cases. logger.error(e)