]>
git.r.bdr.sh - rbdr/prompt/blob - src/lib/stores/prompt.js
1 import { readable
} from 'svelte/store';
2 import { browser
} from '$app/env';
5 kHost: import.meta
.env
.VITE_PUBLIC_BASE_PATH
|| 'http://localhost:3000',
7 kAdjectivesPath: '/adjectives.json',
8 kNounsPath: '/nouns.json',
10 async
get(locale
, path
) {
12 const shortLocale
= locale
.split('-')[0];
13 const targetFile
= internals
.kHost
+ internals
.kDataPrefix
+ shortLocale
+ path
;
14 const data
= browser
&& sessionStorage
.getItem(targetFile
);
17 return JSON
.parse(data
);
20 let newData
= await (await
fetch(targetFile
)).json();
21 browser
&& sessionStorage
.setItem(targetFile
, JSON
.stringify(newData
));
28 return list
[Math
.floor(Math
.random() * list
.length
)];
32 export const getPrompt = function (locale
) {
34 return readable(null, function (set) {
38 const adjectives
= await internals
.get(locale
, internals
.kAdjectivesPath
);
39 const nouns
= await internals
.get(locale
, internals
.kNounsPath
);
41 set(`${internals.random(nouns)}, ${internals.random(nouns)}, ${internals.random(adjectives)}`);
44 return function stop() {};