forked from code-charity/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
125 lines (101 loc) · 2.87 KB
/
core.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*-----------------------------------------------------------------------------
>>> CORE
-----------------------------------------------------------------------------*/
const Satus = new function() {
let memory = {},
storage = {},
events = {};
function get(type, name, on) {
let object = type === 'storage' ? storage : memory,
path;
if (typeof name !== 'string') {
return false;
}
path = name.split('/').filter(function(value) {
return value != '';
});
for (let i = 0, l = path.length; i < l; i++) {
object = object[path[i]];
if (Satus.isset(object) === false) {
break;
}
}
if (on !== false) {
Satus.trigger('get');
}
return object;
}
function set(type, name, value, on) {
let object = type === 'storage' ? storage : memory,
path;
if (typeof name !== 'string') {
return false;
}
path = name.split('/').filter(function(value) {
return value != '';
});
for (let i = 0, l = path.length; i < l; i++) {
let key = path[i];
if (i === l - 1) {
object[key] = value;
} else {
if (
Satus.isset(object) === false ||
Satus.isset(object[key]) === false
) {
object[key] = {};
}
object = object[key];
}
}
if (on !== false) {
Satus.trigger('set', {
name: name,
value: value
});
}
}
this.components = {};
this.isset = function(variable) {
if (typeof variable === 'undefined' || variable === null) {
return false;
}
return true;
};
this.memory = {
get: function(name, on) {
return get('memory', name, on);
},
set: function(name, value, on) {
set('memory', name, value, on);
},
clear: function() {
memory = {};
}
};
this.storage = {
get: function(name, on) {
return get('storage', name, on);
},
set: function(name, value, on) {
set('storage', name, value, on);
},
clear: function() {
storage = {};
Satus.trigger('clear');
}
};
this.on = function(name, callback) {
if (!events.hasOwnProperty(name)) {
events[name] = [];
}
events[name].push(callback);
};
this.trigger = function(name, data) {
if (events.hasOwnProperty(name)) {
for (let i = 0, l = events[name].length; i < l; i++) {
events[name][i](data);
}
}
};
};