Browse Source

rewrite js loader

experimental
Paweł Jedwabny 4 years ago
parent
commit
278dba9329
2 changed files with 79 additions and 38 deletions
  1. +9
    -38
      src/epesi.js
  2. +70
    -0
      src/loader.js

+ 9
- 38
src/epesi.js View File

@@ -5,7 +5,10 @@
* @licence MIT
*/

import Loader from './loader';

const Epesi = {
loader: new Loader(),
default_indicator:'loading...',
procOn:0,
client_id:0,
@@ -208,51 +211,19 @@ const Epesi = {
else if(type=='a')//append
t.append(txt);
},
//js loader
loaded_jss:new Array(),
to_load_jss:new Array(),
to_append_jss:new Array(),
js_loader_running:false,
load_js:function(file) {
if (Epesi.loaded_jss.indexOf(file)!=-1) return;
Epesi.to_load_jss.push(file);
if(Epesi.js_loader_running==false) {
Epesi.js_loader_running=true;
Epesi.js_loader();
}
Epesi.loader.load_js(file);
},
append_js:function(texti) {
if(Epesi.js_loader_running==false) {
Epesi.append_js_script(texti);
} else
Epesi.to_append_jss.push(texti);
Epesi.loader.execute_js(texti);
},
append_js_script:function(texti) {
let fileref = document.createElement("script");
fileref.setAttribute("type", "text/javascript");
fileref.text = texti;
document.getElementsByTagName("head").item(0).appendChild(fileref);
console.warn('DEPRECATED: use Loader.execute_js instead');
Loader.insertScript(texti);
},
js_loader:function() {
let file = Epesi.to_load_jss.shift();
if(typeof file != 'undefined') {
let fileref=document.createElement("script")
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", file);
fileref.onload=fileref.onreadystatechange=function() {
if (fileref.readyState && fileref.readyState != 'loaded' && fileref.readyState != 'complete')
return;
Epesi.loaded_jss.push(file);
Epesi.js_loader();
}
document.getElementsByTagName("head").item(0).appendChild(fileref);
} else {
jq(Epesi.to_append_jss).each(function(i,texti) {
Epesi.append_js_script(texti);
});
Epesi.to_append_jss.length=0;
Epesi.js_loader_running = false;
}
console.warn('DEPRECATED: load is invoked implicitly');
Epesi.loader.load();
},
//csses
loaded_csses:new Array(),


+ 70
- 0
src/loader.js View File

@@ -0,0 +1,70 @@
import Promise from 'bluebird';

class Loader {
loading = false;
loaded_js = [];
to_load_js = [];
to_execute_js = [];

static loadScript = file => {
return new Promise(function (resolve, reject) {
let script = document.createElement('script');
script.type = 'text/javascript';
script.onerror = reject;
script.src = file;
script.onload = script.onreadystatechange = function (_, isAbort) {
if (isAbort || !script.readyState || /loaded|complete/.test(script.readyState)) {
script.onload = script.onreadystatechange = null;
script = undefined;

if (!isAbort) {
resolve();
}
}
};
document.getElementsByTagName('head').item(0).appendChild(script);
});
};

static insertScript = code => {
return new Promise(function (resolve, reject) {
let script = document.createElement('script');
script.type = 'text/javascript';
script.onerror = reject;
script.text = code;
document.getElementsByTagName('head').item(0).appendChild(script);
});
};

load_js = async file => {
if (this.loaded_js.includes(file) || this.to_load_js.includes(file)) return;
this.to_load_js.push(file);
await this.load();
};

execute_js = async code => {
this.to_execute_js.push(code);
await this.load();
};

load = async () => {
if(this.loading === true) return;
this.loading = true;

let file;
while(file = this.to_load_js.shift()) {
if(this.loaded_js.includes(file)) continue;
await Loader.loadScript(file);
this.loaded_js.push(file);
}

let code;
while (code = this.to_execute_js.shift()) {
Loader.insertScript(code);
}

this.loading = false;
}
}

export default Loader;

Loading…
Cancel
Save