Skip to content
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

Webviews: Two-way messaging #46

Merged
merged 7 commits into from
Oct 27, 2023
Merged

Conversation

khawkins
Copy link
Collaborator

In order to support the logic of the forthcoming landing page selection and LWC QA generation, we needed a two-way channel between the webviews and their backing TypeScript controller pages. This does that. I took out the previous one-way button event messaging, and replaced it with a more general two-way protocol.

This is feature complete, but needs tests. But I wanted to get the concept in front of you all for any feedback you wish to offer.

@@ -1,19 +1,29 @@
<!DOCTYPE html>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't give this page too much scrutiny, as it's going away imminently. I just updated for completeness sake.

@@ -24,7 +24,7 @@ export class AuthorizeCommand {
if (!result || result.title === l10n.t('No')) {
return Promise.resolve(false);
} else {
await commands.executeCommand('sfdx.force.auth.web.login');
await commands.executeCommand('sfdx.org.login.web');
Copy link
Collaborator Author

@khawkins khawkins Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually broken in the current code in main. 😔 Apparently the command names moved on and we didn't know it.

src/webviews.ts Outdated
delete data.callbackId;
callback = (responseData?: object) => {
const fullResponseMessage = {
callbackId: returnedCallbackId,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic glue!

@dbreese
Copy link
Collaborator

dbreese commented Oct 19, 2023

Approach looks good to me!

Copy link
Collaborator

@sfdctaka sfdctaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@khawkins khawkins marked this pull request as ready for review October 25, 2023 20:12
@khawkins khawkins requested a review from a team as a code owner October 25, 2023 20:12
@khawkins
Copy link
Collaborator Author

Got good coverage on the webview helper class I pulled out (WebviewProcessor), and left testing of InstructionsWebviewProvider alone, because what's left there doesn't really do much but hand off to WebviewProcessor.

Still need to look at how I can test the JS side of the messaging, but that's for another day.

(messageHandler) => data.type === messageHandler.type
);
if (responsiveHandlers.length > 0) {
const handler = responsiveHandlers[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Refactor into a new file(processor.ts) hides the assertion that why index 0 handler is a valid handler. Since the validation of handler map takes place at all the caller(showInstructionWebview) maybe onWebviewReceivedMessage should be passed a single message handler instead of an array there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch! In my next PR I'm actually moving the handler validation into the processor as well, for the reason you stated and the reason that it's really more congruent there (another aspect you pointed out).

While I like having the array just passed in here for its own processing, I do agree that the selection of just the first item feels a little oblique. I could either add a comment here, or what I'm leaning toward is just switching to messageHandlers.find() instead, which only returns the first item found (which is always going to be 0 or 1 items), and probably reads better for this use case. Let me know what you think.

@khawkins khawkins merged commit 93e6fb3 into salesforce:main Oct 27, 2023
9 checks passed
@khawkins khawkins deleted the two_way_messaging branch October 27, 2023 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants