audio-vis/raw/javascript/FetchHandler.js

36 lines
976 B
JavaScript
Raw Permalink Normal View History

2020-08-08 21:58:15 +02:00
class FetchHandler {
static files = {};
static async loadFiles(array, isJSON) {
let content = [];
for (let i = 0; i < array; i++) {
content.push(await FetchHandler.loadFile(array[i], isJSON));
}
return content;
}
static async loadFile(filename, isJSON) {
filename += '?v=' + version;
let files = FetchHandler.files;
if (files[filename]) {
return files[filename];
}
let data = await FetchHandler.tryFromCache(filename);
if (isJSON) {
data = JSON.parse(data);
}
files[filename] = data;
return data;
}
static async tryFromCache(filename) {
if (caches) {
let cache = await caches.open('vis3d-pwa-1');
let data = await cache.match(filename);
if (!data) {
data = await fetch(filename);
}
return await data.text();
}
}
}