From 7af7534ad630b006af45411a76abf28f4d9d4d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Tue, 9 Jan 2018 18:54:14 +0100 Subject: [PATCH] Clean events, add isListenDevices for ipcMain events --- package.json | 7 +-- src/main/ledger.js | 38 +++++++++----- src/renderer/initEvents.js | 33 +++++++----- yarn.lock | 103 ++++++++++++++++++------------------- 4 files changed, 99 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index 2db1a99e..4c1cea42 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,14 @@ }, "dependencies": { "color": "^2.0.1", - "electron": "1.7.9", + "electron": "1.7.10", "electron-devtools-installer": "^2.2.3", "history": "^4.7.2", "i18next": "^10.2.2", "i18next-node-fs-backend": "^1.0.0", "ledger-node-js-hid": "github:loeck/ledger-node-js-hid", "ledgerco": "^1.2.1", + "object-path": "^0.11.4", "react": "^16.2.0", "react-dom": "^16.2.0", "react-i18next": "^7.3.0", @@ -50,7 +51,7 @@ "webpack": "^3.10.0" }, "devDependencies": { - "babel-eslint": "^8.1.2", + "babel-eslint": "^8.2.1", "babel-plugin-module-resolver": "^3.0.0", "babel-preset-flow": "^6.23.0", "babel-preset-react": "^6.24.1", @@ -66,7 +67,7 @@ "eslint-plugin-import": "^2.8.0", "eslint-plugin-jsx-a11y": "^6.0.3", "eslint-plugin-react": "^7.5.1", - "flow-bin": "^0.62.0", + "flow-bin": "^0.63.1", "flow-typed": "^2.2.3", "prettier": "^1.9.2", "react-hot-loader": "^4.0.0-beta.12" diff --git a/src/main/ledger.js b/src/main/ledger.js index 3d8d6fa2..7d403af1 100644 --- a/src/main/ledger.js +++ b/src/main/ledger.js @@ -3,6 +3,7 @@ import { ipcMain } from 'electron' import { isLedgerDevice } from 'ledgerco/lib/utils' import ledgerco, { comm_node } from 'ledgerco' +import objectPath from 'object-path' import HID from 'ledger-node-js-hid' @@ -16,17 +17,29 @@ async function getWalletInfos(path: string, wallet: string) { throw new Error('invalid wallet') } +let isListenDevices = false + 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), - ) + devices: { + listen: send => { + if (isListenDevices) { + return + } + + isListenDevices = true + + HID.listenDevices.start() + + HID.listenDevices.events.on( + 'add', + device => isLedgerDevice(device) && send('device.add', device), + ) + HID.listenDevices.events.on( + 'remove', + device => isLedgerDevice(device) && send('device.remove', device), + ) + }, + all: send => send('devices.update', HID.devices().filter(isLedgerDevice)), }, requestWalletInfos: async (send, { path, wallet }) => { try { @@ -36,15 +49,12 @@ const handlers = { 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] + const handler = objectPath.get(handlers, type) if (!handler) { return } diff --git a/src/renderer/initEvents.js b/src/renderer/initEvents.js index ecb148b4..ad8995ad 100644 --- a/src/renderer/initEvents.js +++ b/src/renderer/initEvents.js @@ -1,6 +1,7 @@ // @flow import { ipcRenderer } from 'electron' +import objectPath from 'object-path' import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices' @@ -18,17 +19,21 @@ function send(msgType: string, data: *) { export default (store: Object) => { const handlers = { - updateDevices: devices => { - store.dispatch(devicesUpdate(devices)) - if (devices.length) { - send('requestWalletInfos', { - path: devices[0].path, - wallet: 'btc', - }) - } + devices: { + update: devices => { + store.dispatch(devicesUpdate(devices)) + if (devices.length) { + send('requestWalletInfos', { + path: devices[0].path, + wallet: 'btc', + }) + } + }, + }, + device: { + add: device => store.dispatch(deviceAdd(device)), + remove: device => store.dispatch(deviceRemove(device)), }, - addDevice: device => store.dispatch(deviceAdd(device)), - removeDevice: device => store.dispatch(deviceRemove(device)), receiveWalletInfos: ({ path, publicKey }) => { console.log({ path, publicKey }) }, @@ -39,16 +44,18 @@ export default (store: Object) => { ipcRenderer.on('msg', (e: *, payload: MsgPayload) => { const { type, data } = payload - const handler = handlers[type] + + const handler = objectPath.get(handlers, type) if (!handler) { return } + handler(data) }) // First time, we get all devices - send('getDevices') + send('devices.all') // Start detection when we plug/unplug devices - send('listenDevices') + send('devices.listen') } diff --git a/yarn.lock b/yarn.lock index 4bc35643..b709c8dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -26,54 +26,53 @@ version "0.0.6" resolved "https://registry.yarnpkg.com/7zip/-/7zip-0.0.6.tgz#9cafb171af82329490353b4816f03347aa150a30" -"@babel/code-frame@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.31.tgz#473d021ecc573a2cce1c07d5b509d5215f46ba35" +"@babel/code-frame@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.36.tgz#2349d7ec04b3a06945ae173280ef8579b63728e4" dependencies: chalk "^2.0.0" esutils "^2.0.2" js-tokens "^3.0.0" -"@babel/helper-function-name@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.31.tgz#afe63ad799209989348b1109b44feb66aa245f57" +"@babel/helper-function-name@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.36.tgz#366e3bc35147721b69009f803907c4d53212e88d" dependencies: - "@babel/helper-get-function-arity" "7.0.0-beta.31" - "@babel/template" "7.0.0-beta.31" - "@babel/traverse" "7.0.0-beta.31" - "@babel/types" "7.0.0-beta.31" + "@babel/helper-get-function-arity" "7.0.0-beta.36" + "@babel/template" "7.0.0-beta.36" + "@babel/types" "7.0.0-beta.36" -"@babel/helper-get-function-arity@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.31.tgz#1176d79252741218e0aec872ada07efb2b37a493" +"@babel/helper-get-function-arity@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.36.tgz#f5383bac9a96b274828b10d98900e84ee43e32b8" dependencies: - "@babel/types" "7.0.0-beta.31" + "@babel/types" "7.0.0-beta.36" -"@babel/template@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.31.tgz#577bb29389f6c497c3e7d014617e7d6713f68bda" +"@babel/template@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.36.tgz#02e903de5d68bd7899bce3c5b5447e59529abb00" dependencies: - "@babel/code-frame" "7.0.0-beta.31" - "@babel/types" "7.0.0-beta.31" - babylon "7.0.0-beta.31" + "@babel/code-frame" "7.0.0-beta.36" + "@babel/types" "7.0.0-beta.36" + babylon "7.0.0-beta.36" lodash "^4.2.0" -"@babel/traverse@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.31.tgz#db399499ad74aefda014f0c10321ab255134b1df" +"@babel/traverse@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.36.tgz#1dc6f8750e89b6b979de5fe44aa993b1a2192261" dependencies: - "@babel/code-frame" "7.0.0-beta.31" - "@babel/helper-function-name" "7.0.0-beta.31" - "@babel/types" "7.0.0-beta.31" - babylon "7.0.0-beta.31" + "@babel/code-frame" "7.0.0-beta.36" + "@babel/helper-function-name" "7.0.0-beta.36" + "@babel/types" "7.0.0-beta.36" + babylon "7.0.0-beta.36" debug "^3.0.1" - globals "^10.0.0" + globals "^11.1.0" invariant "^2.2.0" lodash "^4.2.0" -"@babel/types@7.0.0-beta.31": - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.31.tgz#42c9c86784f674c173fb21882ca9643334029de4" +"@babel/types@7.0.0-beta.36": + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.36.tgz#64f2004353de42adb72f9ebb4665fc35b5499d23" dependencies: esutils "^2.0.2" lodash "^4.2.0" @@ -408,14 +407,14 @@ babel-core@^6.24.1, babel-core@^6.26.0: slash "^1.0.0" source-map "^0.5.6" -babel-eslint@^8.1.2: - version "8.1.2" - resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.1.2.tgz#a39230b0c20ecbaa19a35d5633bf9b9ca2c8116f" +babel-eslint@^8.2.1: + version "8.2.1" + resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.1.tgz#136888f3c109edc65376c23ebf494f36a3e03951" dependencies: - "@babel/code-frame" "7.0.0-beta.31" - "@babel/traverse" "7.0.0-beta.31" - "@babel/types" "7.0.0-beta.31" - babylon "7.0.0-beta.31" + "@babel/code-frame" "7.0.0-beta.36" + "@babel/traverse" "7.0.0-beta.36" + "@babel/types" "7.0.0-beta.36" + babylon "7.0.0-beta.36" eslint-scope "~3.7.1" eslint-visitor-keys "^1.0.0" @@ -1267,9 +1266,9 @@ babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26.0: lodash "^4.17.4" to-fast-properties "^1.0.3" -babylon@7.0.0-beta.31: - version "7.0.0-beta.31" - resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.31.tgz#7ec10f81e0e456fd0f855ad60fa30c2ac454283f" +babylon@7.0.0-beta.36: + version "7.0.0-beta.36" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.36.tgz#3a3683ba6a9a1e02b0aa507c8e63435e39305b9e" babylon@^6.18.0: version "6.18.0" @@ -2579,9 +2578,9 @@ electron-webpack@1.11.0: webpack-merge "^4.1.0" yargs "^10.0.3" -electron@1.7.9: - version "1.7.9" - resolved "https://registry.yarnpkg.com/electron/-/electron-1.7.9.tgz#add54e9f8f83ed02f6519ec10135f698b19336cf" +electron@1.7.10: + version "1.7.10" + resolved "https://registry.yarnpkg.com/electron/-/electron-1.7.10.tgz#3a3e83d965fd7fafe473be8ddf8f472561b6253d" dependencies: "@types/node" "^7.0.18" electron-download "^3.0.1" @@ -3206,9 +3205,9 @@ flatten@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782" -flow-bin@^0.62.0: - version "0.62.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.62.0.tgz#14bca669a6e3f95c0bc0c2d1eb55ec4e98cb1d83" +flow-bin@^0.63.1: + version "0.63.1" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.63.1.tgz#ab00067c197169a5fb5b4996c8f6927b06694828" flow-typed@^2.2.3: version "2.2.3" @@ -3441,11 +3440,7 @@ global@^4.3.0: min-document "^2.19.0" process "~0.5.1" -globals@^10.0.0: - version "10.4.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-10.4.0.tgz#5c477388b128a9e4c5c5d01c7a2aca68c68b2da7" - -globals@^11.0.1: +globals@^11.0.1, globals@^11.1.0: version "11.1.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.1.0.tgz#632644457f5f0e3ae711807183700ebf2e4633e4" @@ -4889,6 +4884,10 @@ object-keys@~0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-0.4.0.tgz#28a6aae7428dd2c3a92f3d95f21335dd204e0336" +object-path@^0.11.4: + version "0.11.4" + resolved "https://registry.yarnpkg.com/object-path/-/object-path-0.11.4.tgz#370ae752fbf37de3ea70a861c23bba8915691949" + object.omit@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/object.omit/-/object.omit-2.0.1.tgz#1a9c744829f39dbb858c76ca3579ae2a54ebd1fa"