diff --git a/src/lib/getable.ts b/src/lib/getable.ts deleted file mode 100644 index 0dae9f1..0000000 --- a/src/lib/getable.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { writable } from "svelte/store"; - -export function getable(init: T) { - const { subscribe, set } = writable(init); - return { - subscribe, - get: () => init, - set: (value: T) => set(init = value), - update: (fn: (state: T) => any) => set(init = fn(init)) - } -} \ No newline at end of file diff --git a/src/lib/images.ts b/src/lib/images.ts index 216c442..b243888 100644 --- a/src/lib/images.ts +++ b/src/lib/images.ts @@ -1,4 +1,4 @@ -import { getable } from "./getable"; +import { cacheable } from "./cacheable"; type Size = { width: number; @@ -26,7 +26,7 @@ export const images = createImages() function createImages() { type API = [string, number, string] - const { subscribe, set, get, update } = getable>([]) + const { subscribe, set, get, update } = cacheable>('County-Images', [], true) async function load() { if (!get().length) { diff --git a/src/lib/quotes.ts b/src/lib/quotes.ts index dcdff10..5a7c18e 100644 --- a/src/lib/quotes.ts +++ b/src/lib/quotes.ts @@ -1,16 +1,17 @@ -import { getable } from "./getable"; +import { cacheable } from "./cacheable"; import { locale, random } from "./utils"; export const quotes = createQuotes() function createQuotes() { - const { subscribe, set, get, update } = getable([['', '']]) + const { subscribe, set, get, update } = cacheable('County_Quotes', [['', '']], true) return { subscribe, set, get, update, async load() { - const url = `./assets/quotes_${locale()}.json` - const res = await fetch(url); - const json = await res.json() - set(json) + if (!get().length) { + const url = `./assets/quotes_${locale()}.json` + const res = await fetch(url); + set(await res.json()) + } }, random(id: number) { return get()[random(get().length - id)]