From 1c605a2f4ecccf079aa7396533e0db6422b5e961 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 24 Jul 2024 23:25:36 +0530 Subject: [PATCH] FIXES:#45 --- src/q5-util.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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