Browse Source

Ability to trigger a hard reset with LEDGER_RESET_ALL

master
meriadec 7 years ago
parent
commit
15b82f9800
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 10
      src/components/SettingsPage/sections/Profile.js
  2. 14
      src/helpers/hardReset.js
  3. 10
      src/middlewares/db.js
  4. 50
      src/renderer/init.js

10
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<Props, State> {
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 })
}

14
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)
}

10
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()

50
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(<AppContainer>{Comp}</AppContainer>, 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(<AppContainer>{Comp}</AppContainer>, 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)

Loading…
Cancel
Save