-
Notifications
You must be signed in to change notification settings - Fork 1
fix: update handleMessages to avoid returning true and handle promises correctly #7
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
base: main
Are you sure you want to change the base?
Conversation
|
Maybe you can give your opinion here, @tdulcet? I need a second pair of eyes… |
| gotTrueAsReturn = true; | ||
| continue; | ||
| // If the callback returns a Promise, add it to the list | ||
| if (returnValue && typeof returnValue.then === "function") { |
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.
Checking promises with returnValue.then === "function" looks fishy/hacky to me from the first loook…
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.
If you are OK removing the special handing for true, the function body could be simplified to just:
return Promise.all(callbacks[messageType].map((callback) => callback(request, sender, sendResponse)));Promise.all already supports empty and non-promise values.
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.
Hmm AFAIK the special handling of true is unfortunately not something I invented, see:
return true from the event listener. This keeps the sendResponse() function valid after the listener returns, so you can call it later. See the sending an asynchronous response using sendResponse example.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage
(Ah already quoted, so this just means I cannot change this/somehow need to stay compatible, I guess.)
| } | ||
|
|
||
| // handle returning | ||
| if (gotTrueAsReturn) { |
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.
Though I agree that this workaround was ugly too… 😅
This is likely base don this MDN advise:
return true from the event listener. This keeps the sendResponse() function valid after the listener returns, so you can call it later. See the sending an asynchronous response using sendResponse example.
Ah and yeah that could be it: the new code totally destroys the special handling of true for async stuff.
AI-generated by Copilot, so I do not fully trust this.
It is based on #6 and it "fixed" this while wanting to fix the return type typing issue here (f6e03a6)