-
Notifications
You must be signed in to change notification settings - Fork 299
[v1.3] 異步 getValue/getValues/listValues 相关修改 #950
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] 異步 getValue/getValues/listValues 相关修改 #950
Conversation
|
怎么都到这个分支去了 develop/raw-message |
因为两边的 commit 互相影响 |
b6d77de to
31a4165
Compare
31a4165 to
dc9f387
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 实现了异步 getValue/getValues/listValues 相关的重要改进,主要解决了在多标签页并发场景下值读取的一致性问题。通过引入 waitForFreshValueState 机制和批处理架构,确保在读取值之前能够获得最新的状态。
主要变更:
- 新增
waitForFreshValueState方法,确保在读取前获取最新的 value 状态 - 重构
setValues方法为批处理架构,使用任务队列和setValuesByStorageName进行批量处理 - 修改
GM.getValue/GM.listValues/GM.getValues以在读取前等待最新状态
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/app/service/service_worker/value.ts | 核心变更:新增 waitForFreshValueState 和 setValuesByStorageName,重构 value 更新逻辑为批处理模式,引入 ValueUpdateTaskInfo 任务队列 |
| src/app/service/service_worker/value.test.ts | 更新测试以反映新的批处理行为,调整期望值以匹配新的数据结构,添加 flush() 调用处理异步逻辑 |
| src/app/service/service_worker/runtime.ts | 更新 valueUpdate 订阅以使用新的 TScriptValueUpdate 类型,添加脚本状态重新验证逻辑 |
| src/app/service/service_worker/permission_verify.ts | 更新泛型约束从 T 到 T extends Array<any> 以匹配 API 参数类型 |
| src/app/service/service_worker/gm_api/gm_api.ts | 新增 internalApiWaitForFreshValueState API 方法,修正权限链接配置 |
| src/app/service/sandbox/runtime.ts | 更新 valueUpdate 方法以处理新的 ValueUpdateSendData 数据结构 |
| src/app/service/queue.ts | 修改 TScriptValueUpdate 类型定义,从包含 Script 对象改为包含 uuid、status 和 isEarlyStart 字段 |
| src/app/service/content/types.ts | 在 ValueUpdateDataEncoded 中添加 updatetime 字段,新增 ValueUpdateSendData 类型 |
| src/app/service/content/script_executor.ts | 更新 valueUpdate 方法签名以适配新的数据结构 |
| src/app/service/content/inject.ts | 更新类型引用从 ValueUpdateDataEncoded 到 ValueUpdateSendData |
| src/app/service/content/gm_api/gm_api.ts | 实现 waitForFreshValueState 静态方法,更新 GM.getValue/GM.listValues/GM.getValues 以调用该方法,重构 valueUpdate 处理多个更新事件,引入 extValueStoreCopy 和 readFreshes 机制 |
| src/app/service/content/gm_api/gm_api.test.ts | 添加 valueDaoUpdatetimeFix 辅助函数,更新测试以处理新的 waitForFreshValueState 行为,修正正则表达式以匹配计数器格式 |
| src/app/service/content/exec_script.ts | 更新 valueUpdate 方法签名以传递 storageName、uuid 和数据列表 |
| uuid, | ||
| storageName, | ||
| sender: valueSender, | ||
| valueUpdated, |
Copilot
AI
Nov 28, 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.
在 ValueUpdateDataEncoded 对象中包含了 valueUpdated 字段(第257行),但根据 types.ts 中的类型定义(第26-33行),ValueUpdateDataEncoded 类型不包含 valueUpdated 字段,只有 id、entries、uuid、storageName、sender 和 updatetime。这会导致类型不匹配。建议删除第257行的 valueUpdated。
| valueUpdated, |
| const { id, uuid, entries, storageName, sender, valueUpdated } = data; | ||
| if (uuid === scriptRes.uuid || storageName === getStorageName(scriptRes)) { | ||
| let lastUpdateTime = 0; | ||
| if (uuid == scriptRes.uuid || storageName === getStorageName(scriptRes)) { |
Copilot
AI
Nov 28, 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.
应使用严格相等运算符 === 而不是 ==,以避免类型强制转换可能导致的意外行为。应改为 if (uuid === scriptRes.uuid || ...)
| if (uuid == scriptRes.uuid || storageName === getStorageName(scriptRes)) { | |
| if (uuid === scriptRes.uuid || storageName === getStorageName(scriptRes)) { |
| const cacheKey = `${CACHE_KEY_SET_VALUE}${storageName}`; | ||
| const ret = await stackAsyncTask<number | undefined>(cacheKey, async () => { | ||
| const valueModel: Value | undefined = await this.valueDAO.get(storageName); | ||
| // await this.valueDAO.save(storageName, valueModel); |
Copilot
AI
Nov 28, 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.
此处的注释代码 // await this.valueDAO.save(storageName, valueModel); 应该被删除。注释掉的代码会降低代码可读性,如果不需要这行代码,应该完全删除它。
| // await this.valueDAO.save(storageName, valueModel); |
概述 Descriptions
依存: #949
取值前确保读取的是最新 values
否则向service_worker发消息,取得最新的 valueUpdated
变更内容 Changes
截图 Screenshots
测试代码(一):
修改后:
GM.listValues()能在冲突中取得最新,而且不会因本地缓存与valueUpdate冲突而造成次序不一(
useAsync改为false的话就能看 GM_xxxx 的结果 )测试代码(二):
(有
GM_lock做时间控制)修改后GM.getValue的列表新增没问题