skip already queued in onDOMscripted

This commit is contained in:
tophf 2017-08-29 17:12:46 +03:00
parent c15082fed4
commit b91324992d

View File

@ -68,14 +68,15 @@ function onDOMready() {
function onDOMscripted(scripts) {
const queue = onDOMscripted.queue = onDOMscripted.queue || [];
if (scripts) {
return new Promise(resolve => {
addResolver(resolve);
onDOMscripted.scriptQueue = scripts;
queue.push(...scripts.filter(el => !queue.includes(el)));
loadNextScript();
});
}
if (onDOMscripted.scriptQueue) {
if (queue.length) {
return new Promise(resolve => addResolver(resolve));
}
if (document.readyState !== 'loading') {
@ -88,9 +89,9 @@ function onDOMscripted(scripts) {
return onDOMready().then(onDOMscripted);
function loadNextScript() {
const next = onDOMscripted.scriptQueue.shift();
if (!next) {
onDOMscripted.scriptQueue = null;
const empty = !queue.length;
const next = !empty && queue.shift();
if (empty) {
onDOMscripted();
} else if (typeof next === 'function') {
Promise.resolve(next())