2022-12-22 10:43:33 +08:00

34 lines
916 B
TypeScript

import Log from "../Log";
import Timer from "../utils/Timer";
/**
* 基类、单例
*/
export default class Provider {
protected readonly className: string = 'Provider';
private static instance;
private static readonly pool = {};
constructor(...args: unknown[]) {
}
// 返回继承类的实例
public static getInstance<T extends typeof Provider>(this: T, ...args: unknown[]): InstanceType<T> {
if (!this.instance) {
let startTime = new Timer();
this.instance = new this(...args);
let thatName = this.instance.getClassName() || this.name;
Log.info('实例已创建,', thatName, this.instance, '耗时' + startTime.getTimeMs());
Provider.pool[thatName] = this.instance;
}
return this.instance;
}
public static getPool() {
return {
pool: Provider.pool,
}
}
}