Skip to content

fix: recs setEntities now updates component if component exists #407

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

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

MartianGreed
Copy link
Collaborator

@MartianGreed MartianGreed commented Feb 27, 2025

Closes #

Introduced changes

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Add a dedicated CI job for new examples
  • Performed self-review of the code

Summary by CodeRabbit

  • Chores

    • Updated underlying dependencies with the latest patch updates to enhance overall system stability.
  • Bug Fixes

    • Improved the component update process to ensure that existing components are properly refreshed instead of being inadvertently overwritten, leading to more consistent and reliable system behavior.
  • New Features

    • Introduced new methods for enhanced component management: getComponentValue, hasComponent, and updateComponent.

Copy link

coderabbitai bot commented Feb 27, 2025

Walkthrough

This pull request adds a new changeset file documenting patch updates for various Dojo Engine packages and updates the component management logic in the setEntities function. The code now conditionally updates components—using newly imported helper functions (getComponentValue, hasComponent, and updateComponent)—if they exist, instead of always calling setComponent. Minor refactoring such as replacing let with const and additional logging statements have been introduced. No changes to exported or public entity declarations were made.

Changes

File(s) Change Summary
.changeset/chatty-kings-nail.md Added a new file listing dependency patch updates and a note regarding a fix in the component update logic within the system.
packages/state/.../index.ts Modified setEntities: now checks for component existence using hasComponent; if present, calls updateComponent with the current value from getComponentValue, and uses const declarations with added logging.

Sequence Diagram(s)

sequenceDiagram
    participant E as Entity
    participant S as setEntities
    participant H as hasComponent
    participant G as getComponentValue
    participant U as updateComponent
    participant C as setComponent
    participant L as Logger

    E->>S: Invoke setEntities(entities)
    loop For each entity and componentName
        S->>H: Check if component exists
        alt Component exists
            S->>G: Get current component value
            S->>U: Update component with current value
            S->>L: Log update details
        else Component does not exist
            S->>C: Set component for entity
        end
    end
Loading

Possibly related PRs

Poem

I’m a bunny coder with a hop so spry,
Watching components update as they fly by.
With checks and logs, my code’s now neat,
Each component updated with a rhythmic beat.
Hop, hop—let the changes shine bright! 🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f003e04 and d8a9b0d.

📒 Files selected for processing (2)
  • .changeset/chatty-kings-nail.md (1 hunks)
  • packages/state/src/recs/index.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • .changeset/chatty-kings-nail.md
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: check
  • GitHub Check: build
🔇 Additional comments (3)
packages/state/src/recs/index.ts (3)

5-7: LGTM: Imports added for component management functions.

The added imports for getComponentValue, hasComponent, and updateComponent support the new component update logic.

Also applies to: 10-10


411-411: LGTM: Improved variable declarations.

Good use of const instead of let for loop variables, which helps prevent accidental reassignment.

Also applies to: 416-416, 420-422


451-463: LGTM: Now correctly updates existing components.

This implementation properly checks if a component exists and updates it instead of replacing it. The continue statement correctly skips to the next iteration rather than exiting the function completely, which fixes the issue mentioned in the previous review.

The logic now:

  1. Checks if the component exists for the entity
  2. If it exists, updates it with the new values while preserving current state
  3. Adds informative logging
  4. Continues to the next component rather than attempting to set it again

This directly addresses the PR objective of ensuring that "setEntities now updates component if component exists."

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
  • @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

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e92e46 and f003e04.

📒 Files selected for processing (2)
  • .changeset/chatty-kings-nail.md (1 hunks)
  • packages/state/src/recs/index.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build
  • GitHub Check: check
🔇 Additional comments (3)
.changeset/chatty-kings-nail.md (1)

1-17: LGTM! Clear and concise changeset entry.

The changeset correctly documents that this is a patch update across multiple packages, with a clear description of the primary fix: "setEntities now updates component if component exists". This aligns with the code changes in the implementation file.

packages/state/src/recs/index.ts (2)

5-10: Good addition of required imports.

You've correctly imported the necessary functions (getComponentValue, hasComponent, and updateComponent) from the recs package that are used in the updated implementation of setEntities.


411-416: Improved variable declaration using const.

Using const instead of let for loop variables is a good practice when the variables aren't being reassigned within the loop body.

Comment on lines 451 to 463
if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Update component ${recsComponent.metadata?.name} on ${key}`
);
return;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix early return issue in component update logic.

The implementation correctly checks if a component exists and updates it instead of replacing it, which aligns with the PR's purpose. However, there's a critical issue: the return statement on line 462 will exit the entire function after processing only the first component that needs an update, preventing other components and entities from being processed.

Apply this diff to fix the issue:

if (hasComponent(recsComponent, key as Entity)) {
    updateComponent(
        recsComponent,
        key as Entity,
        convertedValue as Partial<ComponentValue>,
        getComponentValue(recsComponent, key as Entity)
    );
    if (logging)
        console.log(
            `Update component ${recsComponent.metadata?.name} on ${key}`
        );
-    return;
+    continue;
}

The continue statement will skip to the next iteration rather than exiting the function completely.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Update component ${recsComponent.metadata?.name} on ${key}`
);
return;
}
if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Update component ${recsComponent.metadata?.name} on ${key}`
);
continue;
}

@MartianGreed MartianGreed merged commit 5fb5071 into main Feb 27, 2025
6 checks passed
@MartianGreed MartianGreed deleted the fix/recs-state-update branch February 27, 2025 16:30
@coderabbitai coderabbitai bot mentioned this pull request May 8, 2025
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