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

Wait for the tiles to be loaded to display the map #341

Merged
merged 1 commit into from
Jan 7, 2025
Merged

Conversation

vicb
Copy link
Owner

@vicb vicb commented Jan 7, 2025

Summary by Sourcery

Enhancements:

  • Wait for the map tiles to load before setting the API loading state to false, improving the user experience by preventing the map from briefly appearing without tiles.

Summary by CodeRabbit

  • Bug Fixes

    • Improved loading state management for Google Maps
    • Enhanced event listener cleanup to prevent potential memory leaks
  • Performance

    • Optimized map component initialization and resource management
  • New Features

    • Updated TypeScript configuration to utilize ECMAScript 2022 features.

Copy link
Contributor

sourcery-ai bot commented Jan 7, 2025

Reviewer's Guide by Sourcery

This PR changes when the API loading state is set to false. Previously, it was set to false immediately after the map was initialized. Now, it waits for the map tiles to be loaded before setting the API loading state to false.

Sequence diagram for map loading and tile initialization

sequenceDiagram
    participant MapElement
    participant GoogleMaps
    participant Store

    MapElement->>GoogleMaps: Initialize map
    Note over MapElement,GoogleMaps: Previously set loading=false here
    GoogleMaps-->>MapElement: Map initialized
    MapElement->>GoogleMaps: Add tilesloaded listener
    GoogleMaps-->>MapElement: Tiles loaded
    MapElement->>Store: dispatch(setApiLoading(false))
    Note over MapElement,Store: Now waits for tiles to load
Loading

State diagram for map loading states

stateDiagram-v2
    [*] --> Loading: Initialize map
    Loading --> WaitingForTiles: Map initialized
    WaitingForTiles --> Ready: Tiles loaded
    Ready --> [*]: Set loading=false
Loading

File-Level Changes

Change Details Files
Wait for map tiles to load before setting API loading state to false
  • Added an event listener to the map that listens for the tilesloaded event.
  • Dispatched the setApiLoading(false) action inside the tilesloaded event listener.
  • Cleared instance listeners on the map when the component is disconnected to prevent memory leaks and unexpected behavior from stale listeners.
apps/fxc-front/src/app/components/2d/map-element.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

coderabbitai bot commented Jan 7, 2025

Walkthrough

The pull request introduces modifications to the MapElement class in the map component, focusing on improving the loading state management and event listener handling for Google Maps. The changes optimize the connectedCallback method to dispatch the loading state action only after map tiles are fully loaded, and enhance the disconnectedCallback method to properly clean up map-related event listeners. Additionally, the TypeScript configuration is updated to target ECMAScript 2022, reflecting newer language features.

Changes

File Change Summary
apps/fxc-front/src/app/components/2d/map-element.ts - Updated connectedCallback to dispatch setApiLoading(false) after tilesloaded event
- Added listener cleanup in disconnectedCallback to prevent memory leaks
apps/fxc-front/tsconfig.json - Changed TypeScript target from "ES2020" to "ES2022"
- Updated lib from ["ES2020", "DOM", "WebWorker"] to ["ES2022", "DOM", "WebWorker"]

Poem

🗺️ Mapping Rabbit's Delight 🐰

Tiles loading, listeners clear,
Maps dancing with code so dear,
Events bound, then gently freed,
A rabbit's map with careful heed!
Performance leaps, memory light! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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 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 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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

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
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @vicb - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

apps/fxc-front/src/app/components/2d/map-element.ts Outdated Show resolved Hide resolved
Copy link

cloudflare-workers-and-pages bot commented Jan 7, 2025

Deploying flyxc with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9d62788
Status: ✅  Deploy successful!
Preview URL: https://42727110.flyxc.pages.dev
Branch Preview URL: https://vb-branch-1.flyxc.pages.dev

View logs

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

🧹 Nitpick comments (1)
apps/fxc-front/src/app/components/2d/map-element.ts (1)

242-244: Consider reordering cleanup operations.

