diff --git a/packages/node/src/methods.ts b/packages/node/src/methods.ts index 9f22cb1..7942ac2 100644 --- a/packages/node/src/methods.ts +++ b/packages/node/src/methods.ts @@ -116,6 +116,34 @@ export async function setupXkeysPanel( // Wait for the device to initialize: await xkeys.init() + let alreadyRejected = false + await new Promise((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