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'
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) {
if (wallet === 'btc') {
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')
}
ipcMain.on('requestWalletInfos', async (event: *, payload) => {
const { path, wallet } = payload
try {
const publicKey = await getWalletInfos(path, wallet)
event.sender.send('receiveWalletInfos', { path, publicKey })
} catch (err) {
event.sender.send('failWalletInfos', { path, err: err.stack })
const handlers = {
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 {
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 =>
event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)),
)
handler(send, data)
})

55
src/renderer/initEvents.js

@ -4,28 +4,51 @@ import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
export default (store: Object) => {
ipcRenderer.on('updateDevices', (e, devices) => {
store.dispatch(devicesUpdate(devices))
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)))
type MsgPayload = {
type: string,
data: *,
}
ipcRenderer.on('receiveWalletInfos', (e, { path, publicKey }) => {
console.log({ path, publicKey })
function send(msgType: string, data: *) {
ipcRenderer.send('msg', {
type: msgType,
data,
})
}
ipcRenderer.on('failWalletInfos', (e, { path, err }) => {
console.log({ path, err })
export default (store: Object) => {
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
ipcRenderer.send('getDevices')
send('getDevices')
// Start detection when we plug/unplug devices
ipcRenderer.send('listenDevices')
send('listenDevices')
}

Loading…
Cancel
Save