Browse Source

Await accounts and counterValues before render Application

master
Loëck Vézien 7 years ago
parent
commit
969cb4e0b1
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  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 { Dispatch } from 'redux'
import type { Account } from 'types/common' import type { Account } from 'types/common'
import { fetchCounterValues } from 'actions/counterValues'
import { startSyncAccounts } from 'renderer/events' import { startSyncAccounts } from 'renderer/events'
function sortAccounts(accounts, orderAccounts) { function sortAccounts(accounts, orderAccounts) {
@ -60,7 +62,7 @@ export const removeAccount: RemoveAccount = payload => ({
payload, payload,
}) })
export type FetchAccounts = () => (Dispatch<*>, Function) => void export type FetchAccounts = () => (Function, Function) => Promise<*, *>
export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => { export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => {
const { settings: { orderAccounts } } = getState() const { settings: { orderAccounts } } = getState()
const accounts = db.get('accounts') const accounts = db.get('accounts')
@ -68,6 +70,7 @@ export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => {
type: 'SET_ACCOUNTS', type: 'SET_ACCOUNTS',
payload: sortAccounts(accounts, orderAccounts), payload: sortAccounts(accounts, orderAccounts),
}) })
return dispatch(fetchCounterValues())
} }
export type UpdateAccount = Account => (Function, Function) => void 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() e.preventDefault()
const { settings, unlock, fetchAccounts } = this.props 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'))) { if (bcrypt.compareSync(inputValue.password, get(settings, 'password.value'))) {
setEncryptionKey('accounts', inputValue.password) setEncryptionKey('accounts', inputValue.password)
fetchAccounts() await fetchAccounts()
unlock() unlock()
this.setState({ this.setState({

18
src/renderer/events.js

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

29
src/renderer/init.js

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

Loading…
Cancel
Save