From 969cb4e0b143ed360dc0304bf2ffd2b236ff4a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Fri, 9 Mar 2018 10:04:54 +0100 Subject: [PATCH] Await accounts and counterValues before render Application --- src/actions/accounts.js | 5 ++++- src/components/IsUnlocked.js | 4 ++-- src/renderer/events.js | 18 +++++++++--------- src/renderer/init.js | 29 ++++++++++++++++------------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/actions/accounts.js b/src/actions/accounts.js index af67eb97..9336d059 100644 --- a/src/actions/accounts.js +++ b/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 diff --git a/src/components/IsUnlocked.js b/src/components/IsUnlocked.js index 00e0a015..9dae0c42 100644 --- a/src/components/IsUnlocked.js +++ b/src/components/IsUnlocked.js @@ -95,7 +95,7 @@ class IsUnlocked extends Component { }, })) - handleSubmit = (e: SyntheticEvent) => { + handleSubmit = async (e: SyntheticEvent) => { e.preventDefault() const { settings, unlock, fetchAccounts } = this.props @@ -103,7 +103,7 @@ class IsUnlocked extends Component { if (bcrypt.compareSync(inputValue.password, get(settings, 'password.value'))) { setEncryptionKey('accounts', inputValue.password) - fetchAccounts() + await fetchAccounts() unlock() this.setState({ diff --git a/src/renderer/events.js b/src/renderer/events.js index 02666dc0..f96593d7 100644 --- a/src/renderer/events.js +++ b/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) diff --git a/src/renderer/init.js b/src/renderer/init.js index 74233997..2d58b5b0 100644 --- a/src/renderer/init.js +++ b/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({Comp}, rootNode) } } -r() +async function init() { + if (!locked) { + // Init accounts with defaults if needed + db.init('accounts', []) + + await store.dispatch(fetchAccounts()) + } + + r() -// 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