Skip to content

Commit

Permalink
add: cacheable images/quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Valexr committed May 8, 2024
1 parent 324fd35 commit 7933ec3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
11 changes: 0 additions & 11 deletions src/lib/getable.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/images.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getable } from "./getable";
import { cacheable } from "./cacheable";

type Size = {
width: number;
Expand Down Expand Up @@ -26,7 +26,7 @@ export const images = createImages()

function createImages() {
type API = [string, number, string]
const { subscribe, set, get, update } = getable<Array<API>>([])
const { subscribe, set, get, update } = cacheable<Array<API>>('County-Images', [], true)

async function load() {
if (!get().length) {
Expand Down
13 changes: 7 additions & 6 deletions src/lib/quotes.ts
Original file line number Diff line number Diff line change
@@ -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<Quote[]>([['', '']])
const { subscribe, set, get, update } = cacheable<Quote[]>('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)]
Expand Down

0 comments on commit 7933ec3

Please sign in to comment.