refactor: impl
This commit is contained in:
@@ -13,12 +13,17 @@ const defaultCommands: MdCommanderCommandMap = {
|
||||
list: listTrackCommand,
|
||||
};
|
||||
|
||||
const [commandsStore, setCommandsStore] = createStore<{
|
||||
export interface CommandsStoreState {
|
||||
commands: MdCommanderCommandMap;
|
||||
initialized: boolean;
|
||||
}>({
|
||||
loading: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
const [commandsStore, setCommandsStore] = createStore<CommandsStoreState>({
|
||||
commands: { ...defaultCommands },
|
||||
initialized: false,
|
||||
loading: false,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -36,7 +41,7 @@ export function initializeCommands(customCommands?: MdCommanderCommandMap): MdCo
|
||||
*/
|
||||
export function registerCommands(customCommands?: MdCommanderCommandMap): void {
|
||||
const commands = initializeCommands(customCommands);
|
||||
setCommandsStore({ commands, initialized: true });
|
||||
setCommandsStore({ commands, initialized: true, loading: false });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,6 +65,41 @@ export function getCommand(name: string): MdCommanderCommand | undefined {
|
||||
return commandsStore.commands[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取加载状态
|
||||
*/
|
||||
export function getCommandsLoading(): boolean {
|
||||
return commandsStore.loading;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
*/
|
||||
export function getCommandsError(): string | undefined {
|
||||
return commandsStore.error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置加载状态
|
||||
*/
|
||||
export function setCommandsLoading(loading: boolean): void {
|
||||
setCommandsStore("loading", loading);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误信息
|
||||
*/
|
||||
export function setCommandsError(error?: string): void {
|
||||
setCommandsStore("error", error);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新命令
|
||||
*/
|
||||
export function updateCommands(updater: (prev: MdCommanderCommandMap) => MdCommanderCommandMap): void {
|
||||
setCommandsStore("commands", (prev) => updater(prev));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CSV 文件加载命令模板并更新命令定义
|
||||
*/
|
||||
@@ -67,6 +107,9 @@ export async function loadCommandTemplatesFromCSV(
|
||||
path: string,
|
||||
articlePath: string
|
||||
): Promise<void> {
|
||||
setCommandsLoading(true);
|
||||
setCommandsError(undefined);
|
||||
|
||||
try {
|
||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
||||
|
||||
@@ -82,7 +125,7 @@ export async function loadCommandTemplatesFromCSV(
|
||||
}
|
||||
|
||||
// 为每个命令添加模板
|
||||
setCommandsStore("commands", (prev) => {
|
||||
updateCommands((prev) => {
|
||||
const updated = { ...prev };
|
||||
for (const [commandName, rows] of templatesByCommand.entries()) {
|
||||
const cmd = updated[commandName];
|
||||
@@ -105,8 +148,14 @@ export async function loadCommandTemplatesFromCSV(
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
|
||||
setCommandsStore("initialized", true);
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
setCommandsError(`加载命令模板失败:${errorMessage}`);
|
||||
console.warn(`Error loading command templates from ${path}:`, error);
|
||||
} finally {
|
||||
setCommandsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user