-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·196 lines (170 loc) · 5.04 KB
/
index.js
File metadata and controls
executable file
·196 lines (170 loc) · 5.04 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
*
* @param __variable_name pass by the user that comet core should change as a response with call
* @param __user_data_object object hold all necessary data that user should pass
* @param __fetch_call_config fetch call custom config data object
* {
* this_scope: user_this_scope, (mandatory)
* url: api_call_url, (mandatory)
* default: variable_default_value, (optional)
* return: return_type_function_object, (optional)
* time: user_time_gap_for_api_call, (optional)
*
* }
*
* @constructor
* @private
*/
exports.V_ = function(__variable_name, __user_data_object) { return V_core(__variable_name, __user_data_object) }
/**
*
* @param __variable_name
* @param __user_data_object
* @constructor
*/
function V_core(__user_variable_name, __user_data_object) {
/**
*@description
* state variable name that user init in react app
*/
var __variable_name
/**
*@description
* this scope in react component
*/
var _this_scope;
/**
*@description
* api url path for fetch call
*/
var _url;
/**
*@description
* return this default value as props if api call failed
*/
var _default;
/**
*@description
* return method body, if user init method comet core pass return value of api to method body
*/
var _return;
/**
*@description
* time gap with two api call
*/
var _time;
/**
*
* @type {{}}
* @private
* @description
* user fetch api config data
*/
var _fetch_config;
if (isEmptyOrNullOrUndefined(__user_variable_name)) { console.log("V_comet ERROR: variable not pass"); return; }
if (__user_data_object == undefined || __user_data_object == null) { console.log("V_comet ERROR: object not pass"); return; }
try {
__variable_name = __user_variable_name.substring(__user_variable_name.lastIndexOf(".") + 1, __user_variable_name.length);
} catch (e) {
console.log("V_comet ERROR in user pass variable name");
return;
}
try {
_url = __user_data_object.url;
} catch (e) {
console.log("V_comet ERROR: url is not defined in user defined object", e);
return;
}
try {
_this_scope = __user_data_object.this_scope;
} catch (e) {
console.log("V_comet ERROR: this scope is defined in user defined object", e);
return;
}
try{
_default = __user_data_object.default;
} catch (e) {
_default = "";
}
try {
_time = __user_data_object.time;
} catch (e) {
_time = 0;
}
try {
_return = __user_data_object.return;
} catch (e) {
_return = null;
}
try {
_fetch_config = __user_data_object.config;
} catch (e) {
_fetch_config = {}
}
if (_time > 0) {
fetchCall(_url, _this_scope, __variable_name, _return, _default, _fetch_config);
setInterval(function () {
fetchCall(_url, _this_scope, __variable_name, _return, _default, _fetch_config);
}, _time);
} else {
fetchCall(_url, _this_scope, __variable_name, _return, _default, _fetch_config);
}
}
/**
*
* @param param_value
* @returns {boolean}
*/
function isEmptyOrNullOrUndefined(param_value) {
if (param_value == null || param_value == undefined || param_value == "") return true;
return false;
}
/**
*
* @param URL
* @param THIS
* @param VAR_NAME
* @param RETURN
*/
function fetchCall(URL, THIS, VAR_NAME, RETURN, DEFAULT, FETCH_CONFIG) {
fetch(URL, FETCH_CONFIG)
.then((res) => res.json())
.then(function (data) {
if (typeof data.error !== 'undefined' ) {
var obj = {};
obj[VAR_NAME] = DEFAULT;
try {
THIS.setState(obj);
} catch (e) {
if (e.message === 'THIS.setState is not a function') {
THIS[VAR_NAME] = obj[VAR_NAME];
}
}
} else {
var obj = {};
if (typeof RETURN === 'undefined') {
obj[VAR_NAME] = data;
} else {
obj[VAR_NAME] = RETURN(data);
}
try {
THIS.setState(obj);
} catch (e) {
if (e.message === 'THIS.setState is not a function') {
THIS[VAR_NAME] = obj[VAR_NAME];
}
}
}
})
.catch(function (error) {
var obj = {};
obj[VAR_NAME] = DEFAULT;
try {
THIS.setState(obj);
} catch (e) {
if (e.message === 'THIS.setState is not a function') {
THIS[VAR_NAME] = obj[VAR_NAME];
}
}
});
}