61a41c98-5e16-4333-a56e-9b3d839f91d2.js
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";
cc._RF.push(module, '61a41yYXhZDM6Vumz2Dn5HS', 'Type');
// script/common/type/Type.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Type = void 0;
const Type = {
registry: new Map(),
/**
*
* @param typeId must unique in app
*/
declare(typeId, ...superTypes) {
if (this.registry.has(typeId)) {
throw Error("The type named '" + typeId + "' had been registered already. Check the conflict of the name.");
}
let assignableTypes = calTypeId(typeId, ...superTypes);
let typeObj = {
id: typeId,
superTypes: superTypes,
assignableTypes: assignableTypes,
/**
* test if obj is a T instance.
* @param obj
*/
isInstanceOf(obj) {
let objTypeId = obj.__assignableTypes;
console.log("isInstanceOf: objTypeId=" + objTypeId);
// undefined false
if (objTypeId === undefined)
return false;
// single type
console.log("isInstanceOf: __id=" + this.id);
if (objTypeId === this.id)
return true;
// union types
let objTypeIds = objTypeId.split("|");
for (const i in objTypeIds) {
let id = objTypeIds[i];
console.log("isInstanceOf: id=" + id);
if (id === this.id)
return true;
}
return false;
}
};
this.registry.set(typeId, typeObj);
console.log("type=" + typeId + "Type");
console.log("assignableTypes=" + assignableTypes);
console.log("typeId=" + typeId);
return typeObj;
},
getType(typeId) {
if (this.registry.has(typeId))
return this.registry.get(typeId);
throw new Error("Type [" + typeId + "] is not found!");
},
isInstanceOf(typeId, obj) {
if (!this.registry.has(typeId))
return false;
let typeObj = this.registry.get(typeId);
return typeObj.isInstanceOf(obj);
}
};
exports.Type = Type;
/**
*
* @param types
* @returns
*/
function calTypeId(selfTypeId, ...types) {
let idSet = _calTypeId(selfTypeId, ...types);
let idStr = "";
idSet.forEach(id => {
idStr += id + "|";
});
return idStr.substr(0, idStr.length - 1);
}
function _calTypeId(selfTypeId, ...types) {
let resultTypeIds = new Set();
if (selfTypeId.length > 0) {
resultTypeIds.add(selfTypeId);
}
for (const type of types) {
resultTypeIds.add(type.id);
if (type.superTypes && type.superTypes.length > 0) {
let superIds = _calTypeId("", ...type.superTypes);
for (const id of superIds) {
resultTypeIds.add(id);
}
}
}
return resultTypeIds;
}
cc._RF.pop();