Add app package

This commit is contained in:
Jon Staab
2024-08-30 09:46:47 -07:00
parent e462ec2ada
commit 6e1ad713c3
34 changed files with 1193 additions and 48 deletions
+57
View File
@@ -0,0 +1,57 @@
import {writable, derived} from 'svelte/store'
import {withGetter} from '@welshman/store'
import type {Zapper} from '@welshman/util'
import {uniq, bech32ToHex, indexBy, tryCatch, uniqBy, batcher, postJson} from '@welshman/lib'
import {env} from './core'
import {collection} from './collection'
import {profilesByPubkey} from './profiles'
export const zappers = withGetter(writable<Zapper[]>([]))
export const zappersByPubkey = derived([profilesByPubkey, zappers], ([$profilesByPubkey, $zappers]) =>
indexBy(
$zapper => $zapper.pubkey,
$zappers.filter($zapper => $zapper.pubkey && $profilesByPubkey.get($zapper.pubkey)?.lnurl === $zapper.lnurl),
),
)
export const fetchZappers = (lnurls: string[]) => {
const base = env.DUFFLEPUD_URL!
if (!base) {
throw new Error("DUFFLEPUD_URL is required to fetch zapper info")
}
const res: any = postJson(`${base}/zapper/info`, {lnurls: lnurls.map(bech32ToHex)})
const zappersByLnurl = new Map<string, Zapper>()
for (const {lnurl, info} of res?.data || []) {
tryCatch(() => zappersByLnurl.set(bech32ToHex(lnurl), info))
}
return zappersByLnurl
}
export const {
indexStore: zappersByLnurl,
deriveItem: deriveZapper,
loadItem: loadZapper,
} = collection({
name: "zappers",
store: zappers,
getKey: (zapper: Zapper) => zapper.lnurl,
load: batcher(800, async (lnurls: string[]) => {
const fresh = await fetchZappers(uniq(lnurls))
const stale = zappersByLnurl.get()
const items: Zapper[] = lnurls.map(lnurl => {
const zapper = fresh.get(lnurl) || stale.get(lnurl) || {}
return {...zapper, lnurl}
})
zappers.update($zappers => uniqBy($zapper => $zapper.lnurl, [...$zappers, ...items]))
return items
}),
})