Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle error events on xkeys that can be emitted during the init process before the instance is exposed for listener registration #113

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/webhid/src/methods.ts
Original file line number Diff line number Diff line change
@@ -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) {
Loading