-
Notifications
You must be signed in to change notification settings - Fork 298
处理 check_script_update_cycle
#906
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
处理 check_script_update_cycle
#906
Conversation
5e12458 to
d7f90be
Compare
|
| import { SystemConfig } from "@App/pkg/config/config"; | |
| import { systemConfig } from "@App/pages/store/global"; |
| const systemConfig = new SystemConfig(this.mq); |
src/service_worker.ts
这个 messageQueue 是不是不用建立?
在ServiceWorkerManager里(src/app/service/service_worker/index.ts)直接取 "@App/pages/store/global" 里的?
scriptcat/src/service_worker.ts
Lines 76 to 77 in 0627a0f
| const messageQueue = new MessageQueue(); | |
| const manager = new ServiceWorkerManager(server, messageQueue, new ServiceWorkerMessageSend()); |
|
@cyfung1031
应该要删除 |
d6c952f 修好了 |
8184c05 to
8434b1f
Compare
6276829 to
e2aae39
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 重构了脚本定时更新检查(check_script_update_cycle)的实现方式,通过引入新的 regular_updatecheck.ts 模块来统一管理 chrome.alarm 的创建和触发逻辑。
主要变更:
- 创建了新的
regular_updatecheck.ts模块,实现了更智能的 alarm 管理逻辑,包括基于上次检查时间的计算、防止 Service Worker 启动时立即触发检查等功能 - 将原本分散在
script.ts和subscribe.ts中的两个独立 alarm(checkScriptUpdate和checkSubscribeUpdate)合并为一个统一的checkScriptUpdatealarm - 更新了所有语言的翻译文件,将
check_frequency重命名为script_update_check_frequency以提高清晰度
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/app/service/service_worker/regular_updatecheck.ts | 新增模块,实现统一的定时更新检查逻辑,包括 alarm 管理和更新时间戳跟踪 |
| src/app/service/service_worker/script.ts | 移除了原有的 alarm 创建代码,改为调用 initRegularUpdateCheck |
| src/app/service/service_worker/subscribe.ts | 重构 checkSubscribeUpdate 方法,将其改为接受参数而非内部获取配置;移除了独立的 checkSubscribeUpdate alarm |
| src/app/service/service_worker/index.ts | 重构了 alarm 触发逻辑,使用新的 onRegularUpdateCheckAlarm 函数;重构了扩展更新检查函数 |
| src/pages/options/routes/Setting.tsx | 在用户更改更新频率设置时添加了对 initRegularUpdateCheck 的调用,并更新了翻译 key |
| src/locales/*/translation.json | 将翻译 key 从 check_frequency 更新为 script_update_check_frequency 以提高描述准确性 |
| chrome.alarms.create( | ||
| "checkScriptUpdate", | ||
| { | ||
| when, | ||
| periodInMinutes: targetPeriodInMinutes, | ||
| }, | ||
| () => { | ||
| const lastError = chrome.runtime.lastError; | ||
| if (lastError) { | ||
| console.error("chrome.runtime.lastError in chrome.alarms.create:", lastError); | ||
| // Starting in Chrome 117, the number of active alarms is limited to 500. Once this limit is reached, chrome.alarms.create() will fail. | ||
| console.error("Chrome alarm is unable to create. Please check whether limit is reached."); | ||
| } | ||
| } | ||
| ); |
Copilot
AI
Nov 15, 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.
在 initRegularUpdateCheck 函数中创建新的 alarm 之前,应该先清除旧的 alarm,以避免创建重复的 alarm。建议在 chrome.alarms.create 之前添加 chrome.alarms.clear("checkScriptUpdate")。
示例:
chrome.alarms.clear("checkScriptUpdate");
chrome.alarms.create(
"checkScriptUpdate",
{
when,
periodInMinutes: targetPeriodInMinutes,
},
// ...
);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.
官方文档写明了 chrome.alarms.create 会把旧的清除
https://developer.chrome.com/docs/extensions/reference/api/alarms?hl=en#method-create
Creates an alarm. Near the time(s) specified by alarmInfo, the onAlarm event is fired. If there is another alarm with the same name (or no name if none is specified), it will be cancelled and replaced by this alarm.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms/create#name
Alarm names are unique within the scope of a single extension. If an alarm with an identical name exists, the existing alarm will be cleared and the alarm being created will replace it.
| Promise.resolve().then(() => { | ||
| initRegularUpdateCheck(systemConfig); | ||
| }); |
Copilot
AI
Nov 15, 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.
在选项页面(Options Page)上下文中直接调用 initRegularUpdateCheck 存在问题。该函数修改模块级变量 allowRegularUpdateCheck,但在选项页面上下文中修改的变量不会影响到 Service Worker 上下文中的同名变量。这可能导致 Service Worker 中的 onRegularUpdateCheckAlarm 函数无法正确工作,因为它依赖 allowRegularUpdateCheck 的值来决定是否执行更新检查。
建议通过消息传递机制来触发 Service Worker 重新初始化定时检查,例如:
// 在 Service Worker 中添加消息处理
this.group.on("reinitRegularUpdateCheck", async () => {
await initRegularUpdateCheck(this.systemConfig);
});
// 在选项页面中发送消息
scriptClient.reinitRegularUpdateCheck();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.
没有影响。已测试过
在 扩展页面可以呼叫 initRegularUpdateCheck
alarm 呼叫回传会在 service_worker 里进行
allowRegularUpdateCheck 的值是以现在时间判断不是以 时间间隔 来判断
所以 service_worker 的 allowRegularUpdateCheck 不用更新
| if (next > now + 30000) { | ||
| when = next; | ||
| } | ||
| } | ||
| when = when || now + 6000; // 六秒后触发第一个alarm | ||
| let targetPeriodInMinutes = Math.ceil(updateCycleSecond / 60); // 分钟 | ||
| targetPeriodInMinutes = Math.ceil(targetPeriodInMinutes / 5) * 5; // 5的倍数 | ||
| if (targetPeriodInMinutes < 15) targetPeriodInMinutes = 15; // 至少15的分钟 | ||
| chrome.alarms.create( | ||
| "checkScriptUpdate", | ||
| { | ||
| when, | ||
| periodInMinutes: targetPeriodInMinutes, | ||
| }, | ||
| () => { | ||
| const lastError = chrome.runtime.lastError; | ||
| if (lastError) { | ||
| console.error("chrome.runtime.lastError in chrome.alarms.create:", lastError); | ||
| // Starting in Chrome 117, the number of active alarms is limited to 500. Once this limit is reached, chrome.alarms.create() will fail. | ||
| console.error("Chrome alarm is unable to create. Please check whether limit is reached."); | ||
| } | ||
| } | ||
| ); | ||
| allowRegularUpdateCheck = now + 3000; // 可以触发alarm的更新程序了 |
Copilot
AI
Nov 15, 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.
第 21 行和第 25 行使用了魔法数字 30000(30 秒)和 6000(6 秒)。这些值应该定义为常量并添加注释说明其用途。
建议在文件顶部定义常量:
// 如果距离下次检查还有超过30秒,则使用计算的时间;否则使用默认延迟
const MIN_REMAINING_TIME_MS = 30000;
// Service Worker 启动后的默认延迟时间,给予足够的初始化时间
const DEFAULT_FIRST_CHECK_DELAY_MS = 6000;
// 允许在预定时间前最多65秒内触发检查(考虑 alarm 触发时间的不精确性)
const ALARM_TRIGGER_WINDOW_MS = 65000;
// Service Worker 启动后允许执行 alarm 的延迟时间
const ALLOW_CHECK_DELAY_MS = 3000;然后在代码中使用这些常量。
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.
Copilot 的 注释没问题。跟随了 26f7e09
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@CodFrm Copilot 指出了我把 |
概述 Descriptions
close #796 #840
变更内容 Changes
用新角度处理 chrome.alarm 相关设计。 简单直接解决问题
截图 Screenshots