save sample list to local storage

This commit is contained in:
Felix Roos 2022-05-29 22:18:07 +02:00
parent 91dfef52ea
commit 7274a455a8

View File

@ -24,11 +24,16 @@ export const playSample = async (url) => playBuffer(await loadBuffer(url)); */
const githubCache = {}; const githubCache = {};
let loaded; let loaded;
export const loadGithubSamples = async (path, nameFn) => { export const loadGithubSamples = async (path, nameFn) => {
const storageKey = 'loadGithubSamples ' + path;
const stored = localStorage.getItem(storageKey);
if (stored) {
return JSON.parse(stored);
}
if (githubCache[path]) { if (githubCache[path]) {
return githubCache[path]; return githubCache[path];
} }
console.log('[sampler]: fetching sample list from github', path);
try { try {
console.log('load github path', path);
const [user, repo, ...folders] = path.split('/'); const [user, repo, ...folders] = path.split('/');
const baseUrl = `https://api.github.com/repos/${user}/${repo}/contents`; const baseUrl = `https://api.github.com/repos/${user}/${repo}/contents`;
const banks = await fetch(`${baseUrl}/${folders.join('/')}`).then((res) => res.json()); const banks = await fetch(`${baseUrl}/${folders.join('/')}`).then((res) => res.json());
@ -54,11 +59,12 @@ export const loadGithubSamples = async (path, nameFn) => {
{}, {},
); );
} catch (err) { } catch (err) {
console.error('failed to fetch github samples', err); console.error('[sampler]: failed to fetch sample list from github', err);
return; return;
} }
loaded = githubCache[path]; loaded = githubCache[path];
console.log('loaded github path ', path); localStorage.setItem(storageKey, JSON.stringify(loaded));
console.log('[sampler]: loaded samples:', loaded);
return githubCache[path]; return githubCache[path];
}; };