Browse Source

Merge pull request #209 from loeck/master

Await accounts and counterValues before render Application
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
bac50daa60
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/actions/accounts.js
  2. 4
      src/components/IsUnlocked.js
  3. 18
      src/renderer/events.js
  4. 29
      src/renderer/init.js

5
src/actions/accounts.js

@ -7,6 +7,8 @@ import db from 'helpers/db'
import type { Dispatch } from 'redux'
import type { Account } from 'types/common'
import { fetchCounterValues } from 'actions/counterValues'
import { startSyncAccounts } from 'renderer/events'
function sortAccounts(accounts, orderAccounts) {
@ -60,7 +62,7 @@ export const removeAccount: RemoveAccount = payload => ({
payload,
})
export type FetchAccounts = () => (Dispatch<*>, Function) => void
export type FetchAccounts = () => (Function, Function) => Promise<*, *>
export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => {
const { settings: { orderAccounts } } = getState()
const accounts = db.get('accounts')
@ -68,6 +70,7 @@ export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => {
type: 'SET_ACCOUNTS',
payload: sortAccounts(accounts, orderAccounts),
})
return dispatch(fetchCounterValues())
}
export type UpdateAccount = Account => (Function, Function) => void

4
src/components/IsUnlocked.js

@ -95,7 +95,7 @@ class IsUnlocked extends Component<Props, State> {
},
}))
handleSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
handleSubmit = async (e: SyntheticEvent<HTMLFormElement>) => {
e.preventDefault()
const { settings, unlock, fetchAccounts } = this.props
@ -103,7 +103,7 @@ class IsUnlocked extends Component<Props, State> {
if (bcrypt.compareSync(inputValue.password, get(settings, 'password.value'))) {
setEncryptionKey('accounts', inputValue.password)
fetchAccounts()
await fetchAccounts()
unlock()
this.setState({

18
src/renderer/events.js

@ -29,8 +29,8 @@ type MsgPayload = {
data: any,
}
let syncAccounts = true
let syncTimeout
let syncAccountsInProgress = true
let syncAccountsTimeout
export function sendEvent(channel: string, msgType: string, data: any) {
ipcRenderer.send(channel, {
@ -48,7 +48,7 @@ export function sendSyncEvent(channel: string, msgType: string, data: any): any
export function startSyncAccounts(accounts: Accounts) {
d.sync('Sync accounts - start')
syncAccounts = true
syncAccountsInProgress = true
sendEvent('accounts', 'sync.all', {
accounts: accounts.map(account => {
const { id, coinType, rootPath, addresses, index, transactions } = account
@ -66,8 +66,8 @@ export function startSyncAccounts(accounts: Accounts) {
export function stopSyncAccounts() {
d.sync('Sync accounts - stop')
syncAccounts = false
clearTimeout(syncTimeout)
syncAccountsInProgress = false
clearTimeout(syncAccountsTimeout)
}
export function checkUpdates() {
@ -84,7 +84,7 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
account: {
sync: {
success: account => {
if (syncAccounts) {
if (syncAccountsInProgress) {
const state = store.getState()
const existingAccount = getAccountById(state, account.id)
@ -115,7 +115,7 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
accounts: {
sync: {
start: () => {
if (!syncAccounts) {
if (!syncAccountsInProgress) {
const state = store.getState()
const accounts = getAccounts(state)
const locked = isLocked(state)
@ -127,9 +127,9 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
},
stop: stopSyncAccounts,
success: () => {
if (syncAccounts && !DISABLED_AUTO_SYNC) {
if (syncAccountsInProgress && !DISABLED_AUTO_SYNC) {
d.sync('Sync accounts - success')
syncTimeout = setTimeout(() => {
syncAccountsTimeout = setTimeout(() => {
const accounts = getAccounts(store.getState())
startSyncAccounts(accounts)
}, SYNC_ACCOUNT_TIMEOUT)

29
src/renderer/init.js

@ -11,7 +11,7 @@ import events from 'renderer/events'
import { fetchAccounts } from 'actions/accounts'
import { fetchSettings } from 'actions/settings'
import { initCounterValues, fetchCounterValues } from 'actions/counterValues'
import { initCounterValues } from 'actions/counterValues'
import { isLocked } from 'reducers/application'
import { getLanguage } from 'reducers/settings'
@ -36,27 +36,30 @@ const state = store.getState() || {}
const language = getLanguage(state)
const locked = isLocked(state)
if (!locked) {
// Init accounts with defaults if needed
db.init('accounts', [])
store.dispatch(fetchAccounts())
store.dispatch(fetchCounterValues())
}
function r(Comp) {
if (rootNode) {
render(<AppContainer>{Comp}</AppContainer>, rootNode)
}
}
r(<App store={store} history={history} language={language} />)
async function init() {
if (!locked) {
// Init accounts with defaults if needed
db.init('accounts', [])
await store.dispatch(fetchAccounts())
}
r(<App store={store} history={history} language={language} />)
// Only init events on MainWindow
if (remote.getCurrentWindow().name === 'MainWindow') {
events({ store, locked })
// Only init events on MainWindow
if (remote.getCurrentWindow().name === 'MainWindow') {
events({ store, locked })
}
}
init()
if (module.hot) {
module.hot.accept('../components/App', () => {
const NewApp = require('../components/App').default

Loading…
Cancel
Save