Skip to content

Commit 5a3b879

Browse files
authored
Create ClientModuleJS.js
1 parent a992a99 commit 5a3b879

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

scripts/ClientModuleJS.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"use strict";
2+
3+
class Modules {
4+
5+
constructor(config) {
6+
this.Path = config.path || "scripts/modules/";
7+
this.Debugged = config.debug || false;
8+
this.Developer = config.developer || false;
9+
return this.Require.bind(this);
10+
}
11+
12+
Development(file){
13+
var Cache = this.Cache();
14+
this.getCode(file, function() {
15+
var res = this.responseText;
16+
var buffer = Cache.getCache(file);
17+
console.log("➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖");
18+
console.log("☑️ 👨🏻‍💻 DEBUG MODO DESENVOLVEDOR 👨🏻‍💻 ☑️");
19+
if(res != buffer){
20+
console.log("👉🏽 Cache Modificado! do modulo : "+file+" ✔️\n O modulo sera recarregado ✔️");
21+
Cache.setCache(file, res);
22+
}else{
23+
console.log("👉🏽 Não mudou nada no cache do modulo : "+file+" 🔴");
24+
}
25+
console.log("➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖");
26+
});
27+
}
28+
29+
getCode(dir, call) {
30+
var req = new XMLHttpRequest();
31+
req.onload = call;
32+
req.open("GET", dir, true);
33+
req.send();
34+
}
35+
36+
static ClearCache(reload) {
37+
if (sessionStorage.getItem("modulesLoaded") != null) {
38+
var Cached = JSON.parse(sessionStorage.getItem("modulesLoaded"));
39+
Cached.map((moduleName, moduleIndex) => {
40+
sessionStorage.removeItem(moduleName);
41+
});
42+
sessionStorage.removeItem("modulesLoaded");
43+
if ((reload || false)) {
44+
location.reload();
45+
}
46+
}
47+
}
48+
49+
Cache(n) {
50+
51+
return {
52+
exists(name) {
53+
if (sessionStorage.getItem(name) == null) {
54+
return false;
55+
} else {
56+
return true;
57+
}
58+
},
59+
setCache(n, c) {
60+
function PushModule(name) {
61+
var CacheModules = JSON.parse(sessionStorage.getItem("modulesLoaded"));
62+
if (CacheModules.indexOf(name) == -1) {
63+
CacheModules.push(name);
64+
sessionStorage.setItem("modulesLoaded", JSON.stringify(CacheModules));
65+
}
66+
}
67+
if (!this.exists("modulesLoaded")) {
68+
sessionStorage.setItem("modulesLoaded", JSON.stringify(new Array()));
69+
}
70+
PushModule(n);
71+
sessionStorage.setItem(n, c);
72+
},
73+
getCache(n) {
74+
return sessionStorage.getItem(n);
75+
}
76+
}
77+
78+
}
79+
80+
Patterns(code, debug, filename) {
81+
var m = new Function("", `
82+
const debug = ${debug};
83+
const exports = {};
84+
const module = {exports: {}};
85+
${code}
86+
if(Object.values(exports).length > 0){
87+
if(debug){ console.log("Modulo Importado via : exports"); }
88+
return exports;
89+
} else if(Object.values(module.exports).length > 0){
90+
if(debug){ console.log("Modulo Importado via : module.exports"); }
91+
return module.exports;
92+
} else{
93+
throw 'Indefinido "exports" ou "module.exports" No Arquivo : ${location.href+filename}';
94+
return exports;
95+
}
96+
`);
97+
return m;
98+
}
99+
100+
Require(filename) {
101+
const Path = this.Path;
102+
let file = Path + filename + ".js";
103+
var Cache = this.Cache();
104+
if (!Cache.exists(file)) {
105+
this.getCode(file, function() {
106+
var res = this.responseText;
107+
Cache.setCache(file, res);
108+
});
109+
}
110+
if (Cache.exists(file)) {
111+
if(this.Developer){ this.Development(file); }
112+
var p = this.Patterns(Cache.getCache(file), this.Debugged, file);
113+
return p();
114+
} else {
115+
var BotVerifyCache = setInterval(function() {
116+
if (Cache.exists(file)) {
117+
clearInterval(BotVerifyCache);
118+
location.reload();
119+
}
120+
}, 1);
121+
}
122+
}
123+
124+
}

0 commit comments

Comments
 (0)