While the cleanup of map listeners is good, consider reordering the operations to ensure a more predictable cleanup sequence.

 disconnectedCallback(): void {
   super.disconnectedCallback();
+  if (this.map) {
+    google.maps.event.clearInstanceListeners(this.map);
+  }
   this.subscriptions.forEach((sub) => sub());
   this.subscriptions.length = 0;
-  if (this.map) {
-    google.maps.event.clearInstanceListeners(this.map);
-  }
   this.map = undefined;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 16ec95f and 6c7d383.

📒 Files selected for processing (1)
  • apps/fxc-front/src/app/components/2d/map-element.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: build (20.x)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (1)
apps/fxc-front/src/app/components/2d/map-element.ts (1)

232-234: LGTM! Improved loading state management.

The use of tilesloaded event ensures a better user experience by showing the map only when it's actually ready to be displayed.

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

🧹 Nitpick comments (2)
apps/fxc-front/src/app/components/2d/map-element.ts (2)

232-240: Good implementation of tile loading with timeout!

The implementation properly handles tile loading with a timeout, which prevents the app from being stuck in a loading state if tiles fail to load. However, consider extracting the timeout duration to a constant for better maintainability.

+const TILE_LOADING_TIMEOUT_MS = 4000;
+
 // Wait for the tiles to be loaded (or timeout) to hide the splashscreen.
 let splashResolve: () => void;
 const splashPromise = new Promise<void>((r) => (splashResolve = r));
-const to = setTimeout(() => splashResolve(), 4000);
+const to = setTimeout(() => splashResolve(), TILE_LOADING_TIMEOUT_MS);
 google.maps.event.addListenerOnce(this.map, 'tilesloaded', () => {
   splashResolve();
   clearTimeout(to);
 });
🧰 Tools
🪛 Biome (1.9.4)

[error] 234-234: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)


233-234: Improve Promise initialization pattern.

The current implementation assigns the resolver in the Promise executor, which is flagged by static analysis. Consider using a cleaner pattern.

-let splashResolve: () => void;
-const splashPromise = new Promise<void>((r) => (splashResolve = r));
+let splashResolve!: () => void;
+const splashPromise = new Promise<void>((resolve) => {
+  splashResolve = resolve;
+});
🧰 Tools
🪛 Biome (1.9.4)

[error] 234-234: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7d383 and 9d62788.

📒 Files selected for processing (2)
  • apps/fxc-front/src/app/components/2d/map-element.ts (1 hunks)
  • apps/fxc-front/tsconfig.json (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
apps/fxc-front/src/app/components/2d/map-element.ts

[error] 234-234: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build (20.x)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (2)
apps/fxc-front/tsconfig.json (1)

5-5: LGTM! Verify compatibility with dependencies.

The update to ES2022 brings useful features that can enhance the codebase. However, ensure all dependencies support ES2022 features to prevent runtime issues.

Run this script to check package compatibility:

Also applies to: 8-8

✅ Verification successful

ES2022 compatibility verified across dependencies

No significant compatibility issues found that would prevent the use of ES2022. Most dependencies are modern and well-maintained, with good support for recent ECMAScript features.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check package.json dependencies for ES2022 compatibility issues
# Look for any reported compatibility issues in package.json

# Find and check package.json files
fd package.json --exec jq -r '.dependencies + .devDependencies | keys[]' {} | sort -u | while read pkg; do
  # Search for ES2022 compatibility issues in dependencies
  gh api graphql -f query="
    query {
      search(query:\"$pkg ES2022 compatibility issue\", type:ISSUE, first:5) {
        nodes {
          ... on Issue {
            title
            url
            repository { nameWithOwner }
            labels(first:5) { nodes { name } }
          }
        }
      }
    }
  "
done

Length of output: 64003

apps/fxc-front/src/app/components/2d/map-element.ts (1)

248-250: LGTM! Good cleanup of map event listeners.

Proper cleanup of instance listeners in disconnectedCallback prevents memory leaks and ensures clean component unmounting.

@vicb vicb merged commit 14c48c8 into master Jan 7, 2025
6 checks passed
@vicb vicb deleted the vb-branch-1 branch January 7, 2025 07:43
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.

1 participant