From 7274a455a8f67354d5079c8a3f8d4d88e25eb396 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sun, 29 May 2022 22:18:07 +0200 Subject: [PATCH] save sample list to local storage --- packages/webdirt/sampler.mjs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/webdirt/sampler.mjs b/packages/webdirt/sampler.mjs index 7a6e38b1..7dbc52b6 100644 --- a/packages/webdirt/sampler.mjs +++ b/packages/webdirt/sampler.mjs @@ -24,11 +24,16 @@ export const playSample = async (url) => playBuffer(await loadBuffer(url)); */ const githubCache = {}; let loaded; export const loadGithubSamples = async (path, nameFn) => { + const storageKey = 'loadGithubSamples ' + path; + const stored = localStorage.getItem(storageKey); + if (stored) { + return JSON.parse(stored); + } if (githubCache[path]) { return githubCache[path]; } + console.log('[sampler]: fetching sample list from github', path); try { - console.log('load github path', path); const [user, repo, ...folders] = path.split('/'); const baseUrl = `https://api.github.com/repos/${user}/${repo}/contents`; const banks = await fetch(`${baseUrl}/${folders.join('/')}`).then((res) => res.json()); @@ -54,11 +59,12 @@ export const loadGithubSamples = async (path, nameFn) => { {}, ); } catch (err) { - console.error('failed to fetch github samples', err); + console.error('[sampler]: failed to fetch sample list from github', err); return; } loaded = githubCache[path]; - console.log('loaded github path ', path); + localStorage.setItem(storageKey, JSON.stringify(loaded)); + console.log('[sampler]: loaded samples:', loaded); return githubCache[path]; };