Browse Source

Merge pull request #226 from loeck/master

Update last counterValue each 60 secondes
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
3db8a55099
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/actions/accounts.js
  2. 9
      src/actions/counterValues.js
  3. 1
      src/components/CalculateBalance.js
  4. 15
      src/components/IsUnlocked.js
  5. 5
      src/constants.js
  6. 4
      src/main/bridge.js
  7. 24
      src/main/counterValuesSync.js
  8. 18
      src/reducers/counterValues.js
  9. 59
      src/renderer/events.js

8
src/actions/accounts.js

@ -9,7 +9,7 @@ import type { Account } from 'types/common'
import { fetchCounterValues } from 'actions/counterValues' import { fetchCounterValues } from 'actions/counterValues'
import { startSyncAccounts } from 'renderer/events' import { startSyncAccounts, startSyncCounterValues } from 'renderer/events'
function sortAccounts(accounts, orderAccounts) { function sortAccounts(accounts, orderAccounts) {
const [order, sort] = orderAccounts.split('|') const [order, sort] = orderAccounts.split('|')
@ -43,7 +43,7 @@ export const updateOrderAccounts: UpdateOrderAccounts = (orderAccounts: string)
export type AddAccount = Account => (Function, Function) => void export type AddAccount = Account => (Function, Function) => void
export const addAccount: AddAccount = payload => (dispatch, getState) => { export const addAccount: AddAccount = payload => (dispatch, getState) => {
const { settings: { orderAccounts }, accounts } = getState() const { settings: { counterValue, orderAccounts }, accounts } = getState()
dispatch({ dispatch({
type: 'ADD_ACCOUNT', type: 'ADD_ACCOUNT',
payload, payload,
@ -52,7 +52,9 @@ export const addAccount: AddAccount = payload => (dispatch, getState) => {
// Start sync accounts the first time you add an account // Start sync accounts the first time you add an account
if (accounts.length === 0) { if (accounts.length === 0) {
startSyncAccounts([payload]) const accounts = [payload]
startSyncCounterValues(counterValue, accounts)
startSyncAccounts(accounts)
} }
} }

9
src/actions/counterValues.js

@ -24,6 +24,15 @@ export const updateCounterValues: UpdateCounterValues = payload => ({
payload, payload,
}) })
export type UpdateLastCounterValueBySymbol = (string, Object) => { type: string, payload: Object }
export const updateLastCounterValueBySymbol: UpdateLastCounterValueBySymbol = (symbol, value) => ({
type: 'DB:UPDATE_LAST_COUNTER_VALUE',
payload: {
symbol,
value,
},
})
export type FetchCounterValues = (?number) => (Dispatch<*>, Function) => Promise<any> export type FetchCounterValues = (?number) => (Dispatch<*>, Function) => Promise<any>
export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, getState) => { export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, getState) => {
const { accounts, counterValues, settings } = getState() const { accounts, counterValues, settings } = getState()

1
src/components/CalculateBalance.js

@ -40,7 +40,6 @@ class CalculateBalance extends PureComponent<Props, State> {
const sameAccounts = this.props.accounts === nextProps.accounts const sameAccounts = this.props.accounts === nextProps.accounts
const sameCounterValues = this.props.counterValues === nextProps.counterValues const sameCounterValues = this.props.counterValues === nextProps.counterValues
const sameDaysCount = this.props.daysCount === nextProps.daysCount const sameDaysCount = this.props.daysCount === nextProps.daysCount
if (!sameAccounts || !sameCounterValues || !sameDaysCount) { if (!sameAccounts || !sameCounterValues || !sameDaysCount) {
this.setState(calculateBalanceToState(nextProps)) this.setState(calculateBalanceToState(nextProps))
} }

15
src/components/IsUnlocked.js

@ -10,12 +10,18 @@ import type { Settings, Accounts, T } from 'types/common'
import get from 'lodash/get' import get from 'lodash/get'
import { startSyncAccounts, stopSyncAccounts } from 'renderer/events' import {
startSyncCounterValues,
startSyncAccounts,
stopSyncAccounts,
stopSyncCounterValues,
} from 'renderer/events'
import { setEncryptionKey } from 'helpers/db' import { setEncryptionKey } from 'helpers/db'
import { fetchAccounts } from 'actions/accounts' import { fetchAccounts } from 'actions/accounts'
import { getAccounts } from 'reducers/accounts' import { getAccounts } from 'reducers/accounts'
import { isLocked, unlock } from 'reducers/application' import { isLocked, unlock } from 'reducers/application'
import { getCounterValue } from 'reducers/settings'
import Box from 'components/base/Box' import Box from 'components/base/Box'
import Input from 'components/base/Input' import Input from 'components/base/Input'
@ -27,6 +33,7 @@ type InputValue = {
type Props = { type Props = {
accounts: Accounts, accounts: Accounts,
children: any, children: any,
counterValue: string,
fetchAccounts: Function, fetchAccounts: Function,
isLocked: boolean, isLocked: boolean,
settings: Settings, settings: Settings,
@ -39,8 +46,9 @@ type State = {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
accounts: getAccounts(state), accounts: getAccounts(state),
settings: state.settings, counterValue: getCounterValue(state),
isLocked: isLocked(state), isLocked: isLocked(state),
settings: state.settings,
}) })
const mapDispatchToProps: Object = { const mapDispatchToProps: Object = {
@ -61,16 +69,19 @@ class IsUnlocked extends Component<Props, State> {
componentWillMount() { componentWillMount() {
if (this.props.isLocked) { if (this.props.isLocked) {
stopSyncCounterValues()
stopSyncAccounts() stopSyncAccounts()
} }
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (this.props.isLocked && !nextProps.isLocked) { if (this.props.isLocked && !nextProps.isLocked) {
startSyncCounterValues(nextProps.counterValue, nextProps.accounts)
startSyncAccounts(nextProps.accounts) startSyncAccounts(nextProps.accounts)
} }
if (!this.props.isLocked && nextProps.isLocked) { if (!this.props.isLocked && nextProps.isLocked) {
stopSyncCounterValues()
stopSyncAccounts() stopSyncAccounts()
} }
} }

5
src/constants.js

@ -1,5 +1,6 @@
export const CHECK_UPDATE_TIMEOUT = 5e3 export const CHECK_UPDATE_DELAY = 5e3
export const SYNC_ACCOUNT_TIMEOUT = 3e3 export const SYNC_ACCOUNT_DELAY = 3e3
export const SYNC_COUNTER_VALUES_DELAY = 60e3
export const MODAL_ADD_ACCOUNT = 'MODAL_ADD_ACCOUNT' export const MODAL_ADD_ACCOUNT = 'MODAL_ADD_ACCOUNT'
export const MODAL_SEND = 'MODAL_SEND' export const MODAL_SEND = 'MODAL_SEND'

4
src/main/bridge.js

@ -8,6 +8,7 @@ import { resolve } from 'path'
import cpuUsage from 'helpers/cpuUsage' import cpuUsage from 'helpers/cpuUsage'
import setupAutoUpdater, { quitAndInstall } from './autoUpdate' import setupAutoUpdater, { quitAndInstall } from './autoUpdate'
import counterValuesSync from './counterValuesSync'
const { DEV_TOOLS } = process.env const { DEV_TOOLS } = process.env
@ -80,6 +81,9 @@ const handlers = {
init: setupAutoUpdater, init: setupAutoUpdater,
quitAndInstall, quitAndInstall,
}, },
counterValues: {
sync: counterValuesSync,
},
kill: { kill: {
process: (send, { pid }) => { process: (send, { pid }) => {
try { try {

24
src/main/counterValuesSync.js

@ -0,0 +1,24 @@
// @flow
import axios from 'axios'
type SendFunction = (type: string, data: *) => void
export default async (send: SendFunction, { counterValue, currencies }: Object) => {
const data = await axios
.get(
`https://min-api.cryptocompare.com/data/pricemulti?extraParams=ledger-test&fsyms=${currencies.join(
',',
)}&tsyms=${counterValue}`,
)
.then(({ data }) =>
currencies.reduce((result, code) => {
result.push({
symbol: `${code}-${counterValue}`,
value: data[code][counterValue],
})
return result
}, []),
)
send('counterValues.update', data)
}

18
src/reducers/counterValues.js

@ -19,6 +19,24 @@ const handlers = {
...state, ...state,
...counterValues, ...counterValues,
}), }),
UPDATE_LAST_COUNTER_VALUE: (
state: CounterValuesState,
{ payload: { symbol, value } }: { payload: { symbol: string, value: number } },
): CounterValuesState => {
// We update only last value (newer)
if (state[symbol]) {
const [date] = state[symbol].list[0]
// [0] date, [1] value, only update value
state[symbol].list[0][1] = value
// Keep the same value for byDate object
state[symbol].byDate[date] = value
// Update reference for a proper update
return { ...state }
}
return state
},
} }
export function getLastCounterValueBySymbol( export function getLastCounterValueBySymbol(

59
src/renderer/events.js

@ -3,16 +3,20 @@
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import objectPath from 'object-path' import objectPath from 'object-path'
import debug from 'debug' import debug from 'debug'
import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
import type { Accounts } from 'types/common' import type { Accounts } from 'types/common'
import { CHECK_UPDATE_TIMEOUT, SYNC_ACCOUNT_TIMEOUT } from 'constants' import { CHECK_UPDATE_DELAY, SYNC_ACCOUNT_DELAY, SYNC_COUNTER_VALUES_DELAY } from 'constants'
import { updateDevices, addDevice, removeDevice } from 'actions/devices'
import { updateAccount } from 'actions/accounts'
import { setUpdateStatus } from 'reducers/update'
import { getAccounts, getAccountById } from 'reducers/accounts' import { getAccounts, getAccountById } from 'reducers/accounts'
import { getCounterValue } from 'reducers/settings'
import { isLocked } from 'reducers/application' import { isLocked } from 'reducers/application'
import { setUpdateStatus } from 'reducers/update'
import { updateLastCounterValueBySymbol } from 'actions/counterValues'
import { updateAccount } from 'actions/accounts'
import { updateDevices, addDevice, removeDevice } from 'actions/devices'
import i18n from 'renderer/i18n/electron' import i18n from 'renderer/i18n/electron'
@ -29,9 +33,11 @@ type MsgPayload = {
data: any, data: any,
} }
let syncAccountsInProgress = true let syncAccountsInProgress = false
let syncAccountsTimeout let syncAccountsTimeout
let syncCounterValuesTimeout
export function sendEvent(channel: string, msgType: string, data: any) { export function sendEvent(channel: string, msgType: string, data: any) {
ipcRenderer.send(channel, { ipcRenderer.send(channel, {
type: msgType, type: msgType,
@ -70,9 +76,25 @@ export function stopSyncAccounts() {
clearTimeout(syncAccountsTimeout) clearTimeout(syncAccountsTimeout)
} }
export function startSyncCounterValues(counterValue: string, accounts: Accounts) {
d.sync('Sync counterValues - start')
sendEvent('msg', 'counterValues.sync', {
counterValue,
currencies: [
...new Set(accounts.map(account => getDefaultUnitByCoinType(account.coinType).code)),
],
})
}
export function stopSyncCounterValues() {
d.sync('Sync counterValues - stop')
clearTimeout(syncCounterValuesTimeout)
}
export function checkUpdates() { export function checkUpdates() {
d.update('Update - check') d.update('Update - check')
setTimeout(() => sendEvent('msg', 'updater.init'), CHECK_UPDATE_TIMEOUT) setTimeout(() => sendEvent('msg', 'updater.init'), CHECK_UPDATE_DELAY)
} }
export default ({ store, locked }: { store: Object, locked: boolean }) => { export default ({ store, locked }: { store: Object, locked: boolean }) => {
@ -132,7 +154,7 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
syncAccountsTimeout = setTimeout(() => { syncAccountsTimeout = setTimeout(() => {
const accounts = getAccounts(store.getState()) const accounts = getAccounts(store.getState())
startSyncAccounts(accounts) startSyncAccounts(accounts)
}, SYNC_ACCOUNT_TIMEOUT) }, SYNC_ACCOUNT_DELAY)
} }
}, },
}, },
@ -152,6 +174,18 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
store.dispatch(removeDevice(device)) store.dispatch(removeDevice(device))
}, },
}, },
counterValues: {
update: counterValues => {
counterValues.map(c => store.dispatch(updateLastCounterValueBySymbol(c.symbol, c.value)))
syncCounterValuesTimeout = setTimeout(() => {
const state = store.getState()
const accounts = getAccounts(state)
const counterValue = getCounterValue(state)
startSyncCounterValues(counterValue, accounts)
}, SYNC_COUNTER_VALUES_DELAY)
},
},
updater: { updater: {
checking: () => store.dispatch(setUpdateStatus('checking')), checking: () => store.dispatch(setUpdateStatus('checking')),
updateAvailable: info => store.dispatch(setUpdateStatus('available', info)), updateAvailable: info => store.dispatch(setUpdateStatus('available', info)),
@ -177,11 +211,16 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
// Start detection when we plug/unplug devices // Start detection when we plug/unplug devices
sendEvent('usb', 'devices.listen') sendEvent('usb', 'devices.listen')
if (!locked && !DISABLED_SYNC) { const state = store.getState()
const accounts = getAccounts(store.getState())
if (!locked) {
const accounts = getAccounts(state)
const counterValue = getCounterValue(state)
startSyncCounterValues(counterValue, accounts)
// Start accounts sync only if we have accounts // Start accounts sync only if we have accounts
if (accounts.length > 0) { if (accounts.length > 0 && !DISABLED_SYNC) {
startSyncAccounts(accounts) startSyncAccounts(accounts)
} }
} }

Loading…
Cancel
Save