-
Notifications
You must be signed in to change notification settings - Fork 274
修订 chrome.alarms 相关代码 #843
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
Open
Open
Conversation
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
现在每次SW重启都会重新注册alarm, 不太好
然后每次SW重启都会一次过跑所有alarm,会拖慢电脑从休眠回复
现在只做了最简单的处理
@cyfung1031
cyfung1031
force-pushed
the
pr-alarm-3001
branch
2 times, most recently
from
October 15, 2025 13:18
5425a0b
to
3959476
Compare
@cyfung1031
cyfung1031
force-pushed
the
pr-alarm-3001
branch
from
October 18, 2025 04:36
3959476
to
d912f3a
Compare
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.
Uh oh!
There was an error while loading. Please reload this page.
修订
chrome.alarms
相关代码类型:重构 / 行为修正
影响范围:Service Worker 定时任务(周期性 alarm 的创建、触发与补偿)
close #840
背景与目标
chrome.alarms
使用相关的若干不一致与边界状况,确保周期任务能够稳定创建、按时触发、异常可补偿。主要改动
周期性 Alarm 的幂等创建
新增/完善
mightCreatePeriodicAlarm
:get()
产生的runtime.lastError
不阻断创建流程;create()
的lastError
仅记录但不抛错(保持 resolve)。触发回调与补偿机制
新增/完善回调注册与监听:
setPeriodicAlarmCallback
、monitorPeriodicAlarm
。触发参数包含:
alarm
:原始 alarm 对象;triggeredAt
:实际执行时间戳;isFlushed
:是否补偿执行(当scheduledTime
与当前时间相差 ≥ ~65 秒视为补偿)。在回调前后通过
chrome.storage.local
维护AlarmPending:<name>
记录,以支持 SW 重启后的补偿扫描:同一进程内
monitorPeriodicAlarm()
只能启动一次,重复调用将抛错。当触发阶段出现
runtime.lastError
(框架层错误)时,本次事件将被忽略:既不调用回调,也不写入/清理 pending。单元测试(Vitest)
为上述关键路径补齐系统性测试,包括:
lastError
分支(get()
与create()
);runtime.lastError
的忽略策略。测试说明
chrome
Mock(alarms
/storage.local
/runtime.lastError
)精准注入与断言副作用。兼容性与风险评估
lastError
的读取与处理更保守,降低偶发 API 异常带来的连锁失败;monitorPeriodicAlarm()
设计为单例监控,若有特殊架构(多入口重复初始化)需留意只初始化一次。关联问题
变更文件(节选)
src/app/service/service_worker/alarm.ts
(核心逻辑)src/app/service/service_worker/alarm.test.ts
(单测,376+ 行)index.ts
、script.ts
、subscribe.ts
、synchronize.ts
与入口src/service_worker.ts
。统计