-
-
Notifications
You must be signed in to change notification settings - Fork 315
fix: update appreciation data properly #839
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: dev
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @HardyNLee, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决鉴赏数据管理中的两个关键问题。首先,它确保了背景图片(CG)和背景音乐(BGM)的解锁鉴赏数据能够立即在游戏中生效,而不再需要手动刷新。其次,它修正了当重复解锁相同 URL 的鉴赏内容时,其名称未能正确更新的逻辑错误,从而提高了数据的一致性和用户体验。 Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
你好,感谢你的贡献。本次 PR 修复了鉴赏数据更新的两个问题,目标明确,修改有效。代码整体不错,但我发现几个可以改进的地方:1. 在 bgm.ts 和 changeBg/index.ts 中存在重复的逻辑,并且对异步操作缺少错误处理。2. 在 userDataReducer.ts 中,可以使用 Array.find() 来优化数组的查找和更新逻辑,使代码更简洁高效。具体的修改建议请参考我提交的评论。
| if (name !== '') { | ||
| webgalStore.dispatch(unlockBgmInUserData({ name, url, series })); | ||
| const userDataState = webgalStore.getState().userData; | ||
| localforage.setItem(WebGAL.gameKey, userDataState).then(() => {}); |
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.
这部分用于持久化用户数据的逻辑与 packages/webgal/src/Core/gameScripts/changeBg/index.ts 中的代码重复了。为了提高代码的可维护性,建议将此逻辑提取到一个共享的辅助函数中。
另外,localforage.setItem 是一个异步操作,目前的代码 .then(() => {}) 会忽略任何可能发生的错误。建议添加错误处理逻辑,以便在数据保存失败时能够捕获并记录错误,这对于调试非常有帮助。
| localforage.setItem(WebGAL.gameKey, userDataState).then(() => {}); | |
| localforage.setItem(WebGAL.gameKey, userDataState).catch(err => console.error('Failed to save user data:', err)); |
| if (unlockName !== '') { | ||
| dispatch(unlockCgInUserData({ name: unlockName, url, series })); | ||
| const userDataState = webgalStore.getState().userData; | ||
| localforage.setItem(WebGAL.gameKey, userDataState).then(() => {}); |
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.
这部分用于持久化用户数据的逻辑与 packages/webgal/src/Core/gameScripts/bgm.ts 中的代码重复了。为了提高代码的可维护性,建议将此逻辑提取到一个共享的辅助函数中。
另外,localforage.setItem 是一个异步操作,目前的代码 .then(() => {}) 会忽略任何可能发生的错误。建议添加错误处理逻辑,以便在数据保存失败时能够捕获并记录错误,这对于调试非常有帮助。
| localforage.setItem(WebGAL.gameKey, userDataState).then(() => {}); | |
| localforage.setItem(WebGAL.gameKey, userDataState).catch(err => console.error('Failed to save user data:', err)); |
| state.appreciationData.cg.forEach((e) => { | ||
| if (url === e.url) { | ||
| isExist = true; | ||
| e.url = url; | ||
| e.name = name; | ||
| e.series = series; | ||
| } | ||
| }); |
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.
使用 forEach 遍历数组来查找并更新单个元素,在找到元素后循环不会停止,效率不高。当数组很大时可能会有性能影响。
建议重构此逻辑,使用 Array.prototype.find() 来查找元素。这样代码更简洁,而且一旦找到匹配项就会停止遍历,效率更高。
例如,可以这样重构整个 unlockCgInUserData reducer:
unlockCgInUserData: (state, action: PayloadAction<IAppreciationAsset>) => {
const { name, url, series } = action.payload;
const existingCg = state.appreciationData.cg.find((e) => e.url === url);
if (existingCg) {
existingCg.name = name;
existingCg.series = series;
} else {
state.appreciationData.cg.push(action.payload);
}
},由于这会改变函数整体结构,因此不提供可直接应用的修改建议,但强烈建议进行此重构。
| state.appreciationData.bgm.forEach((e) => { | ||
| if (url === e.url) { | ||
| isExist = true; | ||
| e.url = url; | ||
| e.name = name; | ||
| e.series = series; | ||
| } | ||
| }); |
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.
与 unlockCgInUserData 中的逻辑类似,这里也建议使用 Array.prototype.find() 来代替 forEach,以提高代码的可读性和效率。
重构示例:
const { name, url, series } = action.payload;
const existingBgm = state.appreciationData.bgm.find((e) => e.url === url);
if (existingBgm) {
existingBgm.name = name;
existingBgm.series = series;
} else {
state.appreciationData.bgm.push(action.payload);
}
介绍