-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.69 KB
/
index.js
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
const modules = [];
var defaultModuleName = 'app';
var defaultModulePrefix = '$';
var apiMethod = ['use', 'getModules', 'getData'];
function isString(f) {
return Object.prototype.toString.call(f) === '[object String]';
}
class Store {
constructor(moduleName, prefix, callback) {
if (Store.instance === undefined) {
if (!(this instanceof Store)) return new Store(moduleName, prefix, callback);
if (isString(moduleName)) defaultModuleName = moduleName.trim();
if (isString(prefix)) defaultModulePrefix = prefix.trim();
modules.push(defaultModuleName);
this[defaultModuleName] = {}
Store.instance = new Proxy(this, {
get: function (target, key) {
if (key.charAt(0) === defaultModulePrefix && key.length > 1) {
return target[key.slice(1)];
} else if (apiMethod.indexOf(key) === -1) {
return target[defaultModuleName][key];
} else {
return target[key];
}
},
set: function (target, key, value) {
if (key.charAt(0) === defaultModulePrefix && key.length > 1) {
let moduleName = key.slice(1);
if (modules.indexOf(moduleName) === -1) modules.push(moduleName);
target[moduleName] = value;
} else {
target[defaultModuleName][key] = value;
}
if (callback) callback()
return true;
}
})
}
return Store.instance;
}
use(moduleName) {
if (isString(moduleName)) {
if (modules.indexOf(moduleName) > 0) defaultModuleName = moduleName;
}
}
getModules() {
console.log(modules.join(','))
}
getData() {
console.log(this)
}
}
export default Store;