Browse Source

Merge branch 'master' into bugfix-settings-db

master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
47efbec074
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/bridge/BridgeSyncContext.js
  2. 21
      src/components/IsUnlocked.js
  3. 1
      src/config/constants.js
  4. 104
      src/renderer/events.js

2
src/bridge/BridgeSyncContext.js

@ -157,6 +157,8 @@ class Provider extends Component<BridgeSyncProviderOwnProps, BridgeSync> {
setTimeout(syncLoop, 2 * 1000)
}
// TODO we might want to call sync straight away when new accounts got added (it will happen every 10s anyway)
api: BridgeSync
render() {

21
src/components/IsUnlocked.js

@ -6,7 +6,6 @@ import { connect } from 'react-redux'
import { compose } from 'redux'
import styled from 'styled-components'
import { translate } from 'react-i18next'
import type { Account } from '@ledgerhq/live-common/lib/types'
import type { Settings, T } from 'types/common'
import IconLockScreen from 'icons/LockScreen'
@ -15,11 +14,9 @@ import { ErrorMessageInput } from 'components/base/Input'
import get from 'lodash/get'
import { startSyncAccounts, stopSyncAccounts } from 'renderer/events'
import { setEncryptionKey } from 'helpers/db'
import { fetchAccounts } from 'actions/accounts'
import { getAccounts } from 'reducers/accounts'
import { isLocked, unlock } from 'reducers/application'
import Box from 'components/base/Box'
@ -30,7 +27,6 @@ type InputValue = {
}
type Props = {
accounts: Account[],
children: any,
fetchAccounts: Function,
isLocked: boolean,
@ -44,7 +40,6 @@ type State = {
}
const mapStateToProps = state => ({
accounts: getAccounts(state),
isLocked: isLocked(state),
settings: state.settings,
})
@ -84,22 +79,6 @@ class IsUnlocked extends Component<Props, State> {
...defaultState,
}
componentWillMount() {
if (this.props.isLocked) {
stopSyncAccounts()
}
}
componentWillReceiveProps(nextProps) {
if (this.props.isLocked && !nextProps.isLocked) {
startSyncAccounts(nextProps.accounts)
}
if (!this.props.isLocked && nextProps.isLocked) {
stopSyncAccounts()
}
}
shouldComponentUpdate(nextProps) {
if (nextProps.isLocked) {
return true

1
src/config/constants.js

@ -1,5 +1,4 @@
export const CHECK_UPDATE_DELAY = 5e3
export const SYNC_ACCOUNT_DELAY = 3e3
export const MODAL_ADD_ACCOUNT = 'MODAL_ADD_ACCOUNT'
export const MODAL_OPERATION_DETAILS = 'MODAL_OPERATION_DETAILS'

104
src/renderer/events.js

@ -5,15 +5,11 @@
import { ipcRenderer } from 'electron'
import objectPath from 'object-path'
import debug from 'debug'
import type { Account } from '@ledgerhq/live-common/lib/types'
import { CHECK_UPDATE_DELAY, SYNC_ACCOUNT_DELAY } from 'config/constants'
import { CHECK_UPDATE_DELAY } from 'config/constants'
import { getAccounts, getAccountById } from 'reducers/accounts'
import { isLocked } from 'reducers/application'
import { setUpdateStatus } from 'reducers/update'
import { updateAccount } from 'actions/accounts'
import { addDevice, removeDevice } from 'actions/devices'
import i18n from 'renderer/i18n/electron'
@ -24,16 +20,11 @@ const d = {
update: debug('lwd:update'),
}
const { DISABLED_SYNC, DISABLED_AUTO_SYNC } = process.env
type MsgPayload = {
type: string,
data: any,
}
let syncAccountsInProgress = false
let syncAccountsTimeout
export function sendEvent(channel: string, msgType: string, data: any) {
ipcRenderer.send(channel, {
type: msgType,
@ -48,68 +39,12 @@ export function sendSyncEvent(channel: string, msgType: string, data: any): any
})
}
export default ({ store, locked }: { store: Object, locked: boolean }) => {
export default ({ store }: { store: Object, locked: boolean }) => {
const handlers = {
dispatch: ({ type, payload }) => store.dispatch({ type, payload }),
application: {
changeLanguage: lang => i18n.changeLanguage(lang),
},
account: {
sync: {
success: account => {
if (syncAccountsInProgress) {
const state = store.getState()
const existingAccount = getAccountById(state, account.id)
if (!existingAccount) {
return
}
const { name, balance, balanceByDay, operations } = existingAccount
if (account.operations.length > 0) {
d.sync(`Update account - ${name}`)
const updatedAccount = {
...account,
balance: balance + account.balance,
balanceByDay: Object.keys(balanceByDay).reduce((result, k) => {
result[k] = balanceByDay[k] + (account.balanceByDay[k] || 0)
return result
}, {}),
index: account.index || existingAccount.index,
operations: [...operations, ...account.operations],
}
store.dispatch(updateAccount(updatedAccount))
}
}
},
},
},
accounts: {
sync: {
start: () => {
if (!syncAccountsInProgress) {
const state = store.getState()
const accounts = getAccounts(state)
const locked = isLocked(state)
if (!locked && !DISABLED_SYNC) {
startSyncAccounts(accounts)
}
}
},
stop: stopSyncAccounts,
success: () => {
if (syncAccountsInProgress && !DISABLED_AUTO_SYNC) {
d.sync('Sync accounts - success')
syncAccountsTimeout = setTimeout(() => {
const accounts = getAccounts(store.getState())
startSyncAccounts(accounts)
}, SYNC_ACCOUNT_DELAY)
}
},
},
},
device: {
add: device => {
d.device('Device - add')
@ -145,47 +80,12 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
// Start detection when we plug/unplug devices
sendEvent('devices', 'listen')
const state = store.getState()
if (!locked) {
const accounts = getAccounts(state)
// Start accounts sync only if we have accounts
if (accounts.length > 0 && !DISABLED_SYNC) {
startSyncAccounts(accounts)
}
}
if (__PROD__) {
// Start check of eventual updates
checkUpdates()
}
}
export function startSyncAccounts(accounts: Account[]) {
d.sync('Sync accounts - start')
syncAccountsInProgress = true
sendEvent('accounts', 'sync', {
accounts: accounts.map(account => {
const { id, currency, walletPath, addresses, index, operations } = account
return {
id,
currencyId: currency.id,
allAddresses: addresses,
currentIndex: index,
walletPath,
operations,
}
}),
})
}
export function stopSyncAccounts() {
d.sync('Sync accounts - stop')
syncAccountsInProgress = false
clearTimeout(syncAccountsTimeout)
}
export function checkUpdates() {
d.update('Update - check')
setTimeout(() => sendEvent('msg', 'updater.init'), CHECK_UPDATE_DELAY)

Loading…
Cancel
Save