diff --git a/src/q5-util.js b/src/q5-util.js index 428e89c..40f2799 100644 --- a/src/q5-util.js +++ b/src/q5-util.js @@ -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; +}; \ No newline at end of file