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. 59
      src/main/ledger.js
  2. 49
      src/renderer/initEvents.js

59
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 => {
HID.listenDevices.start()
HID.listenDevices.events.on(
'add',
device => isLedgerDevice(device) && send('addDevice', device),
)
HID.listenDevices.events.on(
'remove',
device => isLedgerDevice(device) && send('removeDevice', device),
)
},
requestWalletInfos: async (send, { path, wallet }) => {
try { try {
const publicKey = await getWalletInfos(path, wallet) const publicKey = await getWalletInfos(path, wallet)
event.sender.send('receiveWalletInfos', { path, publicKey }) send('receiveWalletInfos', { path, publicKey })
} catch (err) { } catch (err) {
event.sender.send('failWalletInfos', { path, err: err.stack }) send('failWalletInfos', { path, err: err.stack })
} }
}) },
getDevices: send => {
send('updateDevices', HID.devices().filter(isLedgerDevice))
},
}
ipcMain.on('getDevices', event => ipcMain.on('msg', (event: *, payload) => {
event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)), const { type, data } = payload
)
const handler = handlers[type]
if (!handler) {
return
}
const send = (msgType: string, data: *) => {
event.sender.send('msg', {
type: msgType,
data,
})
}
handler(send, data)
})

49
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'
type MsgPayload = {
type: string,
data: *,
}
function send(msgType: string, data: *) {
ipcRenderer.send('msg', {
type: msgType,
data,
})
}
export default (store: Object) => { export default (store: Object) => {
ipcRenderer.on('updateDevices', (e, devices) => { const handlers = {
updateDevices: devices => {
store.dispatch(devicesUpdate(devices)) store.dispatch(devicesUpdate(devices))
if (devices.length) { if (devices.length) {
ipcRenderer.send('requestWalletInfos', { path: devices[0].path, wallet: 'btc' }) 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))) addDevice: device => store.dispatch(deviceAdd(device)),
removeDevice: device => store.dispatch(deviceRemove(device)),
ipcRenderer.on('receiveWalletInfos', (e, { path, publicKey }) => { receiveWalletInfos: ({ path, publicKey }) => {
console.log({ path, publicKey }) console.log({ path, publicKey })
}) },
failWalletInfos: ({ path, err }) => {
ipcRenderer.on('failWalletInfos', (e, { path, err }) => {
console.log({ 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