Skip to content

Conversation

@CodFrm
Copy link
Member

@CodFrm CodFrm commented Nov 17, 2025

概述 Descriptions

close #972

变更内容 Changes

截图 Screenshots

@CodFrm CodFrm changed the base branch from main to release/v1.3 November 17, 2025 09:42
Copy link
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主要优化了系统配置(SystemConfig)的监听机制,并解决了Service Worker中i18n初始化时序问题。

主要变更:

  • 使用EventEmitter替代MessageQueue实现SystemConfig配置变更监听,提高性能并简化代码
  • 新增Promise机制确保i18n初始化完成后再访问localePath,避免Service Worker中的竞态条件
  • 简化了Setting页面的状态管理代码,移除了复杂的自动刷新逻辑

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/pkg/config/config.ts 引入EventEmitter实现配置监听,新增watch方法支持立即执行回调
src/locales/locales.ts 新增initLocalesPromise确保初始化完成,新增watchLanguageChange函数监听语言变更
src/app/service/service_worker/script.ts 使用watchLanguageChange动态更新脚本安装规则的locale路径
src/app/service/service_worker/runtime.ts 等待initLocalesPromise后再打开文档页面
src/app/service/service_worker/index.ts 使用watch方法简化云同步监听,使用Promise确保locale初始化
src/pages/store/AppContext.tsx 移除SystemConfigChange监听,简化代码
src/pages/options/routes/utils.tsx useSystemConfig hook中添加useEffect监听配置变更
src/pages/options/routes/Setting.tsx 移除手动配置同步逻辑,依赖useSystemConfig自动更新
src/pages/components/layout/Sider.tsx 修正用户指南文档链接路径
src/manifest.json 版本升级至1.3.0.1100
package.json 版本升级至1.3.0-beta

Comment on lines 98 to 107
watch<T extends SystemConfigKey>(key: T, callback: (value: SystemConfigValueType<T>) => void) {
// 立即执行一次
this.get(key).then((val) => {
callback(val);
});
// 监听变更
return this.addListener(key, (val) => {
callback(val);
});
}
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

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

watch方法存在错误处理缺失。如果get(key)的Promise被reject,callback不会被调用,但监听器仍会被注册,导致不一致的状态。建议添加.catch()处理或使用Promise.resolve().then()包裹以确保监听器总是被注册。

Copilot uses AI. Check for mistakes.
return systemConfig.addListener(key, (val) => {
setValue(val);
});
}, [key]);
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

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

useEffect的依赖数组中缺少setValue。虽然setValue是由useState返回的稳定引用,但为了代码的完整性和遵循React Hooks最佳实践,建议将setValue添加到依赖数组中,或使用函数式更新形式避免依赖。

Suggested change
}, [key]);
}, [key, setValue]);

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cyfung1031
Copy link
Collaborator

cyfung1031 commented Nov 17, 2025

不打算借此改一下吗

改做以下格式

systemConfig.watch("XXXX", (previous, current)=>{
...
})

如果是第一次,previous 是 undefined
之后更新就会有 previous

例如

systemConfig.watch("XXXX", async (previous, current) => {
if(previous !== undefined) { ... 移除旧的 }
... 做新的 (需要default也在这里做)
})

@CodFrm CodFrm merged commit c50fcf7 into release/v1.3 Nov 18, 2025
3 checks passed
@CodFrm CodFrm deleted the fix/local-path branch December 6, 2025 02:51
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.

[BUG] localePath 相关调整

3 participants