This allows to make use of the sendReply/sendResponse callbacks allowing for the callee to reply directly without having to manually track state and relate a message to a reply.
Also add a bit of documentation on the required callback signature.
Details
- Reviewers
fvogt - Group Reviewers
Plasma - Commits
- R856:59fa1853817b: Support message response and reply callbacks
Sent a message with a callback, replied, got reply in callback.
Returned true from a callback and stored the callback in a variable, did some processing, replied successfully at a later point.
Diff Detail
- Repository
- R856 Plasma Browser Integration
- Lint
Automatic diff as part of commit; lint not applicable. - Unit
Automatic diff as part of commit; unit tests not applicable.
browser.runtime.sendMessage does not seem to have a callback parameter.
See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage:
if two arguments are given:
the arguments are interpreted as (message, options), and the message is sent internally, if the second argument is any of the following:
a valid options object (meaning, it is an object which contains only the properties of options that the browser supports)
null
undefined
otherwise, the arguments are interpreted as (extensionId, message). The message will be sent to the extension identified by extensionId.
It works just fine in Firefox 67 here. Maybe the documentation is outdated?
browser.runtime.sendMessage({foo: "bar"}, callback)
works just fine. Also, I find posts about people using that in Firefox as early as 2017, so this is not a recent addition.
Ok, I have an idea.
browser.runtime.sendMessage (Firefox) returns a promise that is fulfilled when a reply is sent. Chrome instead uses a callback function in chrome.runtime.sendMessage
I'm gonna rework the sendMessage() to always return a Promise (makes for nicer code) by either forwarding the promise from Firefox or creating one forwarding the callback in Chrome
Code looks good and seems to work fine in vivaldi, but I get an error in firefox:
Promise resolved after context unloaded extension.js:78 sendSettings moz-extension://d1f34b4e-4e97-40a5-b4ac-03dc2fea30a9/extension.js:78
appears to be harmless though?