You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.9 KiB
123 lines
3.9 KiB
import { LoadTaskMgr } from "./LoadTaskMgr";
|
|
|
|
export interface IProgressCallback {
|
|
(current: number, total: number, itemName?: string): void;
|
|
}
|
|
|
|
export interface ILoadingTask {
|
|
execute: (progressCallback?: IProgressCallback) => Promise<void>;
|
|
name: string;
|
|
weight?: number; // 任务权重,影响进度比例
|
|
}
|
|
|
|
export class LoadingMgr{
|
|
private tasks: ILoadingTask[] = [];
|
|
private totalWeight: number = 0;
|
|
private currentWeight: number = 0;
|
|
private currentTaskIndex: number = 0;
|
|
|
|
private onProgress: (progress: number, message: string, detailMessage?: string) => void;
|
|
private onComplete: () => void;
|
|
private onError: (error: any) => void;
|
|
|
|
constructor(
|
|
task: Record<string, () => ILoadingTask>
|
|
) {
|
|
this.init(task);
|
|
}
|
|
|
|
initCallBack(
|
|
onProgress: (progress: number, message: string, detailMessage?: string) => void,
|
|
onComplete: () => void,
|
|
onError: (error: any) => void) {
|
|
this.onProgress = onProgress;
|
|
this.onComplete = onComplete;
|
|
this.onError = onError;
|
|
}
|
|
|
|
private addTask(task: ILoadingTask) {
|
|
task.weight = task.weight || 1;
|
|
this.tasks.push(task);
|
|
this.totalWeight += task.weight;
|
|
}
|
|
|
|
private init(task: Record<string, () => ILoadingTask>){
|
|
let tempConfig = new LoadTaskMgr();
|
|
tempConfig.init(task);
|
|
// 添加所有加载任务
|
|
tempConfig.getAllTasks().forEach(task => {
|
|
this.addTask(task);
|
|
});
|
|
}
|
|
|
|
public async start() {
|
|
this.currentWeight = 0;
|
|
this.currentTaskIndex = 0;
|
|
|
|
try {
|
|
for (let i = 0; i < this.tasks.length; i++) {
|
|
const task = this.tasks[i];
|
|
this.currentTaskIndex = i;
|
|
//ConsoleEx.create('log','#9BA4B4','[执行任务]','#fff')(`${task.name}`);
|
|
const now = performance.now();
|
|
this.onProgress(this.currentWeight / this.totalWeight, `正在${task.name}...`);
|
|
// 创建进度回调函数
|
|
const progressCallback: IProgressCallback = (current: number, total: number, itemName?: string) => {
|
|
const subProgress = total > 0 ? current / total : 0;
|
|
const taskProgress = (this.currentWeight + subProgress * task.weight!) / this.totalWeight;
|
|
let detailMessage = '';
|
|
if (itemName) {
|
|
detailMessage = `正在加载: ${itemName} (${current}/${total})`;
|
|
} else if (total > 0) {
|
|
detailMessage = `进度: ${current}/${total}`;
|
|
}
|
|
this.onProgress(taskProgress, `正在${task.name}...`, detailMessage);
|
|
};
|
|
|
|
await task.execute(progressCallback);
|
|
|
|
this.currentWeight += task.weight!;
|
|
this.onProgress(this.currentWeight / this.totalWeight, `${task.name}完成`);
|
|
const end = performance.now() - now;
|
|
ConsoleEx.Com(`${task.name} 用时: ${end}ms`);
|
|
}
|
|
this.onComplete();
|
|
} catch (error) {
|
|
this.onError(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前任务信息
|
|
*/
|
|
public getCurrentTaskInfo(): { index: number, total: number, name: string } {
|
|
const currentTask = this.tasks[this.currentTaskIndex];
|
|
return {
|
|
index: this.currentTaskIndex,
|
|
total: this.tasks.length,
|
|
name: currentTask?.name || ''
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 重置加载管理器(用于重新开始)
|
|
*/
|
|
public reset(): void {
|
|
this.currentWeight = 0;
|
|
this.currentTaskIndex = 0;
|
|
}
|
|
|
|
/**
|
|
* 获取总权重
|
|
*/
|
|
public getTotalWeight(): number {
|
|
return this.totalWeight;
|
|
}
|
|
|
|
/**
|
|
* 获取所有任务列表
|
|
*/
|
|
public getTasks(): ILoadingTask[] {
|
|
return [...this.tasks];
|
|
}
|
|
}
|