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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { initKey } from '@/Core/controller/storage/fastSaveLoad';
import { getFastSaveFromStorage, getSavesFromStorage } from '@/Core/controller/storage/savesController';
import { logger } from '@/Core/util/logger';
import axios from 'axios';
import { IGameVar } from '@/store/stageInterface';

/**
* 获取游戏信息
Expand All @@ -26,11 +27,14 @@ export const infoFetcher = (url: string) => {
await getStorageAsync();
getFastSaveFromStorage();
getSavesFromStorage(0, 0);
// 存储 config.txt 中的配置,用于清除所有数据时还原配置
const gameConfigInit: IGameVar = {};
// 按照游戏的配置开始设置对应的状态
gameConfig.forEach((e) => {
const { command, args } = e;
if (args.length > 0) {
if (args.length > 1) {
gameConfigInit[command] = args.join('|');
dispatch(
setGlobalVar({
key: command,
Expand All @@ -45,6 +49,7 @@ export const infoFetcher = (url: string) => {
res = Number(res);
}

gameConfigInit[command] = res;
dispatch(
setGlobalVar({
key: command,
Expand All @@ -65,6 +70,8 @@ export const infoFetcher = (url: string) => {
}
}
});

window.gameConfigInit = gameConfigInit;

Choose a reason for hiding this comment

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

medium

将状态直接存储在全局 window 对象上虽然可以快速解决问题,但通常被认为是一种需要谨慎使用的模式,因为它会:

  • 污染全局命名空间,可能与其他库或脚本产生冲突。
  • 使数据流变得不明确,难以追踪状态的变更来源,给未来的调试和维护带来困难。

从长远来看,一个更优雅的解决方案可能是将这份初始配置也交由状态管理库(如 Redux)来管理。例如,可以创建一个专门的 slice 来存储这份不会被“清空所有数据”操作影响的初始配置。

当然,目前的实现是有效的,这里仅作为架构上的改进建议供参考。

// @ts-expect-error renderPromiseResolve is a global variable
window.renderPromiseResolve();
setStorage();
Expand Down
2 changes: 1 addition & 1 deletion packages/webgal/src/store/userDataReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const userDataSlice = createSlice({
Object.assign(state.optionData, initialOptionSet);
},
resetAllData(state) {
Object.assign(state, cloneDeep(initState));
Object.assign(state, { ...cloneDeep(initState), globalGameVar: cloneDeep(window.gameConfigInit || {}) });
},
},
});
Expand Down
9 changes: 9 additions & 0 deletions packages/webgal/src/types/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IGameVar } from '@/store/stageInterface';

export {};

declare global {
interface Window {
gameConfigInit?: IGameVar;
}
}