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

fix: state & globalState getter #930

Merged

Conversation

chilingling
Copy link
Member

@chilingling chilingling commented Dec 7, 2024

  1. 修复 state getter、setter 无法保存、修改的bug
  2. 修复 globalState getter 在画布计算不正确的 bug

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

【问题描述】

  1. 状态管理 -> 页面状态 -> 编辑 setter 或者 getter -> 保存
    问题:编辑之后的 setter 或者 getter 没有保存成功
    预期: setter 或者 getter 保存成功

  2. 状态管理 -> 应用状态 -> 添加计算的 getter -> 保存 -> 绑定到Text 组件的文本属性测试显示

问题:画布中文本属性显示了函数原声明
预期:显示函数调用之后计算出来的值

【问题分析】

  1. 重构版本交互逻辑有改变,但是 getFormData 没有进行更新适配
  2. 重构版本使用 parseData 函数进行解析,但是没有发起调用执行

【解决方案】

  1. 进行交互逻辑的适配更新
  2. 发起调用执行

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced form handling in the variable creation process with improved checks for getter and setter functions.
    • Refined validation for variable names, ensuring compliance with naming conventions and uniqueness.
    • Introduced a new reactive property to track the presence of accessor functions.
  • Bug Fixes

    • Addressed issues with undefined getter and setter functions, ensuring proper default values are returned.
  • Documentation

    • Updated error messages for variable name validation to provide clearer guidance to users.

1. 修复 state getter、setter 无法保存、修改的bug
2. 修复 globalState getter 在画布计算不正确的 bug
Copy link
Contributor

coderabbitai bot commented Dec 7, 2024

Walkthrough

The pull request introduces modifications to two primary files: RenderMain.js and CreateVariable.vue. In RenderMain.js, the watchEffect function is updated to change how computedGetters are constructed, affecting state access within the reactive system. In CreateVariable.vue, the getFormData method is enhanced with new checks for getter and setter functions, and the validateName function is refined to improve variable name validation. Minor adjustments in comments and formatting are also included, but they do not alter the overall functionality.

Changes

File Path Change Summary
packages/canvas/render/src/RenderMain.js Modified watchEffect function to change parseData invocation, affecting state access within getters.
packages/plugins/state/src/CreateVariable.vue Updated getFormData method to include checks for getter/setter functions and refined validateName for errors.

Possibly related PRs

Suggested labels

ready_for_review

Suggested reviewers

  • hexqi

🐰 In the code where we play,
New checks and balances come to stay.
Getters and setters, now aligned,
With clearer names, our code refined.
Hopping through logic, smooth and bright,
Our changes make the code feel right! 🌟


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

@github-actions github-actions bot added bug Something isn't working refactor-main refactor/develop branch feature labels Dec 7, 2024
@chilingling chilingling added the ready_for_review This PR requires more reviews label Dec 7, 2024
Copy link
Contributor

@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 and nitpick comments (2)
packages/plugins/state/src/CreateVariable.vue (2)

260-273: Improve accessor object creation logic

The changes improve the handling of getter/setter functions, but the accessor object creation could be optimized further.

Consider this more concise implementation:

      const defaultValue = getDefaultValue()
      const getter = getterEditor.value.getEditor().getValue()
      const setter = setterEditor.value.getEditor().getValue()
      if (!getter && !setter) return defaultValue

-      const result = { defaultValue }
-
-      if (getter && getter !== DEFAULT_GETTER) {
-        result.accessor = { ...result.accessor, getter: { type: 'JSFunction', value: getter } }
-      }
-
-      if (setter && setter !== DEFAULT_SETTER) {
-        result.accessor = { ...result.accessor, setter: { type: 'JSFunction', value: setter } }
-      }
-
-      // 没有设置 getter setter,需要直接返回 defaultValue
-      if (!result.accessor) {
-        return defaultValue
-      }
-
-      return result
+      const accessor = {}
+      if (getter && getter !== DEFAULT_GETTER) {
+        accessor.getter = { type: 'JSFunction', value: getter }
+      }
+      if (setter && setter !== DEFAULT_SETTER) {
+        accessor.setter = { type: 'JSFunction', value: setter }
+      }
+      return Object.keys(accessor).length ? { defaultValue, accessor } : defaultValue

This implementation:

  1. Creates the accessor object only when needed
  2. Avoids unnecessary spread operations
  3. Simplifies the return logic

Line range hint 277-292: Consider using local error state in validation

The current implementation uses shared state for error messages, which could lead to race conditions if multiple validations occur simultaneously.

Consider this approach:

    const validateName = (rule, name, callback) => {
-     state.errorMessage = ''
+     let errorMessage = ''

      if (!name) {
-       state.errorMessage = 'state 属性名称未定义'
+       errorMessage = 'state 属性名称未定义'
      } else if (!verifyJsVarName(name)) {
-       state.errorMessage = ' state 属性名称只能以字母或下划线开头且仅包含数字字母及下划线'
+       errorMessage = ' state 属性名称只能以字母或下划线开头且仅包含数字字母及下划线'
      } else if (
        Object.keys(props.dataSource).includes(name) &&
        (props.flag !== 'update' || name !== props.updateKey)
      ) {
-       state.errorMessage = '已存在同名 state 属性'
+       errorMessage = '已存在同名 state 属性'
      }

-     state.errorMessage ? callback(new Error(state.errorMessage)) : callback()
+     state.errorMessage = errorMessage
+     errorMessage ? callback(new Error(errorMessage)) : callback()
    }

This change:

  1. Uses local state for error handling
  2. Updates shared state only after validation
  3. Reduces the risk of race conditions
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 038914b and 0a9ffdd.

📒 Files selected for processing (1)
  • packages/plugins/state/src/CreateVariable.vue (1 hunks)
🔇 Additional comments (1)
packages/plugins/state/src/CreateVariable.vue (1)

Line range hint 260-292: Changes effectively address the state management issues

The modifications successfully improve the state getter/setter functionality by:

  1. Properly handling accessor objects in getFormData
  2. Ensuring valid state names through enhanced validation
  3. Maintaining backward compatibility

The implementation aligns well with the PR objectives of fixing state getter/setter saving issues.

@hexqi hexqi merged commit d45eb4b into opentiny:refactor/develop Dec 10, 2024
2 checks passed
This was referenced Dec 11, 2024
@coderabbitai coderabbitai bot mentioned this pull request Dec 24, 2024
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready_for_review This PR requires more reviews refactor-main refactor/develop branch feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants