-
Notifications
You must be signed in to change notification settings - Fork 22
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
Start using webext-messenger
(for background functions and messages)
#1258
Conversation
Shorten whoAmI ID
webext-messenger
webext-messenger
(for background functions and messages)
return sender; | ||
} | ||
|
||
export async function notifyReady(): Promise<void> { |
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.
I moved the contents of this function to the only place where it was called, to avoid too much redirection. This readiness part can likely be further simplified over time
if (sender == null) { | ||
// If you see this error, it means the wrong message handler responded to the message. | ||
// The most likely cause of this is that background listener function was accidentally marked | ||
// with the "async" keyword as that prevents the method from returning "undefined" to indicate | ||
// that it did not handle the message | ||
throw new ConnectionError( | ||
`Internal error: received null response for ${MESSAGE_CONTENT_SCRIPT_ECHO_SENDER}. Check use of async in message listeners` | ||
); | ||
} |
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 applies to every single message and it's now being automatically checked by webext-messenger itself:
|
||
declare global { | ||
interface MessengerMethods { | ||
CONTAINS_PERMISSIONS: typeof browser.permissions.contains; |
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 define a literal of what you passed to registerMethods, shouldn't you be able to have typescript generate the type from the literal instead of having to retype it all?
Updated: I wrote a comment in slack after I looked over the library code
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.
That's exactly what we're trying to avoid in #669
If you specify the object:
const handlers = {
UNARY: unary
}
registerMethods(handlers);
With the intent of using the type in the caller context, webpack will definitely include the unary
function in both contexts.
payload: {}, | ||
}); | ||
export async function whoAmI( | ||
this: MessengerMeta |
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.
I'm really not a fan of "overloading" this
to mean the messenger. this
has a special meaning in OO languages
Thoughts on Re-reading the code, it looks like they're self contained within the handlers, so if we don't need the serialization they should be safe to drop if we don't think serialization is necessary |
The sendMessage API indeed already serializes errors but:
So I think in order to maintain stacktraces of errors that happened in the background I'll have to implement the error serialization now |
} | ||
|
||
// Chrome offers this API in more contexts than Firefox, so it skips the messenger entirely | ||
export const containsPermissions = browser.permissions |
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.
I'm not sure there's much benefit in having different code paths for the browser here
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.
Sure, I just preserved the existing logic:
pixiebrix-extension/src/utils/permissions.ts
Lines 54 to 58 in 5d181f7
if (browser.permissions) { | |
return browser.permissions.contains(permissions); | |
} | |
return containsPermissionsInBackground(permissions); |
However I think it's still worth doing because:
- it's a simple API wrap
- Chrome users (the majority) win in perf
- we still need a comment to explain why we're using a background call
- it reminds us we will probably be able to drop this background call at some point
Related:
NB: By necessity and by design, this pattern is more verbose than the
lift
pattern since we intentionally keep the implementation separate from the caller. More context in:This PR:
sendMessage
callsuninstallContextMenu
andactivateTab
, which I'm not sure how to trigger exactly since tab is already activeFuture work: