102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import { makeAutoObservable } from "mobx";
|
|
|
|
export interface ModelLoadingState {
|
|
isLoading: boolean;
|
|
progress: number;
|
|
modelId: string | null;
|
|
error?: string;
|
|
startTime?: number;
|
|
}
|
|
|
|
class ModelLoadingStore {
|
|
private loadingStates: Map<string, ModelLoadingState> = new Map();
|
|
|
|
constructor() {
|
|
makeAutoObservable(this);
|
|
}
|
|
|
|
// Начать отслеживание загрузки модели
|
|
startLoading(modelId: string) {
|
|
this.loadingStates.set(modelId, {
|
|
isLoading: true,
|
|
progress: 0,
|
|
modelId,
|
|
startTime: Date.now(),
|
|
});
|
|
}
|
|
|
|
// Обновить прогресс загрузки
|
|
updateProgress(modelId: string, progress: number) {
|
|
const state = this.loadingStates.get(modelId);
|
|
if (state) {
|
|
state.progress = Math.min(100, Math.max(0, progress));
|
|
}
|
|
}
|
|
|
|
// Завершить загрузку модели
|
|
finishLoading(modelId: string) {
|
|
const state = this.loadingStates.get(modelId);
|
|
if (state) {
|
|
state.isLoading = false;
|
|
state.progress = 100;
|
|
}
|
|
}
|
|
|
|
// Остановить загрузку (в случае ошибки)
|
|
stopLoading(modelId: string) {
|
|
this.loadingStates.delete(modelId);
|
|
}
|
|
|
|
// Обработать ошибку загрузки
|
|
handleError(modelId: string, error?: string) {
|
|
const state = this.loadingStates.get(modelId);
|
|
if (state) {
|
|
state.isLoading = false;
|
|
state.error = error || "Ошибка загрузки модели";
|
|
}
|
|
}
|
|
|
|
// Получить состояние загрузки для конкретной модели
|
|
getLoadingState(modelId: string): ModelLoadingState | undefined {
|
|
return this.loadingStates.get(modelId);
|
|
}
|
|
|
|
// Проверить, загружается ли какая-либо модель
|
|
get isAnyModelLoading(): boolean {
|
|
return Array.from(this.loadingStates.values()).some(
|
|
(state) => state.isLoading
|
|
);
|
|
}
|
|
|
|
// Получить все загружающиеся модели
|
|
get loadingModels(): ModelLoadingState[] {
|
|
return Array.from(this.loadingStates.values()).filter(
|
|
(state) => state.isLoading
|
|
);
|
|
}
|
|
|
|
// Получить общий прогресс всех загружающихся моделей
|
|
get overallProgress(): number {
|
|
const loadingModels = this.loadingModels;
|
|
if (loadingModels.length === 0) return 100;
|
|
|
|
const totalProgress = loadingModels.reduce(
|
|
(sum, model) => sum + model.progress,
|
|
0
|
|
);
|
|
return Math.round(totalProgress / loadingModels.length);
|
|
}
|
|
|
|
// Проверить, заблокировано ли сохранение (есть ли загружающиеся модели)
|
|
get isSaveBlocked(): boolean {
|
|
return this.isAnyModelLoading;
|
|
}
|
|
|
|
// Очистить все состояния загрузки
|
|
clearAll() {
|
|
this.loadingStates.clear();
|
|
}
|
|
}
|
|
|
|
export const modelLoadingStore = new ModelLoadingStore();
|