-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,13 +69,28 @@ export async function setupXkeysPanel(browserDevice: HIDDevice): Promise<XKeys> | |
}) | ||
}) | ||
|
||
// Wait for the device to initialize: | ||
try { | ||
await xkeys.init() | ||
|
||
return xkeys | ||
} catch (e) { | ||
await deviceWrap.close() | ||
throw e | ||
} | ||
let alreadyRejected = false | ||
return new Promise((resolve, reject) => { | ||
const xkeysStopgapErrorHandler = (e: unknown) => { | ||
if (alreadyRejected) { | ||
console.error(`Xkeys: Error emitted after setup already rejected:`, e) | ||
return | ||
} | ||
deviceWrap.close() | ||
.then(() => reject()) | ||
.catch(reject) | ||
.finally(() => (alreadyRejected = true)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing this in the |
||
} | ||
// Handle all error events until the instance is returned | ||
xkeys.on('error', xkeysStopgapErrorHandler) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't use |
||
|
||
// Wait for the device to initialize: | ||
xkeys.init() | ||
.then(() => { | ||
resolve(xkeys) | ||
xkeys.removeListener('error', xkeysStopgapErrorHandler) | ||
}) | ||
.catch(reject) | ||
.finally(() => (alreadyRejected = true)) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor note: is there a reason the
reject()
has no argument here? Should wereject(e)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for the path where
deviceWrap.close()
promise resolves, where it should resolve to a void promise, but still I want to avoid forwarding whatever value it resolves to as the rejected value of the enclosing (returned) promise in case the API of that method changes.