104 lines
2.0 KiB
TypeScript
104 lines
2.0 KiB
TypeScript
interface UploadNProgress {
|
|
start: (msg?: string) => void;
|
|
done: () => void;
|
|
set: (progress: number) => void;
|
|
}
|
|
export type UploadProgressData = {
|
|
progress: number;
|
|
progressFixed: number;
|
|
filename?: string;
|
|
taskId?: string;
|
|
};
|
|
type UploadProgressOpts = {
|
|
onStart?: () => void;
|
|
onDone?: () => void;
|
|
onProgress?: (progress: number, data?: UploadProgressData) => void;
|
|
};
|
|
export class UploadProgress implements UploadNProgress {
|
|
/**
|
|
* 进度
|
|
*/
|
|
progress: number;
|
|
/**
|
|
* 开始回调
|
|
*/
|
|
onStart: (() => void) | undefined;
|
|
/**
|
|
* 结束回调
|
|
*/
|
|
onDone: (() => void) | undefined;
|
|
/**
|
|
* 消息回调
|
|
*/
|
|
onProgress: ((progress: number, data?: UploadProgressData) => void) | undefined;
|
|
/**
|
|
* 数据
|
|
*/
|
|
data: any;
|
|
/**
|
|
* 是否结束
|
|
*/
|
|
end: boolean;
|
|
constructor(uploadOpts: UploadProgressOpts) {
|
|
this.progress = 0;
|
|
this.end = false;
|
|
const mockFn = () => {};
|
|
this.onStart = uploadOpts.onStart || mockFn;
|
|
this.onDone = uploadOpts.onDone || mockFn;
|
|
this.onProgress = uploadOpts.onProgress || mockFn;
|
|
}
|
|
start(msg?: string) {
|
|
this.progress = 0;
|
|
msg && this.info(msg);
|
|
this.end = false;
|
|
this.onStart?.();
|
|
}
|
|
done() {
|
|
this.progress = 100;
|
|
this.end = true;
|
|
this.onDone?.();
|
|
}
|
|
set(progress: number, data?: UploadProgressData) {
|
|
this.progress = progress;
|
|
this.data = data;
|
|
this.onProgress?.(progress, data);
|
|
console.log('uploadProgress set', progress, data);
|
|
}
|
|
/**
|
|
* 开始回调
|
|
*/
|
|
setOnStart(callback: () => void) {
|
|
this.onStart = callback;
|
|
}
|
|
/**
|
|
* 结束回调
|
|
*/
|
|
setOnDone(callback: () => void) {
|
|
this.onDone = callback;
|
|
}
|
|
/**
|
|
* 消息回调
|
|
*/
|
|
setOnProgress(callback: (progress: number, data?: UploadProgressData) => void) {
|
|
this.onProgress = callback;
|
|
}
|
|
/**
|
|
* 打印信息
|
|
*/
|
|
info(msg: string) {
|
|
console.log(msg);
|
|
}
|
|
/**
|
|
* 打印错误
|
|
*/
|
|
error(msg: string) {
|
|
console.error(msg);
|
|
}
|
|
/**
|
|
* 打印警告
|
|
*/
|
|
warn(msg: string) {
|
|
console.warn(msg);
|
|
}
|
|
}
|