-
Notifications
You must be signed in to change notification settings - Fork 310
优化 systemconfig 和处理 SW 中的i18n问题 #976
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
Conversation
There was a problem hiding this 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 |
src/pkg/config/config.ts
Outdated
| 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); | ||
| }); | ||
| } |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
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()包裹以确保监听器总是被注册。
| return systemConfig.addListener(key, (val) => { | ||
| setValue(val); | ||
| }); | ||
| }, [key]); |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
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添加到依赖数组中,或使用函数式更新形式避免依赖。
| }, [key]); | |
| }, [key, setValue]); |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
不打算借此改一下吗 改做以下格式 如果是第一次,previous 是 undefined 例如 |
概述 Descriptions
close #972
变更内容 Changes
截图 Screenshots