]>
Commit | Line | Data |
---|---|---|
1 | import { readable } from 'svelte/store'; | |
2 | import { browser } from '$app/env'; | |
3 | ||
4 | const internals = { | |
5 | kHost: import.meta.env.VITE_PUBLIC_BASE_PATH || 'http://localhost:3000', | |
6 | kDataPrefix: '/data/', | |
7 | kAdjectivesPath: '/adjectives.json', | |
8 | kNounsPath: '/nouns.json', | |
9 | ||
10 | async get(locale, path) { | |
11 | ||
12 | const shortLocale = locale.split('-')[0]; | |
13 | const targetFile = internals.kHost + internals.kDataPrefix + shortLocale + path; | |
14 | const data = browser && localStorage.getItem(targetFile); | |
15 | ||
16 | if (data) { | |
17 | return JSON.parse(data); | |
18 | } | |
19 | ||
20 | let newData = await (await fetch(targetFile)).json(); | |
21 | browser && localStorage.setItem(targetFile, JSON.stringify(newData)); | |
22 | ||
23 | return newData; | |
24 | }, | |
25 | ||
26 | random(list) { | |
27 | ||
28 | return list[Math.floor(Math.random() * list.length)]; | |
29 | } | |
30 | }; | |
31 | ||
32 | export const getPrompt = function (locale) { | |
33 | ||
34 | return readable(null, function (set) { | |
35 | ||
36 | (async function() { | |
37 | ||
38 | const adjectives = await internals.get(locale, internals.kAdjectivesPath); | |
39 | const nouns = await internals.get(locale, internals.kNounsPath); | |
40 | ||
41 | set(`${internals.random(nouns)}, ${internals.random(nouns)}, ${internals.random(adjectives)}`); | |
42 | })(); | |
43 | ||
44 | return function stop() {}; | |
45 | }); | |
46 | }; |