blob: 43b9aa274a2c4df94d70d7bb4c4c68774c01e13a (
plain)
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
|
export default class BasicClassLoader {
constructor(classes = {}) {
this._classes = Object.assign(this._getDefaultClasses(), classes);
this._instances = {};
}
/**
*
* @param {String} name
* @param {*} properties
* @return {Object}
* @api
*/
getInstance(name, ...properties) {
if(!this._instances.hasOwnProperty(name) || !this._instances[name]) {
this._instances[name] = this.getClass(name, ...properties);
}
return this._instances[name];
}
/**
*
* @param {String} name
* @param {Object} object
* @return {BasicClassLoader}
* @api
*/
setInstance(name, object) {
this._instances[name] = object;
return this;
}
/**
*
* @param {String} name
* @param {*} properties
* @return {Object}
*/
getClass(name, ...properties) {
if(!this._classes.hasOwnProperty(name)) {
throw new Error(`The class ${name} does not exist`);
}
let creator = this._classes[name];
if(creator instanceof Function) {
if(!creator.prototype || creator.hasOwnProperty('arguments') && creator.hasOwnProperty('caller')) {
return creator(...properties);
}
return new creator(...properties);
} else {
return creator;
}
}
/**
*
* @param {String} name
* @param {Object} constructor
* @return {BasicClassLoader}
* @api
*/
registerClass(name, constructor) {
this._classes[name] = constructor;
return this;
}
/**
*
* @return {Object}
* @protected
*/
_getDefaultClasses() {
return {};
}
}
|