-
Notifications
You must be signed in to change notification settings - Fork 309
[v1.3] GM value 代码相关调整优化 #1125
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
base: release/v1.3
Are you sure you want to change the base?
[v1.3] GM value 代码相关调整优化 #1125
Conversation
cf13c1b to
34c5607
Compare
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 对 GM value(脚本存储)相关代码进行了优化调整,主要目标是将 #950 的部分代码改动拆分出来,仅针对现有逻辑做必要修正,不引入额外机制。
核心变更包括:
- 引入
storageName任务分组机制,使用队列和锁控制并发写入 - 使用
aNow确保updatetime严格递增 - 优化消息传递结构,不再传递完整脚本对象,仅传递必要字段
- 引入
extValueStoreCopy统一各页面的 key 顺序 - 将
valueChangeListener延后到下一个 microTask 执行
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/app/service/service_worker/value.ts |
核心重构:引入任务队列、storageName 分组处理、aNow 时间戳机制 |
src/app/service/service_worker/value.test.ts |
更新测试用例以匹配新的消息结构和批处理行为 |
src/app/service/service_worker/runtime.ts |
更新 valueUpdate 订阅逻辑,使用新的精简事件数据结构 |
src/app/service/service_worker/permission_verify.ts |
完善泛型类型约束,提高类型安全性 |
src/app/service/service_worker/gm_api/gm_api.ts |
修正 GM_setValues 的权限链接配置 |
src/app/service/sandbox/runtime.ts |
重命名 execScripts 为 execScriptMap,更新 valueUpdate 处理逻辑 |
src/app/service/queue.ts |
重构 TScriptValueUpdate 类型,移除完整 Script 对象依赖 |
src/app/service/content/types.ts |
重组类型定义,引入 ValueUpdateSendData 结构 |
src/app/service/content/script_executor.ts |
重命名 execMap 为 execScriptMap,更新 valueUpdate 签名 |
src/app/service/content/inject.ts |
更新类型引用以匹配新的消息结构 |
src/app/service/content/gm_api/gm_api.ts |
重构 valueUpdate 逻辑,引入 extValueStoreCopy,延后 listener 触发 |
src/app/service/content/gm_api/gm_api.test.ts |
修正测试正则表达式,添加缺失的 Message mock |
src/app/service/content/exec_script.ts |
重构 valueUpdate 方法,整合 extValueStoreCopy 机制 |
src/app/service/content/content.ts |
更新变量名引用 |
src/app/repo/scripts.ts |
提取 ValueStore 类型定义,提高代码可维护性 |
| // 如果是预加载脚本,需要更新脚本代码重新注册 | ||
| // scriptMatchInfo 里的 value 改变 => compileInjectionCode -> injectionCode 改变 | ||
| const script = await this.scriptDAO.get(uuid); | ||
| // 因為從 scriptDAO 取了最新的。所以再確認一下吧。 |
Copilot
AI
Jan 8, 2026
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.
注释使用了繁体中文("因為從"、"確認"),而项目中其他注释使用简体中文。建议将注释改为简体中文以保持一致性,例如:"因为从 scriptDAO 取了最新的,所以再确认一下吧。"
| // 因為從 scriptDAO 取了最新的。所以再確認一下吧。 | |
| // 因为从 scriptDAO 取了最新的,所以再确认一下吧。 |
| if (uuid === contextScriptRes.uuid || storageName === getStorageName(contextScriptRes)) { | ||
| contextScriptRes.value = context.extValueStoreCopy || contextScriptRes.value; | ||
| const valueStore = contextScriptRes.value; | ||
| context.valueStoreUpdate(valueStore, responses); | ||
| context.extValueStoreCopy = { ...valueStore }; | ||
| } |
Copilot
AI
Jan 8, 2026
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.
在 valueUpdate 方法中,对 scriptRes 和 contextScriptRes 进行了重复的 uuid 和 storageName 检查。由于外层已经检查了 scriptRes.uuid === uuid || getStorageName(scriptRes) === storageName,而 contextScriptRes 通常是 scriptRes 的引用或相同对象,内层的检查(第 75 行)是冗余的。建议移除内层重复检查,简化逻辑。
| if (uuid === contextScriptRes.uuid || storageName === getStorageName(contextScriptRes)) { | |
| contextScriptRes.value = context.extValueStoreCopy || contextScriptRes.value; | |
| const valueStore = contextScriptRes.value; | |
| context.valueStoreUpdate(valueStore, responses); | |
| context.extValueStoreCopy = { ...valueStore }; | |
| } | |
| contextScriptRes.value = context.extValueStoreCopy || contextScriptRes.value; | |
| const valueStore = contextScriptRes.value; | |
| context.valueStoreUpdate(valueStore, responses); | |
| context.extValueStoreCopy = { ...valueStore }; |
目标
waitForFreshValueState等额外机制或新依赖。变更内容
1) 内部数据传输(page ↔ sw)优化
uuid、storageName相关的数据传递进行整合与归一化,减少重复字段与分散的消息结构。2) valueChangeListener 延后到下一个 microTask
valueChangeListener的处理延后到 下一个 microTask 执行,避免在同步阶段干扰valueUpdate的处理流程与时序。3) 以 storageName 归类 task,并配合 storageName 锁
storageName对任务进行分组,并使用对应的锁控制并发。setValuesByStorageName统一集中处理 同一 storageName 下的 value 变更,减少交错写入导致的竞争与顺序问题。4) “valueUpdate” MQ 事件不再传整段脚本
valueUpdate事件仅发送必要字段:uuid,valueUpdated,status,isEarlyStart不再传递完整脚本内容,以降低消息体积与耦合。
5) 引入 aNow,保证 updatetime 严格递增
aNow,确保updatetime每次更新都 严格递增。updatetime也保证发生变化,避免“时间相同导致排序不稳定”的情况。6) 使用 valueChangeOnSequence 队列,保证 setValues 顺序一致
valueChangeOnSequence队列,确保setValues的执行顺序与 SW 接收顺序一致。await this.scriptDAO.get(uuid);等异步读取导致更新顺序被打乱。7) 使用 extValueStoreCopy,统一各页面的 key 顺序
extValueStoreCopy,使每个页面的valueStore的 键顺序完全一致。