Skip to content

Commit

Permalink
FIXES:q5js#45
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jul 24, 2024
1 parent 198ef81 commit 1c605a2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/q5-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,42 @@ Q5.modules.util = ($, p) => {
$.minute = () => new Date().getMinutes();
$.second = () => new Date().getSeconds();
};

Q5.load = (...args) => {
let p = new Promise((resolve, reject) => {
let count = 0;
let total = args.length;
args.forEach((arg) => {
if (typeof arg == 'string') {
count++;
Q5._loadFile(arg, () => {
count--;
if (count == 0) resolve();
}, 'text');
} else if (typeof arg == 'object') {
count++;
Q5._loadFile(arg.path, () => {
count--;
if (count == 0) resolve();
}, arg.type);
}
});
});
return p;
};

Q5._loadFile = (path, cb, type) => {
p._preloadCount++;
let ret = {};
fetch(path)
.then((r) => {
if (type == 'json') return r.json();
if (type == 'text') return r.text();
})
.then((r) => {
p._preloadCount--;
Object.assign(ret, r);
if (cb) cb(r);
});
return ret;
};

0 comments on commit 1c605a2

Please sign in to comment.