Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Fixes to message routing in the extension #158

Merged
merged 3 commits into from
Aug 23, 2024
Merged

Fixes to message routing in the extension #158

merged 3 commits into from
Aug 23, 2024

Conversation

jonathankap
Copy link
Contributor

@jonathankap jonathankap commented Aug 23, 2024

These fixes make sure that messages and responses go from iframe -> service worker -> content script and all the way back.

I learned that you can't use async methods in chrome message listeners: as soon as you await something, the method returns and no response is sent. Instead the message listener needs to return true (which tells chrome the response will be async) and run the async code separately.

Summary by CodeRabbit

  • New Features

    • Improved message handling in the Chrome extension for better reliability and response timing.
    • Enhanced responses for "NEW_EDIT" messages to include details about modifications made.
  • Bug Fixes

    • Ensured responses are sent back even when no active tab is found, improving user feedback.
  • Refactor

    • Restructured message handling logic to use asynchronous patterns, enhancing performance and maintainability.

@jonathankap jonathankap requested review from elg0nz and fitzk August 23, 2024 19:23
Copy link

coderabbitai bot commented Aug 23, 2024

Walkthrough

Walkthrough

The changes involve restructuring the asynchronous message handling logic in a Chrome extension, focusing on encapsulating operations within async functions. The modifications enhance response timing and robustness, particularly for "UNAUTHORIZED" and "NEW_EDIT" messages. The response now includes detailed modification data for better feedback. The imports have also been updated to accommodate new data types. Overall, the updates aim for clearer and more reliable message processing.

Changes

File Path Change Summary
apps/mocksi-lite-next/src/pages/background/index.ts Restructured async message handling; improved response timing for "UNAUTHORIZED" messages; ensured responses are sent even without active tabs.
apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx Updated async message handling with IIFE; included modification data in response for "NEW_EDIT" messages; modified imports to include new data types.

Sequence Diagram(s)

sequenceDiagram
    participant Sender
    participant Background
    participant Content

    Sender->>Background: Send message (e.g., "UNAUTHORIZED")
    Background->>Background: Process message asynchronously
    Background-->>Sender: Send response after processing

    Sender->>Content: Send message (e.g., "NEW_EDIT")
    Content->>Content: Process changes asynchronously
    Content-->>Sender: Send modification data in response
Loading

Poem

🐇 In a world of code, where rabbits code,
Asynchronous changes lighten the load.
Messages dance in a swift ballet,
Modifications bloom, brightening the day!
With each new line, a hop and a cheer,
Progress leaps forward, the future is near! 🌼


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 858c670 and 2c3e7b4.

Files selected for processing (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts (1 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (2 hunks)
Additional comments not posted (3)
apps/mocksi-lite-next/src/pages/background/index.ts (1)

77-122: Ensure correct async handling in message listener.

The restructuring of the asynchronous logic within the chrome.runtime.onMessageExternal.addListener is well-implemented. Wrapping the logic in an async function and returning true ensures that Chrome waits for the async response. This is crucial for handling asynchronous operations correctly in Chrome extensions.

apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (2)

2-2: Import AppliedModifications correctly.

The addition of AppliedModifications in the import statement suggests that modifications are being tracked. Ensure that this import is used correctly in the code.


65-126: Async handling in message listener is well-structured.

The use of an async IIFE within the message listener allows for proper handling of asynchronous operations. The inclusion of modification data in the response provides detailed feedback, which is a good enhancement.

Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

Looks solid, and while normally I would say "let's not use nested ifs", in code that uses async methods they are a necessity because it's quite easy to break the control flow by calling other functions.

:shipit:

Copy link
Contributor

@fitzk fitzk left a comment

Choose a reason for hiding this comment

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

lgtm

@elg0nz elg0nz merged commit dbb85c9 into main Aug 23, 2024
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants