Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

目标


变更内容

1) 内部数据传输(page ↔ sw)优化

  • 针对 uuidstorageName 相关的数据传递进行整合与归一化,减少重复字段与分散的消息结构。

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键顺序完全一致
  • 否则不同页面可能因本地更新与 SW 下发更新的到达顺序不同,导致各自 key 顺序不一致,进而影响执行/遍历时的稳定性。

@cyfung1031 cyfung1031 changed the title GM value 代码相关调整优化 [v1.3] GM value 代码相关调整优化 Jan 2, 2026
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 对 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 取了最新的。所以再確認一下吧。
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

注释使用了繁体中文("因為從"、"確認"),而项目中其他注释使用简体中文。建议将注释改为简体中文以保持一致性,例如:"因为从 scriptDAO 取了最新的,所以再确认一下吧。"

Suggested change
// 因為從 scriptDAO 取了最新的。所以再確認一下吧
// 因为从 scriptDAO 取了最新的,所以再确认一下吧

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +80
if (uuid === contextScriptRes.uuid || storageName === getStorageName(contextScriptRes)) {
contextScriptRes.value = context.extValueStoreCopy || contextScriptRes.value;
const valueStore = contextScriptRes.value;
context.valueStoreUpdate(valueStore, responses);
context.extValueStoreCopy = { ...valueStore };
}
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

valueUpdate 方法中,对 scriptRescontextScriptRes 进行了重复的 uuid 和 storageName 检查。由于外层已经检查了 scriptRes.uuid === uuid || getStorageName(scriptRes) === storageName,而 contextScriptRes 通常是 scriptRes 的引用或相同对象,内层的检查(第 75 行)是冗余的。建议移除内层重复检查,简化逻辑。

Suggested change
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 };

Copilot uses AI. Check for mistakes.
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.

1 participant