-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeDef.js
More file actions
87 lines (73 loc) · 1.95 KB
/
Copy pathTypeDef.js
File metadata and controls
87 lines (73 loc) · 1.95 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
'use strict';
module.exports = (XIBLE) => {
let TYPE_DEFS = null;
class TypeDef {
constructor(obj) {
if (obj) {
Object.assign(this, obj);
}
}
/**
* Verifies whether the given typeDef matches this typeDef.
* If not matched directly, the extends property (-tree) of the given typeDef
* is verified against this typeDef.
* @returns {Boolean}
*/
matches(typeDef) {
if (typeDef === this) {
return true;
}
if (!typeDef || !typeDef.extends) {
return false;
}
// check for extends
if (typeof typeDef.extends === 'string') {
const extendsTypeDef = TYPE_DEFS[typeDef.extends];
if (!extendsTypeDef || extendsTypeDef === typeDef) {
return false;
}
return this.matches(extendsTypeDef);
}
if (Array.isArray(typeDef.extends)) {
for (let i = 0; i < typeDef.extends.length; i += 1) {
const extendsTypeDef = TYPE_DEFS[typeDef.extends[i]];
if (!extendsTypeDef) {
continue;
}
if (this.matches(extendsTypeDef)) {
return true;
}
}
}
return false;
}
/**
* Returns the cached result for getAll().
* If there is no result yet, simply returns getAll().
* @private
*/
static getAllCached() {
if (TYPE_DEFS) {
return Promise.resolve(TYPE_DEFS);
}
return this.getAll();
}
/**
* Retrieves all typeDefs from the XIBLE API.
* @returns {Promise.<TypeDef[]>}
*/
static getAll() {
const req = XIBLE.http.request('GET', 'api/typedefs');
return req.toObject(TypeDef)
.then((typeDefs) => {
TYPE_DEFS = {};
Object.keys(typeDefs)
.forEach((typeDefName) => {
TYPE_DEFS[typeDefName] = new TypeDef(typeDefs[typeDefName]);
});
return TYPE_DEFS;
});
}
}
return TypeDef;
};