Browse Source

Refactor the internal folder mess

master
meriadec 7 years ago
parent
commit
c9f6f566ec
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 8
      src/actions/devices.js
  2. 6
      src/components/DeviceCheckAddress.js
  3. 4
      src/components/DeviceConnect/index.js
  4. 6
      src/components/DeviceMonit/index.js
  5. 12
      src/components/DeviceMonitNew/index.js
  6. 7
      src/components/ManagerPage/AppsList.js
  7. 11
      src/components/ManagerPage/DeviceInfos.js
  8. 12
      src/components/modals/AddAccount/index.js
  9. 0
      src/internals/accounts/helpers.js
  10. 70
      src/internals/accounts/index.js
  11. 0
      src/internals/accounts/scanAccountsOnDevice.js
  12. 10
      src/internals/accounts/sync.js
  13. 44
      src/internals/devices/checkIfAppOpened.js
  14. 2
      src/internals/devices/index.js
  15. 24
      src/internals/devices/listen.js
  16. 20
      src/internals/index.js
  17. 0
      src/internals/manager/constants.js
  18. 12
      src/internals/manager/getMemInfos.js
  19. 0
      src/internals/manager/helpers.js
  20. 22
      src/internals/manager/index.js
  21. 12
      src/internals/manager/installApp.js
  22. 14
      src/internals/manager/listApps.js
  23. 12
      src/internals/manager/uninstallApp.js
  24. 28
      src/internals/usb/devices.js
  25. 3
      src/internals/usb/index.js
  26. 50
      src/internals/usb/manager/index.js
  27. 105
      src/internals/usb/wallet/index.js
  28. 15
      src/main/bridge.js
  29. 9
      src/reducers/devices.js
  30. 98
      src/renderer/events.js
  31. 34
      src/renderer/runJob.js
  32. 2
      src/types/common.js

8
src/actions/devices.js

