Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/webgal/src/Core/gameScripts/bgm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { playBgm } from '@/Core/controller/stage/playBgm';
import { getNumberArgByKey, getStringArgByKey } from '@/Core/util/getSentenceArg';
import { webgalStore } from '@/store/store';
import { unlockBgmInUserData } from '@/store/userDataReducer';
import localforage from 'localforage';
import { WebGAL } from '../WebGAL';

/**
* 播放一段bgm
Expand All @@ -20,6 +22,8 @@ export const bgm = (sentence: ISentence): IPerform => {

if (name !== '') {
webgalStore.dispatch(unlockBgmInUserData({ name, url, series }));
const userDataState = webgalStore.getState().userData;
localforage.setItem(WebGAL.gameKey, userDataState).then(() => {});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这部分用于持久化用户数据的逻辑与 packages/webgal/src/Core/gameScripts/changeBg/index.ts 中的代码重复了。为了提高代码的可维护性,建议将此逻辑提取到一个共享的辅助函数中。

另外,localforage.setItem 是一个异步操作,目前的代码 .then(() => {}) 会忽略任何可能发生的错误。建议添加错误处理逻辑,以便在数据保存失败时能够捕获并记录错误,这对于调试非常有帮助。

Suggested change
localforage.setItem(WebGAL.gameKey, userDataState).then(() => {});
localforage.setItem(WebGAL.gameKey, userDataState).catch(err => console.error('Failed to save user data:', err));

}

playBgm(url, enter, volume);
Expand Down
3 changes: 3 additions & 0 deletions packages/webgal/src/Core/gameScripts/changeBg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import cloneDeep from 'lodash/cloneDeep';
import { getAnimateDuration } from '@/Core/Modules/animationFunctions';
import { WebGAL } from '@/Core/WebGAL';
import { DEFAULT_BG_OUT_DURATION } from '@/Core/constants';
import localforage from 'localforage';

/**
* 进行背景图片的切换
Expand All @@ -34,6 +35,8 @@ export const changeBg = (sentence: ISentence): IPerform => {
const dispatch = webgalStore.dispatch;
if (unlockName !== '') {
dispatch(unlockCgInUserData({ name: unlockName, url, series }));
const userDataState = webgalStore.getState().userData;
localforage.setItem(WebGAL.gameKey, userDataState).then(() => {});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这部分用于持久化用户数据的逻辑与 packages/webgal/src/Core/gameScripts/bgm.ts 中的代码重复了。为了提高代码的可维护性,建议将此逻辑提取到一个共享的辅助函数中。

另外,localforage.setItem 是一个异步操作,目前的代码 .then(() => {}) 会忽略任何可能发生的错误。建议添加错误处理逻辑,以便在数据保存失败时能够捕获并记录错误,这对于调试非常有帮助。

Suggested change
localforage.setItem(WebGAL.gameKey, userDataState).then(() => {});
localforage.setItem(WebGAL.gameKey, userDataState).catch(err => console.error('Failed to save user data:', err));

}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/webgal/src/store/userDataReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const userDataSlice = createSlice({
state.appreciationData.cg.forEach((e) => {
if (url === e.url) {
isExist = true;
e.url = url;
e.name = name;
e.series = series;
}
});
Comment on lines 69 to 75

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

使用 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);
  }
},

由于这会改变函数整体结构,因此不提供可直接应用的修改建议,但强烈建议进行此重构。

Expand All @@ -84,7 +84,7 @@ const userDataSlice = createSlice({
state.appreciationData.bgm.forEach((e) => {
if (url === e.url) {
isExist = true;
e.url = url;
e.name = name;
e.series = series;
}
});
Comment on lines 84 to 90

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);
}

Expand Down