From f91e67ca2941523303ec90c91539164677decd1e Mon Sep 17 00:00:00 2001 From: meriadec Date: Wed, 27 Jun 2018 12:33:17 +0200 Subject: [PATCH] Cache device genuinity in GenuineCheck component --- src/components/GenuineCheck.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/components/GenuineCheck.js b/src/components/GenuineCheck.js index 44a307cf..3528b50e 100644 --- a/src/components/GenuineCheck.js +++ b/src/components/GenuineCheck.js @@ -45,6 +45,14 @@ const mapStateToProps = state => ({ const Bold = props => +// to speed up genuine check, cache result by device id +const GENUINITY_CACHE = {} +const getDeviceId = (device: Device) => device.path +const setDeviceGenuinity = (device: Device, isGenuine: boolean) => + (GENUINITY_CACHE[getDeviceId(device)] = isGenuine) +const getDeviceGenuinity = (device: Device): ?boolean => + GENUINITY_CACHE[getDeviceId(device)] || null + class GenuineCheck extends PureComponent { connectInteractionHandler = () => createCancelablePolling(() => { @@ -68,6 +76,9 @@ class GenuineCheck extends PureComponent { device: Device, deviceInfo: DeviceInfo, }) => { + if (getDeviceGenuinity(device) === true) { + return true + } const res = await getIsGenuine .send({ devicePath: device.path, deviceInfo }) .pipe(timeout(GENUINE_TIMEOUT)) @@ -76,6 +87,7 @@ class GenuineCheck extends PureComponent { if (!isGenuine) { return Promise.reject(new Error('Device not genuine')) // TODO: use custom error class } + setDeviceGenuinity(device, true) return Promise.resolve(true) }