@ -1,6 +1,6 @@
// @flow
import type { Device, Devices } from 'types/common'
import type { Device } from 'types/common'
export type SetCurrentDevice = (Device | null) => { type: string, payload: Device | null }
export const setCurrentDevice: SetCurrentDevice = payload => ({
@ -19,9 +19,3 @@ export const removeDevice: RemoveDevice = payload => ({
type: 'REMOVE_DEVICE',
payload,
})
type UpdateDevices = Devices => { type: string, payload: Devices }
export const updateDevices: UpdateDevices = payload => ({
type: 'UPDATE_DEVICES',
payload,
})

6
src/components/DeviceCheckAddress.js

@ -37,14 +37,14 @@ class CheckAddress extends PureComponent<Props, State> {
handleMsgEvent = (e: any, { type }: { type: string }) => {
const { onCheck } = this.props
if (type === 'wallet.verifyAddress.success') {
if (type === 'accounts.verifyAddress.success') {
this.setState({
isVerified: true,
})
onCheck(true)
}
if (type === 'wallet.verifyAddress.fail') {
if (type === 'accounts.verifyAddress.fail') {
this.setState({
isVerified: false,
})
@ -53,7 +53,7 @@ class CheckAddress extends PureComponent<Props, State> {
}
verifyAddress = ({ device, account }: { device: Device, account: Account }) =>
sendEvent('usb', 'wallet.verifyAddress', {
sendEvent('accounts', 'verifyAddress', {
pathDevice: device.path,
path: account.path,
segwit: account.path.startsWith("49'"), // TODO: store segwit info in account

4
src/components/DeviceConnect/index.js

@ -5,7 +5,7 @@ import styled from 'styled-components'
import { Trans, translate } from 'react-i18next'
import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types'
import type { T, Device, Devices } from 'types/common'
import type { T, Device } from 'types/common'
import noop from 'lodash/noop'
@ -139,7 +139,7 @@ type Props = {
accountName: null | string,
appOpened: null | 'success' | 'fail',
currency: CryptoCurrency,
devices: Devices,
devices: Device[],
deviceSelected: Device | null,
onChangeDevice: Function,
t: T,

6
src/components/DeviceMonit/index.js

@ -80,7 +80,7 @@ class DeviceMonit extends PureComponent<Props, State> {
return
}
sendEvent('usb', 'wallet.checkIfAppOpened', {
sendEvent('devices', 'checkIfAppOpened', {
devicePath: currentDevice.path,
accountPath: account.path,
accountAddress: account.address,
@ -97,12 +97,12 @@ class DeviceMonit extends PureComponent<Props, State> {
}
handleMsgEvent = (e, { type }) => {
if (type === 'wallet.checkIfAppOpened.success') {
if (type === 'devices.checkIfAppOpened.success') {
this.handleStatusChange('appOpened')
clearTimeout(this._timeout)
}
if (type === 'wallet.checkIfAppOpened.fail') {
if (type === 'devices.checkIfAppOpened.fail') {
this._timeout = setTimeout(this.checkAppOpened, 1e3)
}
}

12
src/components/DeviceMonitNew/index.js

@ -5,7 +5,7 @@ import { connect } from 'react-redux'
import { ipcRenderer } from 'electron'
import type { Account, CryptoCurrency } from '@ledgerhq/live-common/lib/types'
import type { Device, Devices } from 'types/common'
import type { Device } from 'types/common'
import { sendEvent } from 'renderer/events'
import { getDevices } from 'reducers/devices'
@ -20,14 +20,14 @@ type OwnProps = {
render?: ({
appStatus: AppStatus,
currency: CryptoCurrency,
devices: Devices,
devices: Device[],
deviceSelected: ?Device,
deviceStatus: DeviceStatus,
}) => React$Element<*>,
}
type Props = OwnProps & {
devices: Devices,
devices: Device[],
}
type DeviceStatus = 'unconnected' | 'connected'
@ -111,7 +111,7 @@ class DeviceMonit extends PureComponent<Props, State> {
}
}
sendEvent('usb', 'wallet.checkIfAppOpened', {
sendEvent('devices', 'checkIfAppOpened', {
devicePath: deviceSelected.path,
...options,
})
@ -134,12 +134,12 @@ class DeviceMonit extends PureComponent<Props, State> {
return
}
if (type === 'wallet.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) {
if (type === 'devices.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) {
this.handleStatusChange(deviceStatus, 'success')
this._timeout = setTimeout(this.checkAppOpened, 1e3)
}
if (type === 'wallet.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) {
if (type === 'devices.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) {
this.handleStatusChange(deviceStatus, 'fail')
this._timeout = setTimeout(this.checkAppOpened, 1e3)
}

7
src/components/ManagerPage/AppsList.js

@ -4,7 +4,7 @@ import React, { PureComponent } from 'react'
import styled from 'styled-components'
import { translate } from 'react-i18next'
import { runJob } from 'renderer/events'
import runJob from 'renderer/runJob'
import Box from 'components/base/Box'
import Modal, { ModalBody } from 'components/base/Modal'
@ -74,11 +74,12 @@ class AppsList extends PureComponent<Props, State> {
_unmounted = false
async fetchList() {
console.log(`fetching app list`)
const appsList =
CACHED_APPS ||
(await runJob({
channel: 'usb',
job: 'manager.listApps',
channel: 'manager',
job: 'listApps',
successResponse: 'manager.listAppsSuccess',
errorResponse: 'manager.listAppsError',
}))

11
src/components/ManagerPage/DeviceInfos.js

@ -2,7 +2,8 @@
import React, { PureComponent } from 'react'
import { runJob } from 'renderer/events'
import runJob from 'renderer/runJob'
import Text from 'components/base/Text'
import Box, { Card } from 'components/base/Box'
import Button from 'components/base/Button'
@ -33,10 +34,10 @@ class DeviceInfos extends PureComponent<Props, State> {
device: { path: devicePath },
} = this.props
const memoryInfos = await runJob({
channel: 'usb',
job: 'manager.getMemInfos',
successResponse: 'device.getMemInfosSuccess',
errorResponse: 'device.getMemInfosError',
channel: 'manager',
job: 'getMemInfos',
successResponse: 'manager.getMemInfosSuccess',
errorResponse: 'manager.getMemInfosError',
data: { devicePath },
})
this.setState({ memoryInfos, isLoading: false })

12
src/components/modals/AddAccount/index.js

@ -20,7 +20,7 @@ import {
decodeAccount,
} from 'reducers/accounts'
import { runJob } from 'renderer/events'
import runJob from 'renderer/runJob'
import { addAccount, updateAccount } from 'actions/accounts'
@ -120,10 +120,10 @@ class AddAccountModal extends PureComponent<Props, State> {
try {
// scan every account for given currency and device
await runJob({
channel: 'usb',
job: 'wallet.scanAccountsOnDevice',
successResponse: 'wallet.scanAccountsOnDevice.success',
errorResponse: 'wallet.scanAccountsOnDevice.fail',
channel: 'accounts',
job: 'scan',
successResponse: 'accounts.scanAccountsOnDevice.success',
errorResponse: 'accounts.scanAccountsOnDevice.fail',
data: {
devicePath: deviceSelected.path,
currencyId: currency.id,
@ -186,7 +186,7 @@ class AddAccountModal extends PureComponent<Props, State> {
handleMsgEvent = (e, { data, type }) => {
const { addAccount, existingAccounts } = this.props
if (type === 'wallet.scanAccountsOnDevice.accountScanned') {
if (type === 'accounts.scanAccountsOnDevice.accountScanned') {
// create Account from AccountRaw account scanned on device
const account = {
...decodeAccount(data),

0
src/internals/usb/wallet/accounts.js → src/internals/accounts/helpers.js

70
src/internals/accounts/index.js

@ -1 +1,69 @@
export sync from './sync'
// @flow
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import type { IPCSend } from 'types/electron'
import scanAccountsOnDevice from './scanAccountsOnDevice'
import { verifyAddress, getFreshReceiveAddress } from './helpers'
import sync from './sync'
export default {
sync,
scan: async (
send: IPCSend,
{
devicePath,
currencyId,
}: {
devicePath: string,
currencyId: string,
},
) => {
try {
send('accounts.scanAccountsOnDevice.start', { pid: process.pid }, { kill: false })
const accounts = await scanAccountsOnDevice({
devicePath,
currencyId,
onAccountScanned: account => {
send('accounts.scanAccountsOnDevice.accountScanned', account, { kill: false })
},
})
send('accounts.scanAccountsOnDevice.success', accounts)
} catch (err) {
send('accounts.scanAccountsOnDevice.fail', err)
}
},
getFreshReceiveAddress: async (
send: IPCSend,
{
currencyId,
accountIndex,
}: {
currencyId: string,
accountIndex: number,
},
) => {
try {
const freshAddress = await getFreshReceiveAddress({ currencyId, accountIndex })
send('accounts.getFreshReceiveAddress.success', freshAddress)
} catch (err) {
send('accounts.getFreshReceiveAddress.fail', err)
}
},
verifyAddress: async (
send: IPCSend,
{ pathDevice, path }: { pathDevice: string, path: string },
) => {
const transport = await CommNodeHid.open(pathDevice)
try {
await verifyAddress({ transport, path })
send('accounts.verifyAddress.success')
} catch (err) {
send('accounts.verifyAddress.fail')
}
},
}

0
src/internals/usb/wallet/scanAccountsOnDevice.js → src/internals/accounts/scanAccountsOnDevice.js

10
src/internals/accounts/sync.js

@ -1,7 +1,7 @@
// @flow
export default (send: Function) => ({
all: () => {
setTimeout(() => send('accounts.sync.success'), 5e3)
},
})
import type { IPCSend } from 'types/electron'
export default (send: IPCSend) => {
setTimeout(() => send('accounts.sync.success'), 5e3)
}

44
src/internals/devices/checkIfAppOpened.js

@ -0,0 +1,44 @@
// @flow
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import Btc from '@ledgerhq/hw-app-btc'
import type { IPCSend } from 'types/electron'
import { getPath } from 'internals/accounts/helpers'
export default async (
send: IPCSend,
{
currencyId,
devicePath,
accountPath,
accountAddress,
segwit = true,
}: {
currencyId?: string,
devicePath: string,
accountPath: string,
accountAddress: string,
segwit: boolean,
},
) => {
try {
const transport = await CommNodeHid.open(devicePath)
const btc = new Btc(transport)
if (accountPath) {
const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit)
if (bitcoinAddress === accountAddress) {
send('devices.checkIfAppOpened.success', { devicePath })
} else {
throw new Error('Address is different')
}
}
if (currencyId) {
await btc.getWalletPublicKey(getPath({ currencyId, segwit }), false, segwit)
send('devices.checkIfAppOpened.success', { devicePath })
}
} catch (err) {
send('devices.checkIfAppOpened.fail', { devicePath })
}
}

2
src/internals/devices/index.js

@ -0,0 +1,2 @@
export listen from './listen'
export checkIfAppOpened from './checkIfAppOpened'

24
src/internals/devices/listen.js

@ -0,0 +1,24 @@
// @flow
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import noop from 'lodash/noop'
import type { IPCSend } from 'types/electron'
export default (send: IPCSend) => {
CommNodeHid.listen({
error: noop,
complete: noop,
next: async e => {
if (!e.device) {
return
}
if (e.type === 'add') {
send('device.add', e.device, { kill: false })
}
if (e.type === 'remove') {
send('device.remove', e.device, { kill: false })
}
},
})
}

20
src/internals/index.js

@ -17,23 +17,21 @@ function sendEvent(type: string, data: any, options: Object = { kill: true }) {
}
// $FlowFixMe
const func = require(`./${FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require
const handlers = Object.keys(func).reduce((result, key) => {
result[key] = func[key](sendEvent)
return result
}, {})
let handlers = require(`./${FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require
// handle babel export object syntax
if (handlers.default) {
handlers = handlers.default
}
const onMessage = payload => {
process.on('message', payload => {
const { type, data } = payload
const handler = objectPath.get(handlers, type)
if (!handler) {
console.warn(`No handler found for ${type}`)
return
}
handler(data)
}
process.on('message', onMessage)
handler(sendEvent, data)
})
if (__DEV__ || DEV_TOOLS) {
cpuUsage(cpuPercent =>

0
src/internals/usb/manager/constants.js → src/internals/manager/constants.js

12
src/internals/manager/getMemInfos.js

@ -0,0 +1,12 @@
// @flow
import type { IPCSend } from 'types/electron'
import { createTransportHandler, getMemInfos } from './helpers'
export default (send: IPCSend, data: any) =>
createTransportHandler(send, {
action: getMemInfos,
successResponse: 'manager.getMemInfosSuccess',
errorResponse: 'manager.getMemInfosError',
})(data)

0
src/internals/usb/manager/helpers.js → src/internals/manager/helpers.js

22
src/internals/manager/index.js

@ -0,0 +1,22 @@
// @flow
/**
* Manager
* -------
*
* xXx
* xXx
* xXx
* xxxXxxx
* xxXxx
* xXx
* xX x Xx
* xX Xx
* xxXXXXXXXxx
*
*/
export { default as getMemInfos } from './getMemInfos'
export { default as installApp } from './installApp'
export { default as listApps } from './listApps'
export { default as uninstallApp } from './uninstallApp'

12
src/internals/manager/installApp.js

@ -0,0 +1,12 @@
// @flow
import type { IPCSend } from 'types/electron'
import { createTransportHandler, installApp } from './helpers'
export default (send: IPCSend, data: any) =>
createTransportHandler(send, {
action: installApp,
successResponse: 'manager.appInstalled',
errorResponse: 'manager.appInstallError',
})(data)

14
src/internals/manager/listApps.js

@ -0,0 +1,14 @@
// @flow
import axios from 'axios'
import type { IPCSend } from 'types/electron'
export default async (send: IPCSend) => {
try {
const { data } = await axios.get('https://api.ledgerwallet.com/update/applications')
send('manager.listAppsSuccess', data['nanos-1.4'])
} catch (err) {
send('manager.listAppsError', { message: err.message, stack: err.stack })
}
}

12
src/internals/manager/uninstallApp.js

@ -0,0 +1,12 @@
// @flow
import type { IPCSend } from 'types/electron'
import { createTransportHandler, uninstallApp } from './helpers'
export default (send: IPCSend, data: any) =>
createTransportHandler(send, {
action: uninstallApp,
successResponse: 'manager.appUninstalled',
errorResponse: 'manager.appUninstallError',
})(data)

28
src/internals/usb/devices.js

@ -1,28 +0,0 @@
// @flow
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import noop from 'lodash/noop'
import type { IPCSend } from 'types/electron'
export default (send: IPCSend) => ({
listen: () => {
CommNodeHid.listen({
error: noop,
complete: noop,
next: async e => {
if (!e.device) {
return
}
if (e.type === 'add') {
send('device.add', e.device, { kill: false })
}
if (e.type === 'remove') {
send('device.remove', e.device, { kill: false })
}
},
})
},
})

3
src/internals/usb/index.js

@ -1,3 +0,0 @@
export devices from './devices'
export wallet from './wallet'
export manager from './manager'

50
src/internals/usb/manager/index.js

@ -1,50 +0,0 @@
// @flow
/**
* Manager
* -------
*
* xXx
* xXx
* xXx
* xxxXxxx
* xxXxx
* xXx
* xX x Xx
* xX Xx
* xxXXXXXXXxx
*
*/
import type { IPCSend } from 'types/electron'
import axios from 'axios'
import { createTransportHandler, installApp, uninstallApp, getMemInfos } from './helpers'
export default (send: IPCSend) => ({
installApp: createTransportHandler(send, {
action: installApp,
successResponse: 'device.appInstalled',
errorResponse: 'device.appInstallError',
}),
uninstallApp: createTransportHandler(send, {
action: uninstallApp,
successResponse: 'device.appUninstalled',
errorResponse: 'device.appUninstallError',
}),
getMemInfos: createTransportHandler(send, {
action: getMemInfos,
successResponse: 'device.getMemInfosSuccess',
errorResponse: 'device.getMemInfosError',
}),
listApps: async () => {
try {
const { data } = await axios.get('https://api.ledgerwallet.com/update/applications')
send('manager.listAppsSuccess', data['nanos-1.4'])
} catch (err) {
send('manager.listAppsError', { message: err.message, stack: err.stack })
}
},
})

105
src/internals/usb/wallet/index.js

@ -1,105 +0,0 @@
// @flow
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import Btc from '@ledgerhq/hw-app-btc'
import { getPath, verifyAddress, getFreshReceiveAddress } from './accounts'
import scanAccountsOnDevice from './scanAccountsOnDevice'
export default (sendEvent: Function) => ({
/**
* Scan all the accounts for the given device and currency and returns them
*/
scanAccountsOnDevice: async ({
devicePath,
currencyId,
}: {
devicePath: string,
currencyId: string,
}) => {
try {
sendEvent('wallet.scanAccountsOnDevice.start', { pid: process.pid }, { kill: false })
const accounts = await scanAccountsOnDevice({
devicePath,
currencyId,
onAccountScanned: account => {
sendEvent('wallet.scanAccountsOnDevice.accountScanned', account, { kill: false })
},
})
sendEvent('wallet.scanAccountsOnDevice.success', accounts)
} catch (err) {
sendEvent('wallet.scanAccountsOnDevice.fail', err)
}
},
getAccounts: async ({
pathDevice,
currencyId,
currentAccounts,
}: {
pathDevice: string,
currencyId: string,
currentAccounts: Array<string>,
}) => {
console.warn(
`NOT IMPLEMENTED: getting account for ${pathDevice} ${currencyId} ${currentAccounts.length}`,
)
},
getFreshReceiveAddress: async ({
currencyId,
accountIndex,
}: {
currencyId: string,
accountIndex: number,
}) => {
try {
console.log(accountIndex)
const freshAddress = await getFreshReceiveAddress({ currencyId, accountIndex })
sendEvent('wallet.getFreshReceiveAddress.success', freshAddress)
} catch (err) {
sendEvent('wallet.getFreshReceiveAddress.fail', err)
}
},
verifyAddress: async ({ pathDevice, path }: { pathDevice: string, path: string }) => {
const transport = await CommNodeHid.open(pathDevice)
try {
await verifyAddress({ transport, path })
sendEvent('wallet.verifyAddress.success')
} catch (err) {
sendEvent('wallet.verifyAddress.fail')
}
},
checkIfAppOpened: async ({
currencyId,
devicePath,
accountPath,
accountAddress,
segwit = true,
}: {
currencyId?: string,
devicePath: string,
accountPath: string,
accountAddress: string,
segwit: boolean,
}) => {
try {
const transport = await CommNodeHid.open(devicePath)
const btc = new Btc(transport)
if (accountPath) {
const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit)
if (bitcoinAddress === accountAddress) {
sendEvent('wallet.checkIfAppOpened.success', { devicePath })
} else {
throw new Error('Address is different')
}
}
if (currencyId) {
await btc.getWalletPublicKey(getPath({ currencyId, segwit }), false, segwit)
sendEvent('wallet.checkIfAppOpened.success', { devicePath })
}
} catch (err) {
sendEvent('wallet.checkIfAppOpened.fail', { devicePath })
}
},
})

15
src/main/bridge.js

@ -25,7 +25,7 @@ function sendEventToWindow(name, { type, data }) {
}
}
function onForkChannel(forkType, callType) {
function onForkChannel(forkType) {
return (event: any, payload) => {
const { type, data } = payload
@ -51,12 +51,7 @@ function onForkChannel(forkType, callType) {
if (options.window) {
sendEventToWindow(options.window, { type, data })
} else {
if (callType === 'async') {
event.sender.send('msg', { type, data })
}
if (callType === 'sync') {
event.returnValue = { type, data }
}
event.sender.send('msg', { type, data })
}
if (options.kill && compute) {
kill()
@ -71,8 +66,9 @@ function onForkChannel(forkType, callType) {
}
// Forwards every `type` messages to another process
ipcMain.on('usb', onForkChannel('usb', 'async'))
ipcMain.on('accounts', onForkChannel('accounts', 'async'))
ipcMain.on('devices', onForkChannel('devices'))
ipcMain.on('accounts', onForkChannel('accounts'))
ipcMain.on('manager', onForkChannel('manager'))
ipcMain.on('clean-processes', cleanProcesses)
@ -94,6 +90,7 @@ ipcMain.on('msg', (event: any, payload) => {
const { type, data } = payload
const handler = objectPath.get(handlers, type)
if (!handler) {
console.warn(`No handler found for ${type}`)
return
}
const send = (type: string, data: *) => event.sender.send('msg', { type, data })

9
src/reducers/devices.js

@ -2,11 +2,11 @@
import { handleActions } from 'redux-actions'
import type { Device, Devices } from 'types/common'
import type { Device } from 'types/common'
export type DevicesState = {
currentDevice: Device | null,
devices: Devices,
devices: Device[],
}
const state: DevicesState = {
@ -20,11 +20,6 @@ function setCurrentDevice(state) {
}
const handlers: Object = {
UPDATE_DEVICES: (state: DevicesState, { payload: devices }: { payload: Devices }) =>
setCurrentDevice({
...state,
devices,
}),
ADD_DEVICE: (state: DevicesState, { payload: device }: { payload: Device }) =>
setCurrentDevice({
...state,

98
src/renderer/events.js

@ -14,7 +14,7 @@ import { isLocked } from 'reducers/application'
import { setUpdateStatus } from 'reducers/update'
import { updateAccount } from 'actions/accounts'
import { updateDevices, addDevice, removeDevice } from 'actions/devices'
import { addDevice, removeDevice } from 'actions/devices'
import i18n from 'renderer/i18n/electron'
@ -41,37 +41,6 @@ export function sendEvent(channel: string, msgType: string, data: any) {
})
}
export function runJob({
channel,
job,
successResponse,
errorResponse,
data,
}: {
channel: string,
job: string,
successResponse: string,
errorResponse: string,
data?: any,
}): Promise<void> {
return new Promise((resolve, reject) => {
ipcRenderer.send(channel, { type: job, data })
ipcRenderer.on('msg', handler)
function handler(e, res) {
const { type, data } = res
if (![successResponse, errorResponse].includes(type)) {
return
}
ipcRenderer.removeListener('msg', handler)
if (type === successResponse) {
resolve(data)
} else if (type === errorResponse) {
reject(data)
}
}
})
}
export function sendSyncEvent(channel: string, msgType: string, data: any): any {
return ipcRenderer.sendSync(`${channel}:sync`, {
type: msgType,
@ -79,35 +48,6 @@ export function sendSyncEvent(channel: string, msgType: string, data: any): any
})
}
export function startSyncAccounts(accounts: Account[]) {
d.sync('Sync accounts - start')
syncAccountsInProgress = true
sendEvent('accounts', 'sync.all', {
accounts: accounts.map(account => {
const { id, currency, rootPath, addresses, index, operations } = account
return {
id,
currencyId: currency.id,
allAddresses: addresses,
currentIndex: index,
rootPath,
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)
}
export default ({ store, locked }: { store: Object, locked: boolean }) => {
const handlers = {
dispatch: ({ type, payload }) => store.dispatch({ type, payload }),
@ -170,11 +110,6 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
},
},
},
devices: {
update: devices => {
store.dispatch(updateDevices(devices))
},
},
device: {
add: device => {
d.device('Device - add')
@ -208,7 +143,7 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
ipcRenderer.send('clean-processes')
// Start detection when we plug/unplug devices
sendEvent('usb', 'devices.listen')
sendEvent('devices', 'listen')
const state = store.getState()
@ -226,3 +161,32 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
checkUpdates()
}
}
export function startSyncAccounts(accounts: Account[]) {
d.sync('Sync accounts - start')
syncAccountsInProgress = true
sendEvent('accounts', 'sync', {
accounts: accounts.map(account => {
const { id, currency, rootPath, addresses, index, operations } = account
return {
id,
currencyId: currency.id,
allAddresses: addresses,
currentIndex: index,
rootPath,
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)
}

34
src/renderer/runJob.js

@ -0,0 +1,34 @@
// @flow
import { ipcRenderer } from 'electron'
export default function runJob({
channel,
job,
successResponse,
errorResponse,
data,
}: {
channel: string,
job: string,
successResponse: string,
errorResponse: string,
data?: any,
}): Promise<void> {
return new Promise((resolve, reject) => {
ipcRenderer.send(channel, { type: job, data })
ipcRenderer.on('msg', handler)
function handler(e, res) {
const { type, data } = res
if (![successResponse, errorResponse].includes(type)) {
return
}
ipcRenderer.removeListener('msg', handler)
if (type === successResponse) {
resolve(data)
} else if (type === errorResponse) {
reject(data)
}
}
})
}

2
src/types/common.js

@ -8,8 +8,6 @@ export type Device = {
vendorId: string,
}
export type Devices = Array<Device>
// -------------------- Settings
export type CurrencySettings = {

Loading…
Cancel
Save