Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/113'
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Dec 5, 2024
2 parents 723cf1b + 494f579 commit 193eb78
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/webhid/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,34 @@ export async function setupXkeysPanel(browserDevice: HIDDevice): Promise<XKeys>
})
})

// Wait for the device to initialize:
let alreadyRejected = false
try {
await xkeys.init()
await new Promise<void>((resolve, reject) => {
const markRejected = (e: unknown) => {
reject(e)
alreadyRejected = true
}
const xkeysStopgapErrorHandler = (e: unknown) => {
if (alreadyRejected) {
console.error(`Xkeys: Error emitted after setup already rejected:`, e)
return
}

markRejected(e)
}

// Handle all error events until the instance is returned
xkeys.on('error', xkeysStopgapErrorHandler)

// Wait for the device to initialize:
xkeys
.init()
.then(() => {
resolve()
xkeys.removeListener('error', xkeysStopgapErrorHandler)
})
.catch(markRejected)
})

return xkeys
} catch (e) {
Expand Down

0 comments on commit 193eb78

Please sign in to comment.