nutui-react icon indicating copy to clipboard operation
nutui-react copied to clipboard

fix(switch): 异步操作中抛错可中断 loading 态

Open irisSong opened this issue 8 months ago • 3 comments

🤔 这个变动的性质是?

  • [ ] 新特性提交
  • [ ] 日常 bug 修复
  • [ ] 站点、文档改进
  • [ ] 演示代码改进
  • [ ] 组件样式/交互改进
  • [ ] TypeScript 定义更新
  • [ ] 包体积优化
  • [ ] 性能优化
  • [ ] 功能增强
  • [ ] 国际化改进
  • [ ] 重构
  • [ ] 代码风格优化
  • [ ] 测试用例
  • [ ] 分支合并
  • [ ] 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项。⚠️

  • [ ] 文档已补充或无须补充
  • [ ] 代码演示已提供或无须提供
  • [ ] TypeScript 定义已补充或无须补充
  • [ ] fork仓库代码是否为最新避免文件冲突
  • [ ] Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 错误修复

    • 调整了开关组件在执行异步操作时的状态更新流程,确保在出现异常时能够正确恢复,提升了整体稳定性。
  • 重构

    • 优化了开关组件及演示页面中的异步交互逻辑,移除了冗余操作,使切换体验更加顺畅一致。

irisSong avatar Apr 02 '25 09:04 irisSong

Walkthrough

本次变更主要在 Switch 组件及其示例和测试中引入异步操作与错误处理。onClick 函数被改为异步函数,使用 await 调用 onChange 回调,并通过 try-catch 块捕获错误,及时调用 setChanging(false) 重置状态。示例代码中移除了 mockRequest 函数,改为内联 Promise 实现,同时根据异步结果更新状态或抛出异常。测试中则将 onChange 回调由模拟函数替换为直接抛错的箭头函数,从而改变了事件处理逻辑。

Changes

文件路径 变更摘要
src/packages/switch/switch.taro.tsx
src/packages/switch/switch.tsx
将 onClick 改为异步函数,使用 await onChange(!value) 并添加 try-catch 进行错误处理。
src/packages/switch/demos/.../demo2.tsx (h5 和 taro) 移除 mockRequest 函数,内联延时 Promise,依照返回值条件判断并处理异常或更新状态。
src/packages/switch/__test__/switch.spec.tsx 替换原 vi.fn() 模拟 onChange 实现,改为抛出 “Function not implemented.” 错误。

Sequence Diagram(s)

sequenceDiagram
  participant U as 用户
  participant SC as Switch组件
  participant OC as onChange回调
  participant ST as setChanging

  U->>SC: 点击 Switch
  SC->>OC: await onChange(!value)
  alt 执行成功
    SC->>SC: 正常更新状态
  else 捕获异常
    SC->>ST: 调用 setChanging(false)
  end
sequenceDiagram
  participant U as 用户
  participant D as Demo2组件
  participant P as Promise
  participant ST as 状态更新

  U->>D: 触发 onChangeAsync
  D->>P: 创建延时 Promise (2秒)
  P-->>D: 返回 true/false
  alt 返回 true
    D->>ST: 更新组件状态
  else 返回 false
    D->>D: 抛出错误
  end

Possibly related PRs

  • jdf2e/nutui-react#3122: 修改 Switch 组件 onClick 异步行为及错误处理,与当前变更在代码实现上相关。
  • jdf2e/nutui-react#3006: 更新组件 onClick/onChange 异步调用及错误处理机制,代码级改动相似。
  • jdf2e/nutui-react#2950: 调整异步事件处理逻辑,对 onChange 和 beforeChange 函数添加错误检测,联系紧密。

Suggested reviewers

  • oasis-cloud
  • xiaoyatong

Poem

我是小兔,欢跳代码间,
异步之路,步步皆新鲜;
try-catch 保驾护航稳,
状态更新,错误不再烦;
变更之后,开发乐无边!
🐇💻✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

🪧 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 plan to trigger planning for file edits and PR creation.
  • @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.

coderabbitai[bot] avatar Apr 02 '25 09:04 coderabbitai[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 87.20%. Comparing base (abd2516) to head (b041d36). Report is 9 commits behind head on feat_v3.x.

Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #3143      +/-   ##
=============================================
- Coverage      87.20%   87.20%   -0.01%     
=============================================
  Files            279      279              
  Lines          18538    18542       +4     
  Branches        2822     2823       +1     
=============================================
+ Hits           16166    16169       +3     
- Misses          2367     2368       +1     
  Partials           5        5              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Apr 02 '25 09:04 codecov[bot]

对于此PR关联的issue中需要纯异步的场景,switch的onChange事件和switch的状态没有任何关联,而是通过再调用外部弹窗来对状态进行更改场景,只能在onchang事件中直接抛出异常让组件进行识别了。

抛开上面的场景,这个改动还是蛮大的,每个switch的onChange事件只要不是直接使用useState状态的,使用外部接口的都需要判断是否要抛出异常了,否则就可能无法再次点击了

Ryan-CW-Code avatar Apr 04 '25 11:04 Ryan-CW-Code

不如参考antd、tdesign、vant等组件库,把loading交给用户来进行控制。 根据onchang的返回值或者onchang抛出错误从数据流向来说感觉不是很对劲呀

反正都是解决方案,希望尽快决定下来,推一个版本。🙂

Ryan-CW-Code avatar Apr 07 '25 05:04 Ryan-CW-Code

@oasis-cloud @dushoujun @irisSong 周五了,这周的发版也还修改这个方案吗? 您们可以讨论决定一下,用那种方案。 确定方案了,如果没时间的话我可以根据您们觉得的方案提个PR

Ryan-CW-Code avatar Apr 11 '25 01:04 Ryan-CW-Code

@oasis-cloud @dushoujun @irisSong 周五了,这周的发版也还修改这个方案吗? 您们可以讨论决定一下,用那种方案。 确定方案了,如果没时间的话我可以根据您们觉得的方案提个PR

可以提个 PR,来看看你实际想要的是什么样子的组件。代码理解意图最直观。

oasis-cloud avatar Apr 11 '25 03:04 oasis-cloud

@oasis-cloud #3170 用的antdesign控制的方案

Ryan-CW-Code avatar Apr 11 '25 06:04 Ryan-CW-Code