Skip to content

Conversation

@flang
Copy link

@flang flang commented Aug 18, 2025

Close #20

Summary by CodeRabbit

  • Bug Fixes
    • Prevented rare runtime errors in the Markdown editor by making the theme observer cleanup more robust, improving stability during theme changes and when the editor is removed.

@coderabbitai
Copy link

coderabbitai bot commented Aug 18, 2025

Walkthrough

Replaces a direct property access in unobserveThemeChange with optional chaining to safely check for themeChangeObserver on markDownEditor.$connector before disconnecting and clearing it.

Changes

Cohort / File(s) Change Summary
Frontend connector null-guard
src/main/resources/META-INF/resources/frontend/fcMarkdownEditorConnector.js
Updated condition to use optional chaining (markDownEditor?.$connector?.themeChangeObserver) to avoid runtime errors when markDownEditor or $connector is undefined; logic for disconnecting and clearing observer unchanged.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent null/undefined access of $connector in unobserveThemeChange to avoid console error [#20]

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch issue-20

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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>, please review it.
    • 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.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

🔭 Outside diff range comments (2)
src/main/resources/META-INF/resources/frontend/fcMarkdownEditorConnector.js (2)

24-31: Prevent re-initialization lockout after unobserve

observeThemeChange returns early if $connector exists, but unobserveThemeChange does not remove $connector, only nulls themeChangeObserver. On a re-attach for the same element instance, observeThemeChange will bail out and never recreate the observer. This silently disables theme observation after the first detach/attach cycle.

Refactor the guard to only skip when an active observer exists, and ensure $connector is initialized when missing.

Apply this diff:

-      // Check whether the connector was already initialized for markDownEditor
-      if (markDownEditor.$connector) {
-        return;
-      }
-
-      markDownEditor.$connector = {};
+      // Initialize connector namespace if missing
+      if (!markDownEditor.$connector) {
+        markDownEditor.$connector = {};
+      }
+      // Skip only if an observer is already active
+      if (markDownEditor.$connector.themeChangeObserver) {
+        return;
+      }

74-80: Tear down connector state to allow clean re-observe

When unobserving, also clear the themeChangeObserver property (and $connector if empty) so a subsequent observeThemeChange can re-initialize correctly.

Apply this diff:

-      if (markDownEditor?.$connector?.themeChangeObserver) {
-        markDownEditor.$connector.themeChangeObserver.disconnect();
-        markDownEditor.$connector.themeChangeObserver = null;
-      }
+      if (markDownEditor?.$connector?.themeChangeObserver) {
+        markDownEditor.$connector.themeChangeObserver.disconnect();
+        // Remove the observer reference to allow re-initialization later
+        delete markDownEditor.$connector.themeChangeObserver;
+        // If no other connector state is stored, clean up the namespace too
+        if (Object.keys(markDownEditor.$connector).length === 0) {
+          delete markDownEditor.$connector;
+        }
+      }
🧹 Nitpick comments (2)
src/main/resources/META-INF/resources/frontend/fcMarkdownEditorConnector.js (2)

51-53: Scope the MutationObserver to the ‘theme’ attribute only

Narrowing the observer to attributeFilter ['theme'] reduces noise and avoids callbacks for irrelevant attribute changes.

Apply this diff:

-      // options for the observer (which mutations to observe)
-      const config = { attributes: true };
+      // options for the observer (only watch the 'theme' attribute)
+      const config = { attributes: true, attributeFilter: ['theme'] };

Also applies to: 69-73


59-61: Remove debug logging

console.log in the hot path will spam the console on every theme change. Remove or downgrade to console.debug if needed.

Apply this diff:

-            console.log("theme", themeName);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9400316 and 2e3d13e.

📒 Files selected for processing (1)
  • src/main/resources/META-INF/resources/frontend/fcMarkdownEditorConnector.js (1 hunks)
🔇 Additional comments (1)
src/main/resources/META-INF/resources/frontend/fcMarkdownEditorConnector.js (1)

76-79: Null-safe disconnect via optional chaining looks good

Using optional chaining prevents the “Cannot read properties of null (reading '$connector')” error on detach. This directly addresses Issue #20 without changing behavior otherwise.

@flang flang requested a review from paodb August 18, 2025 19:21
@paodb paodb merged commit 98e0d91 into master Aug 18, 2025
3 checks passed
@paodb paodb deleted the issue-20 branch August 18, 2025 19:25
@github-project-automation github-project-automation bot moved this from To Do to Pending release in Flowing Code Addons Aug 18, 2025
@paodb paodb moved this from Pending release to Done in Flowing Code Addons Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

Cannot read properties of null (reading '$connector') on unobserveThemeChange call

4 participants