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.
165 lines
4.8 KiB
165 lines
4.8 KiB
import { PREVIEW } from "cc/env";
|
|
|
|
/**
|
|
* 缓存数据单例工具
|
|
*/
|
|
export default class DataInstanceT {
|
|
|
|
private static allIns: any[] = [];
|
|
/** 单例属性名 */
|
|
private static InstanceKey: symbol = Symbol();
|
|
/** 单例属性名 */
|
|
private static InsName: string = 'Ins';
|
|
/**
|
|
* 单例类装饰器
|
|
* @param _ifPassive 是否被动访问的时候创建实例,为false的话会立即创建出实例
|
|
* ! 被装饰的类的构造方法不能时public类型的
|
|
*/
|
|
public static DecorateInstance(_ifPassive: boolean = false) {
|
|
return function (target: any) {
|
|
if (_ifPassive) {
|
|
//添加一个监听属性
|
|
Object.defineProperty(target, DataInstanceT.InsName, {
|
|
get: function () {
|
|
if (!target[DataInstanceT.InstanceKey]) {
|
|
target[DataInstanceT.InstanceKey] = new target();
|
|
DataInstanceT.allIns.push(target[DataInstanceT.InstanceKey]);
|
|
}
|
|
if (PREVIEW) {
|
|
window[target.name] = target[DataInstanceT.InstanceKey];
|
|
}
|
|
return target[DataInstanceT.InstanceKey];
|
|
}
|
|
});
|
|
} else {
|
|
target[DataInstanceT.InsName] = new target();
|
|
if (PREVIEW) {
|
|
window[target.name] = target[DataInstanceT.InsName]
|
|
}
|
|
DataInstanceT.allIns.push(target[DataInstanceT.InsName]);
|
|
}
|
|
}
|
|
}
|
|
|
|
static initData(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element.initData == "function")
|
|
{
|
|
try {
|
|
element.initData();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
static initLogic(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element.initLogic == "function")
|
|
{
|
|
try {
|
|
element.initLogic();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
static ClearData(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element.ClearData == "function")
|
|
{
|
|
try {
|
|
element.ClearData();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
//重新构建数据(重生)
|
|
static ReBuildData(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element.ReBuildData == "function")
|
|
{
|
|
try {
|
|
element.ReBuildData();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// 数据修复
|
|
static RepairData(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element.RepairData == "function")
|
|
{
|
|
try {
|
|
element.RepairData();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
static RefreshEveryDay(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element._RefreshEveryDay == "function")
|
|
{
|
|
try {
|
|
element._RefreshEveryDay();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
static RefreshWeek(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element._RefreshWeek == "function")
|
|
{
|
|
try {
|
|
element._RefreshWeek();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
static RefreshMonth(){
|
|
for (let i = 0; i < this.allIns.length; i++) {
|
|
const element = this.allIns[i];
|
|
if(typeof element._RefreshMonth == "function")
|
|
{
|
|
try {
|
|
element._RefreshMonth();
|
|
} catch (error) {
|
|
error("错误",error,element);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|