Skip to content

fix: 修正 src/pages/install/App.tsx closeWindow 代码#1435

Open
cyfung1031 wants to merge 3 commits into
mainfrom
fix/installPage/closeWindow-code
Open

fix: 修正 src/pages/install/App.tsx closeWindow 代码#1435
cyfung1031 wants to merge 3 commits into
mainfrom
fix/installPage/closeWindow-code

Conversation

@cyfung1031
Copy link
Copy Markdown
Collaborator

fix: 修正 src/pages/install/App.tsx closeWindow 代码

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

该 PR 旨在修正安装页(src/pages/install/App.tsx)在“安装后关闭/手动关闭”场景下可能重复触发关窗逻辑的问题,通过引入关窗中的标志位来避免重复执行 closeWindow()

Changes:

  • 新增 closingWindow 标志,防止 handleInstall / handleClose 在短时间内重复触发关窗。
  • useEffect 中重置关窗标志,避免状态残留影响后续流程。
  • handleClose 调整为 async,并在“关闭且不再更新”时 await 更新配置后再关窗。

Comment thread src/pages/install/App.tsx
Comment on lines +52 to 55
let closingWindow = false;
const closeWindow = (doBackwards: boolean) => {
if (doBackwards) {
history.go(-1);
Comment thread src/pages/install/App.tsx
Comment on lines +518 to +523
const handleClose = async (options?: { noMoreUpdates: boolean }) => {
if (closingWindow) return;
const { noMoreUpdates = false } = options || {};
if (noMoreUpdates && scriptInfo && !scriptInfo.userSubscribe) {
scriptClient.setCheckUpdateUrl(scriptInfo.uuid, false);
await scriptClient.setCheckUpdateUrl(scriptInfo.uuid, false);
}
Copy link
Copy Markdown
Member

@CodFrm CodFrm left a comment

Choose a reason for hiding this comment

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

看了一下改动,方向对,有 1 个实质修复,但还有几处建议收敛。

真正有价值的改动

  • await scriptClient.setCheckUpdateUrl(App.tsx:519)这是这次唯一实质性的修复。原代码 fire-and-forget,窗口可能在写入 storage 完成前就关掉,"不再检查更新" 偶发丢失。👍

建议优化

1. 50ms setTimeout 没有依据(App.tsx:521-525)
既然已经 await 了,没必要再延迟 50ms 关窗。看上去是模仿 handleInstall 的 500ms,但那里 500ms 是给用户看 success Message 的;这里没有 Message,建议直接同步调用 closeWindow(doBackwards)

2. 双重检查冗余

if (closingWindow) return;        // 入口已守卫
// ...
if (!closingWindow) {             // 永远成立,死代码
  closingWindow = true;
  setTimeout(...);
}

handleInstall(App.tsx:505)和 handleClose(App.tsx:524)都有这个模式,内层 if 可以去掉。

3. await 抛错没处理(App.tsx:519)
原来失败也会关窗。现在如果 setCheckUpdateUrl reject(通道异常等),窗口不会关,且 closingWindow 已置 true,后续点击也无效,用户卡死。建议 try/catchfinally 中触发关闭。

4. 模块级可变状态(App.tsx:52)
let closingWindow = false; 写在模块顶层不太地道。安装页是单例所以无实际问题,但更 React 的写法是 useRef<boolean>,生命周期和组件绑定,不需要在 effect 里手动重置。

5. eslint-disable react-hooks/exhaustive-deps 没加注释(App.tsx:319)
依赖没动,仅是绕开 lint。建议在注释里说明原因,否则下次有人改这块会困惑。

6. useEffect 重置时机(App.tsx:318)
searchParams / loaded 变化会重置 closingWindow。极端情况下关闭流程中 URL 参数变更,会让 50ms 内可以再次触发关闭。当前流程下不容易触发,但耦合不稳。

测试

没有新增测试。建议至少加一个 fake timer 单测覆盖关闭节流和 await setCheckUpdateUrl 的时序。

小结

  • 保留:await setCheckUpdateUrl
  • 去掉:50ms setTimeout、内层冗余判断
  • 补:异常路径的 close 兜底、useRef 替代模块级 let、eslint-disable 加注释

双击节流本身意义不大(window.close() / history.go(-1) 实质幂等),但既然加了就把它写干净。整体改一下应该就可以合了。

@cyfung1031
Copy link
Copy Markdown
Collaborator Author

不改
故意加的 50ms 防止未操作完就关闭
其他代码也是没问题

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.

3 participants