Browse Source

Merge pull request #381 from gre/listenDevices-command

migrate listen to a listenDevices command
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
464032bd0e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/commands/listenDevices.js
  2. 6
      src/internals/devices/index.js
  3. 24
      src/internals/devices/listen.js
  4. 28
      src/renderer/events.js

9
src/commands/listenDevices.js

@ -0,0 +1,9 @@
// @flow
import { createCommand } from 'helpers/ipc'
import { Observable } from 'rxjs'
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
const cmd = createCommand('devices', 'listenDevices', () => Observable.create(CommNodeHid.listen))
export default cmd

6
src/internals/devices/index.js

@ -8,14 +8,11 @@ import getFirmwareInfo from 'commands/getFirmwareInfo'
import getIsGenuine from 'commands/getIsGenuine'
import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice'
import installApp from 'commands/installApp'
import listenDevices from 'commands/listenDevices'
import uninstallApp from 'commands/uninstallApp'
import installOsuFirmware from 'commands/installOsuFirmware'
import installFinalFirmware from 'commands/installFinalFirmware'
import installMcu from 'commands/installMcu'
import listen from './listen'
// TODO port these to commands
export { listen }
export const commands: Array<Command<any, any>> = [
getAddress,
@ -25,6 +22,7 @@ export const commands: Array<Command<any, any>> = [
getIsGenuine,
getLatestFirmwareForDevice,
installApp,
listenDevices,
uninstallApp,
installOsuFirmware,
installFinalFirmware,

24
src/internals/devices/listen.js

@ -1,24 +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) => {
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 })
}
},
})
}

28
src/renderer/events.js

@ -18,6 +18,8 @@ import { setUpdateStatus } from 'reducers/update'
import { addDevice, removeDevice } from 'actions/devices'
import listenDevices from 'commands/listenDevices'
import i18n from 'renderer/i18n/electron'
const d = {
@ -51,16 +53,6 @@ export default ({ store }: { store: Object, locked: boolean }) => {
application: {
changeLanguage: lang => i18n.changeLanguage(lang),
},
device: {
add: device => {
d.device('Device - add')
store.dispatch(addDevice(device))
},
remove: device => {
d.device('Device - remove')
store.dispatch(removeDevice(device))
},
},
updater: {
checking: () => store.dispatch(setUpdateStatus('checking')),
updateAvailable: info => store.dispatch(setUpdateStatus('available', info)),
@ -70,7 +62,6 @@ export default ({ store }: { store: Object, locked: boolean }) => {
downloaded: () => store.dispatch(setUpdateStatus('downloaded')),
},
}
ipcRenderer.on('msg', (event: any, payload: MsgPayload) => {
const { type, data } = payload
const handler = objectPath.get(handlers, type)
@ -83,8 +74,19 @@ export default ({ store }: { store: Object, locked: boolean }) => {
// Ensure all sub-processes are killed before creating new ones (dev mode...)
ipcRenderer.send('clean-processes')
// Start detection when we plug/unplug devices
sendEvent('devices', 'listen')
listenDevices.send().subscribe({
next: ({ device, type }) => {
if (device) {
if (type === 'add') {
d.device('Device - add')
store.dispatch(addDevice(device))
} else if (type === 'remove') {
d.device('Device - remove')
store.dispatch(removeDevice(device))
}
}
},
})
if (__PROD__) {
// Start check of eventual updates

Loading…
Cancel
Save