You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GooseMod/src/moduleStore/jsCache.js

33 lines
1.1 KiB

let goosemodScope = {};
export const setThisScope = (scope) => {
goosemodScope = scope;
};
export const getCache = () => JSON.parse(goosemod.storage.get('goosemodJSCache') || '{}');
export const purgeCache = () => goosemod.storage.remove('goosemodJSCache');
export const updateCache = (moduleName, hash, js) => {
let cache = goosemodScope.moduleStoreAPI.jsCache.getCache();
cache[moduleName] = {hash, js};
goosemod.storage.set('goosemodJSCache', JSON.stringify(cache));
};
export const getJSForModule = async (moduleName) => {
const moduleInfo = goosemodScope.moduleStoreAPI.modules.find((x) => x.name === moduleName);
const cache = goosemodScope.moduleStoreAPI.jsCache.getCache();
if (cache[moduleName] && moduleInfo.hash === cache[moduleName].hash) {
return cache[moduleName].js;
} else {
const baseUrl = moduleInfo.repo.split('/').slice(0, -1).join('/');
const js = await (await fetch(`${baseUrl}/module/${moduleName}.js?_=${Date.now()}`)).text();
goosemodScope.moduleStoreAPI.jsCache.updateCache(moduleName, moduleInfo.hash, js);
return js;
}
};