[DRAFT] ♻️ GM value 更新流程重构:storageName 分组批处理、顺序一致与 updatetime 严格递增 (#1125 重设计)#1627
Draft
cyfung1031 wants to merge 1 commit into
Draft
[DRAFT] ♻️ GM value 更新流程重构:storageName 分组批处理、顺序一致与 updatetime 严格递增 (#1125 重设计)#1627cyfung1031 wants to merge 1 commit into
cyfung1031 wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
背景
按 #1125 的想法在最新 main 上重新设计实现(#1125 基于旧架构、已关闭;其部分内容如
GM_deleteValuelink 修正、verify<T>泛型、aNow工具已在 main 落地,不再重复)。目前 main 的 GM value 流程仍存在:setValues在进入 per-storage 队列前先await scriptDAO.get(uuid),多个并发调用的入队次序取决于该异步读取的耗时,最终写入顺序可能与调用顺序不一致。setValues都各自valueDAO.get+save+ 推送一次,高频写入时浪费明显。updatetime不更新且不保证递增:现有实现只在 value 模型创建时写入updatetime,后续变更从不更新;同一时间片内的连续更新也无法区分先后。valueStore的 key 顺序可能不一致:本地乐观写入与 SW 下发更新的到达顺序不同,导致GM_listValues等遍历顺序在不同页面间发散(优先度:低)。本次改动
对脚本用户可见的行为不变(GM value API 语义不变),内部流程调整:
ValueService.setValues两级队列化(src/app/service/service_worker/value.ts):valueChangeOnSequence顺序队列保证任务入队次序与调用次序一致;storageName分组累积,由setValuesByStorageName在 per-storage 队列(沿用CACHE_KEY_SET_VALUE锁)中集中处理:一次valueDAO.get、按序应用全部任务、至多一次save、一次推送。updatetime用aNow()写入:值实际变更时更新updatetime,同一时间片内亦严格递增;创建路径保留原有ts回填语义。src/app/service/content/types.ts):新增ValueUpdateSendData = { storageName, storageChanges: Record<uuid, ValueUpdateDataEncoded[]> };ValueUpdateDataEncoded的entries更名valueChanges、移除可由valueChanges.length推导的valueUpdated字段。RuntimeService.pushValueUpdate改收updatedScripts: Script[](本批实际变更的脚本,用于 early-start 重注册)+ 一份ValueUpdateSendData。scripting.ts/script_runtime.ts/script_executor.ts/ sandboxruntime.ts按新结构分发;匹配逻辑(uuid / storageName)收敛到ExecScript.valueUpdate。sandbox 侧 crontab 脚本的 value 同步顺带修正了undefined应删除 key 而非写入undefined的问题。gm_api.ts):valueUpdate改为valueStoreUpdate(valueStore, responses),同步更新 store;valueChangeListener回调延后到下一个 microTask 执行;extValueStoreCopy:以 SW 确认顺序维护 store 快照,SW 更新到达时以快照为基准应用,使各页面 key 顺序一致;generateValChangeId()消除_GM_setValue/_GM_setValues的重复计数器逻辑。TScriptValueUpdate(旧valueUpdateMQ 事件在 storage.session 广播改造时已移除)与未被引用的ValueUpdateData/ValueUpdateDataEntry类型。实现考虑
setValuesByStorageName在任何await之前同步取走并删除该 storageName 的整批任务表,之后入队的任务会建立新列表并由其自己的队列调用处理,不存在丢任务窗口。GM.setValue等依赖带id的回执解除等待,因此即使本批无实际变更也照常推送(valueChanges为空数组)。updatedScripts按 uuid 去重,仅对本批有实际变更、启用中且为 early-start 的脚本执行updateResourceOnScriptChange,语义与原实现一致。extValueStoreCopy的取舍:本地乐观写入不进快照;未被 SW 确认的本地写入在下一次 SW 更新到达时会暂时回退、待确认后恢复 —— 以此换取跨页面 key 顺序一致(与 [Dev] GM value 代码相关调整优化 - 包括 确保 script.value key 顺序跨页一致(优先度:低) (PR-TAG: TBC) #1125 设计一致,优先度低,可单独讨论)。建议审查重点
setValuesByStorageName的批处理边界:批内多 uuid(共享 storageName)、isReplace、新建 value 模型 +ts回填路径。extValueStoreCopy的乐观写入回退窗口是否可接受。关联
验证
pnpm test:309 文件 / 3436 测试全部通过。pnpm lint:prettier + tsc + check:i18n + eslint 全部通过。value.test.ts(新增批处理合并、顺序一致、updatetime 严格递增、错误不卡队列等用例,7 例先失败)再实现;gm_api.test.ts/create_context.test.ts适配新签名并补充监听器 microTask 延后断言。🤖 Generated with Claude Code