diff --git a/packages/plugin-electron-device/device.js b/packages/plugin-electron-device/device.js index 3b0679692e..e83e8c4efc 100644 --- a/packages/plugin-electron-device/device.js +++ b/packages/plugin-electron-device/device.js @@ -45,49 +45,53 @@ module.exports = (app, screen, process, filestore, NativeClient, powerMonitorPro } }) - powerMonitorPromise.then(powerMonitor => { + let powerMonitor = { getSystemIdleTime: () => 'nope' } + + powerMonitorPromise.then(pm => { client.addMetadata('device', { // the value for 'idleThreshold' doesn't matter here because it is ignored // if the system is locked and that's the only state we care about - isLocked: powerMonitor.getSystemIdleState(1) === 'locked', + isLocked: pm.getSystemIdleState(1) === 'locked', // note: this is only available from Electron 12, earlier versions cannot // read the battery state without 'on-ac'/'on-battery' events - usingBattery: powerMonitor.onBatteryPower + usingBattery: pm.onBatteryPower }) - powerMonitor.on('on-ac', () => { + pm.on('on-ac', () => { client.addMetadata('device', { usingBattery: false }) }) - powerMonitor.on('on-battery', () => { + pm.on('on-battery', () => { client.addMetadata('device', { usingBattery: true }) }) - powerMonitor.on('unlock-screen', () => { + pm.on('unlock-screen', () => { client.addMetadata('device', { isLocked: false }) }) - powerMonitor.on('lock-screen', () => { + pm.on('lock-screen', () => { client.addMetadata('device', { isLocked: true }) }) - client.addOnError(event => { - event.device = Object.assign( - {}, - event.device, - device, - { - freeMemory: kibibytesToBytes(process.getSystemMemoryInfo().free), - time: new Date() - } - ) - - event.addMetadata('device', { idleTime: powerMonitor.getSystemIdleTime() }) - - setDefaultUserId(event) - }, true) + powerMonitor = pm }) + client.addOnError(event => { + event.device = Object.assign( + {}, + event.device, + device, + { + freeMemory: kibibytesToBytes(process.getSystemMemoryInfo().free), + time: new Date() + } + ) + + event.addMetadata('device', { idleTime: powerMonitor.getSystemIdleTime() }) + + setDefaultUserId(event) + }, true) + app.whenReady().then(() => { // on windows, app.getLocale won't return the locale until the app is ready const locale = app.getLocale()