Skip to content

Commit

Permalink
handle errors emitted by xkeys during init process before the instanc…
Browse files Browse the repository at this point in the history
…e is exposed to the caller
  • Loading branch information
loucadufault committed Dec 4, 2024
1 parent b8a5aef commit 454ac00
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/node/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ export async function setupXkeysPanel(
// Wait for the device to initialize:
await xkeys.init()

let alreadyRejected = false
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) {
if (device) await device.close().catch(() => null) // Suppress error
Expand Down

0 comments on commit 454ac00

Please sign in to comment.