Browse Source

refactor the way we send events between main & renderer

master
meriadec 7 years ago
parent
commit
61c75fa617
No known key found for this signature in database GPG Key ID: C9A285535CC6EA7D
  1. 65
      src/main/ledger.js
  2. 55
      src/renderer/initEvents.js

65
src/main/ledger.js

@ -6,20 +6,6 @@ import ledgerco, { comm_node } from 'ledgerco'
import HID from 'ledger-node-js-hid' import HID from 'ledger-node-js-hid'
ipcMain.on('listenDevices', event => {
HID.listenDevices.start()
HID.listenDevices.events.on(
'add',
device => isLedgerDevice(device) && event.sender.send('addDevice', device),
)
HID.listenDevices.events.on(
'remove',
device => isLedgerDevice(device) && event.sender.send('removeDevice', device),
)
})
async function getWalletInfos(path: string, wallet: string) { async function getWalletInfos(path: string, wallet: string) {
if (wallet === 'btc') { if (wallet === 'btc') {
const comm = new comm_node(new HID.HID(path), true, 0, false) const comm = new comm_node(new HID.HID(path), true, 0, false)
@ -30,16 +16,45 @@ async function getWalletInfos(path: string, wallet: string) {
throw new Error('invalid wallet') throw new Error('invalid wallet')
} }
ipcMain.on('requestWalletInfos', async (event: *, payload) => { const handlers = {
const { path, wallet } = payload listenDevices: send => {
try { HID.listenDevices.start()
const publicKey = await getWalletInfos(path, wallet) HID.listenDevices.events.on(
event.sender.send('receiveWalletInfos', { path, publicKey }) 'add',
} catch (err) { device => isLedgerDevice(device) && send('addDevice', device),
event.sender.send('failWalletInfos', { path, err: err.stack }) )
HID.listenDevices.events.on(
'remove',
device => isLedgerDevice(device) && send('removeDevice', device),
)
},
requestWalletInfos: async (send, { path, wallet }) => {
try {
const publicKey = await getWalletInfos(path, wallet)
send('receiveWalletInfos', { path, publicKey })
} catch (err) {
send('failWalletInfos', { path, err: err.stack })
}
},
getDevices: send => {
send('updateDevices', HID.devices().filter(isLedgerDevice))
},
}
ipcMain.on('msg', (event: *, payload) => {
const { type, data } = payload
const handler = handlers[type]
if (!handler) {
return
}
const send = (msgType: string, data: *) => {
event.sender.send('msg', {
type: msgType,
data,
})
} }
})
ipcMain.on('getDevices', event => handler(send, data)
event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)), })
)

55
src/renderer/initEvents.js

@ -4,28 +4,51 @@ import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices' import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
export default (store: Object) => { type MsgPayload = {
ipcRenderer.on('updateDevices', (e, devices) => { type: string,
store.dispatch(devicesUpdate(devices)) data: *,
if (devices.length) { }
ipcRenderer.send('requestWalletInfos', { path: devices[0].path, wallet: 'btc' })
}
})
ipcRenderer.on('addDevice', (e, device) => store.dispatch(deviceAdd(device)))
ipcRenderer.on('removeDevice', (e, device) => store.dispatch(deviceRemove(device)))
ipcRenderer.on('receiveWalletInfos', (e, { path, publicKey }) => { function send(msgType: string, data: *) {
console.log({ path, publicKey }) ipcRenderer.send('msg', {
type: msgType,
data,
}) })
}
ipcRenderer.on('failWalletInfos', (e, { path, err }) => { export default (store: Object) => {
console.log({ path, err }) const handlers = {
updateDevices: devices => {
store.dispatch(devicesUpdate(devices))
if (devices.length) {
send('requestWalletInfos', {
path: devices[0].path,
wallet: 'btc',
})
}
},
addDevice: device => store.dispatch(deviceAdd(device)),
removeDevice: device => store.dispatch(deviceRemove(device)),
receiveWalletInfos: ({ path, publicKey }) => {
console.log({ path, publicKey })
},
failWalletInfos: ({ path, err }) => {
console.log({ path, err })
},
}
ipcRenderer.on('msg', (e: *, payload: MsgPayload) => {
const { type, data } = payload
const handler = handlers[type]
if (!handler) {
return
}
handler(data)
}) })
// First time, we get all devices // First time, we get all devices
ipcRenderer.send('getDevices') send('getDevices')
// Start detection when we plug/unplug devices // Start detection when we plug/unplug devices
ipcRenderer.send('listenDevices') send('listenDevices')
} }

Loading…
Cancel
Save