Skip to content

Commit

Permalink
fix(electron): move powerManager interactions into app.whenReady
Browse files Browse the repository at this point in the history
  • Loading branch information
lemnik committed Oct 11, 2023
1 parent 86e96f9 commit cf447e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-electron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
electron: [ '^12.0.0', '^20.0.0' ]
electron: [ '^12.0.0', '^20.0.0', '^25.0.0', '^26.0.0' ]
node-version: [12, 14]
os: [ ubuntu-20.04, windows-2019 ]
os: [ ubuntu-22.04, windows-2019 ]

steps:
- uses: actions/checkout@v2
Expand Down
80 changes: 41 additions & 39 deletions packages/plugin-electron-device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,49 @@ module.exports = (app, screen, process, filestore, NativeClient, powerMonitor) =
}
})

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',
// note: this is only available from Electron 12, earlier versions cannot
// read the battery state without 'on-ac'/'on-battery' events
usingBattery: powerMonitor.onBatteryPower
app.whenReady().then(() => {
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',
// note: this is only available from Electron 12, earlier versions cannot
// read the battery state without 'on-ac'/'on-battery' events
usingBattery: powerMonitor.onBatteryPower
})

powerMonitor.on('on-ac', () => {
client.addMetadata('device', { usingBattery: false })
})

powerMonitor.on('on-battery', () => {
client.addMetadata('device', { usingBattery: true })
})

powerMonitor.on('unlock-screen', () => {
client.addMetadata('device', { isLocked: false })
})

powerMonitor.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)

app.whenReady().then(() => {
// on windows, app.getLocale won't return the locale until the app is ready
const locale = app.getLocale()
Expand Down Expand Up @@ -88,38 +122,6 @@ module.exports = (app, screen, process, filestore, NativeClient, powerMonitor) =
})
})

powerMonitor.on('on-ac', () => {
client.addMetadata('device', { usingBattery: false })
})

powerMonitor.on('on-battery', () => {
client.addMetadata('device', { usingBattery: true })
})

powerMonitor.on('unlock-screen', () => {
client.addMetadata('device', { isLocked: false })
})

powerMonitor.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)

client.addOnSession(session => {
session.device = Object.assign(
{},
Expand Down
3 changes: 2 additions & 1 deletion scripts/cppcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ SUPPRESSED_ERRORS=(\
--suppress='unusedVariable:*/deps/*' \
--suppress='variableScope:*/deps/*' \
--suppress='ConfigurationNotChecked:*/deps/parson/parson.c:1425' \
--suppress='knownConditionTrueFalse:*/deps/parson/parson.c:692' \
--suppress='knownConditionTrueFalse:*/deps/parson/parson.c:600' \
--suppress='memleak:*/plugin-electron-client-state-persistence/src/deps/tinycthread/tinycthread.c:620' \
--suppress='constParameter:*/plugin-electron-client-state-persistence/src/deps/tinycthread/tinycthread.c:591' \
--suppress='unusedFunction:*/plugin-electron-client-state-persistence/src/api.c:513' \
--suppress='unusedFunction:*/plugin-electron-app/src/api.c:60')

Expand Down
2 changes: 1 addition & 1 deletion test/electron/features/support/steps/request-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { readFixtureFile } = require('../utils')
const expect = require('../utils/expect')
const { applySourcemaps } = require('../utils/source-mapper')

const REQUEST_RESOLUTION_TIMEOUT = 3000
const REQUEST_RESOLUTION_TIMEOUT = 5000
const launchConfig = { timeout: 30 * 1000 }
const requestDelay = (callback) => new Promise((resolve, reject) => {
setTimeout(() => callback(resolve), REQUEST_RESOLUTION_TIMEOUT)
Expand Down

0 comments on commit cf447e3

Please sign in to comment.