Conversation
There was a problem hiding this comment.
Pull Request Overview
此PR重构了favicon的获取和缓存机制,将相关逻辑从前端页面迁移到service worker,并增加了持久化存储支持。主要改进了favicon管理的架构和性能。
- 将favicon获取逻辑从
src/pkg/utils/favicon.ts迁移到service worker层 - 新增
FaviconDAO用于数据持久化存储 - 使用OPFS(Origin Private File System)存储favicon文件,提升性能
- 移除了不再使用的chrome.runtime.onMessage监听器和相关工具类型
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/service_worker.ts | 移除了已废弃的chrome.runtime.onMessage监听器和相关导入 |
| src/pkg/utils/favicon.ts | 删除文件,功能迁移至service worker层 |
| src/pages/store/utils.ts | 更新favicon加载逻辑,调用新的SystemClient API |
| src/app/service/service_worker/utils.ts | 移除不再使用的TMsgResponse类型和msgResponse函数 |
| src/app/service/service_worker/system.ts | 新增getScriptFavicon和loadFavicon方法,实现OPFS缓存 |
| src/app/service/service_worker/script.ts | 移除未使用的isEarlyStartScript导入 |
| src/app/service/service_worker/index.ts | 注入FaviconDAO到SystemService |
| src/app/service/service_worker/fetch.ts | 删除文件,功能迁移至favicon.ts |
| src/app/service/service_worker/favicon.ts | 新增文件,包含域名提取和favicon获取逻辑 |
| src/app/service/service_worker/favicon.test.ts | 新增单元测试 |
| src/app/service/service_worker/client.ts | 更新SystemClient的方法签名 |
| src/app/repo/favicon.ts | 新增FaviconDAO用于favicon数据持久化 |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
应该在安装时进行读取(储存到OPFS) |
cyfung1031
left a comment
There was a problem hiding this comment.
可以不用等待
要等待。因为不等待的话 try-catch 不能抓 Promise的报错
所以我经常强调用 async 写法必须加 await
favicons 不应直接存在 root
应该存在 opfsRoot/cached_favicons
因为有需要时,直接整个 cached_favicons 删掉重建就可行
| const opfsRoot = await navigator.storage.getDirectory(); | ||
| opfsRoot.removeEntry(`favicons:${uuid}`, { recursive: true }); |
There was a problem hiding this comment.
| const opfsRoot = await navigator.storage.getDirectory(); | |
| opfsRoot.removeEntry(`favicons:${uuid}`, { recursive: true }); | |
| await this.removeFaviconFolder(uuid); |
There was a problem hiding this comment.
但是也不处理错误,要忽略掉错误,不等待,不try-catch,就不加await了
5ee46af to
7be3098
Compare
| await this.faviconDAO.delete(uuid); | ||
| // 删除opfs缓存 | ||
| try { | ||
| await removeFaviconFolder(uuid); |
There was a problem hiding this comment.
正如我之前所说,不需要处理错误,直接就不用加await了,当然你这样也可以
我觉得现在这样也没问题 |
| if (domain.domain) { | ||
| const icons = await fetchIconByDomain(domain.domain); | ||
| const icon = icons.length > 0 ? icons[0] : ""; | ||
| return { match: domain.match, website: "http://" + domain.domain, icon }; |
There was a problem hiding this comment.
[nitpick] 对所有域名统一使用 http:// 协议可能不合适。建议使用 https:// 作为默认协议,因为大多数现代网站都支持 HTTPS,这也与第 147 行 fetchIconByDomain 函数中使用 https:// 保持一致。
| return { match: domain.match, website: "http://" + domain.domain, icon }; | |
| return { match: domain.match, website: "https://" + domain.domain, icon }; |
| }); | ||
| } | ||
| await cacheInstance.set<boolean>("vscodeReconnect", true); | ||
| } |
There was a problem hiding this comment.
将同步代码改为异步执行可能导致初始化逻辑不按预期执行。原代码使用 await 确保 vscode 重连逻辑在 init() 完成前执行,现在改为 Promise chain 后失去了这种保证。如果这是有意为之(避免阻塞初始化),应该添加错误处理 .catch() 来捕获可能的异常,防止未处理的 Promise rejection。
| } | |
| } | |
| }).catch((err) => { | |
| // 捕获并处理异常,防止未处理的 Promise rejection | |
| console.error("[SystemService] vscode 自动连接异常:", err); |
| type: script.type, | ||
| isEarlyStart: isEarlyStartScript(script.metadata), | ||
| })); | ||
| })) as TDeleteScript[]; |
There was a problem hiding this comment.
[nitpick] 使用类型断言 as TDeleteScript[] 来解决类型问题不是最佳实践。根据 src/app/service/queue.ts 的定义,TDeleteScript 类型包含 { uuid: string; storageName: string; type: SCRIPT_TYPE }。移除了 isEarlyStart 字段后应该正好匹配类型定义,但如果仍需要类型断言,建议检查 map 函数返回的对象是否真的符合 TDeleteScript 的所有必需字段。
有看过代码。现在可以。 |
* ♻️ 重构优化脚本图标加载 * 增加单元测试 * 整理代码 * Update src/app/service/service_worker/system.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * 调整代码 (scriptscat#896) * 根据copilot修改 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: cyfung1031 <44498510+cyfung1031@users.noreply.github.com>
概述 Descriptions
close #887 #726
变更内容 Changes
重构优化脚本图标加载,现在图标使用opfs存储
截图 Screenshots