Slim down some things
This commit is contained in:
@@ -15,12 +15,11 @@ export const collection = <T, LoadArgs extends any[]>({
|
||||
load: (key: string, ...args: LoadArgs) => Promise<any>
|
||||
}) => {
|
||||
const indexStore = withGetter(derived(store, $items => indexBy(getKey, $items)))
|
||||
const getItem = (key: string) => indexStore.get().get(key)
|
||||
const pending = new Map<string, Promise<Maybe<T>>>()
|
||||
const loadAttempts = new Map<string, number>()
|
||||
|
||||
const loadItem = async (key: string, ...args: LoadArgs) => {
|
||||
const stale = getItem(key)
|
||||
const stale = indexStore.get().get(key)
|
||||
const freshness = getFreshness(name, key)
|
||||
|
||||
// If we have an item, reload if it's stale
|
||||
@@ -30,7 +29,7 @@ export const collection = <T, LoadArgs extends any[]>({
|
||||
|
||||
// If we already are loading, await and return
|
||||
if (pending.has(key)) {
|
||||
return pending.get(key)!.then(() => getItem(key))
|
||||
return pending.get(key)!.then(() => indexStore.get().get(key))
|
||||
}
|
||||
|
||||
const attempt = loadAttempts.get(key) || 0
|
||||
@@ -52,7 +51,7 @@ export const collection = <T, LoadArgs extends any[]>({
|
||||
|
||||
pending.delete(key)
|
||||
|
||||
const fresh = getItem(key)
|
||||
const fresh = indexStore.get().get(key)
|
||||
|
||||
if (fresh) {
|
||||
loadAttempts.delete(key)
|
||||
@@ -73,5 +72,5 @@ export const collection = <T, LoadArgs extends any[]>({
|
||||
return derived(indexStore, $index => $index.get(key))
|
||||
}
|
||||
|
||||
return {indexStore, deriveItem, loadItem, getItem}
|
||||
return {indexStore, deriveItem, loadItem}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,17 @@
|
||||
import {FOLLOWS, getListValues, asDecryptedEvent, readList} from '@welshman/util'
|
||||
import {FOLLOWS, asDecryptedEvent, readList} from '@welshman/util'
|
||||
import {type TrustedEvent, type PublishedList} from '@welshman/util'
|
||||
import {type SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {deriveEventsMapped, withGetter} from '@welshman/store'
|
||||
import {deriveEventsMapped} from '@welshman/store'
|
||||
import {repository, load} from './core'
|
||||
import {collection} from './collection'
|
||||
import {ensurePlaintext} from './plaintext'
|
||||
import {loadRelaySelections} from './relaySelections'
|
||||
|
||||
export const follows = withGetter(
|
||||
deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [FOLLOWS]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: async (event: TrustedEvent) =>
|
||||
readList(
|
||||
asDecryptedEvent(event, {
|
||||
content: await ensurePlaintext(event),
|
||||
}),
|
||||
),
|
||||
})
|
||||
)
|
||||
export const follows = deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [FOLLOWS]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: (event: TrustedEvent) =>
|
||||
readList(asDecryptedEvent(event)),
|
||||
})
|
||||
|
||||
export const {
|
||||
indexStore: followsByPubkey,
|
||||
@@ -33,6 +26,3 @@ export const {
|
||||
await load({...request, filters: [{kinds: [FOLLOWS], authors: [pubkey]}]})
|
||||
},
|
||||
})
|
||||
|
||||
export const getFollows = (pubkey: string) =>
|
||||
getListValues("p", followsByPubkey.get().get(pubkey))
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {writable, derived} from 'svelte/store'
|
||||
import {withGetter} from '@welshman/store'
|
||||
import {type SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {ctx, tryCatch, fetchJson, uniq, batcher, postJson, last} from '@welshman/lib'
|
||||
import {collection} from './collection'
|
||||
@@ -41,7 +40,7 @@ export async function queryProfile(nip05: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export const handles = withGetter(writable<Handle[]>([]))
|
||||
export const handles = writable<Handle[]>([])
|
||||
|
||||
export const fetchHandles = async (nip05s: string[]) => {
|
||||
const base = ctx.app.dufflepudUrl!
|
||||
|
||||
+12
-18
@@ -1,24 +1,22 @@
|
||||
import {MUTES, getListValues, asDecryptedEvent, readList} from '@welshman/util'
|
||||
import {MUTES, asDecryptedEvent, readList} from '@welshman/util'
|
||||
import {type TrustedEvent, type PublishedList} from '@welshman/util'
|
||||
import {type SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {deriveEventsMapped, withGetter} from '@welshman/store'
|
||||
import {deriveEventsMapped} from '@welshman/store'
|
||||
import {repository, load} from './core'
|
||||
import {collection} from './collection'
|
||||
import {ensurePlaintext} from './plaintext'
|
||||
import {loadRelaySelections} from './relaySelections'
|
||||
|
||||
export const mutes = withGetter(
|
||||
deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [MUTES]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: async (event: TrustedEvent) =>
|
||||
readList(
|
||||
asDecryptedEvent(event, {
|
||||
content: await ensurePlaintext(event),
|
||||
}),
|
||||
),
|
||||
})
|
||||
)
|
||||
export const mutes = deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [MUTES]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: async (event: TrustedEvent) =>
|
||||
readList(
|
||||
asDecryptedEvent(event, {
|
||||
content: await ensurePlaintext(event),
|
||||
}),
|
||||
),
|
||||
})
|
||||
|
||||
export const {
|
||||
indexStore: mutesByPubkey,
|
||||
@@ -34,7 +32,3 @@ export const {
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
export const getMutes = (pubkey: string) =>
|
||||
getListValues("p", mutesByPubkey.get().get(pubkey))
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ export const {
|
||||
indexStore: profilesByPubkey,
|
||||
deriveItem: deriveProfile,
|
||||
loadItem: loadProfile,
|
||||
getItem: getProfile,
|
||||
} = collection({
|
||||
name: "profiles",
|
||||
store: profiles,
|
||||
|
||||
@@ -1,56 +1,62 @@
|
||||
import {uniq} from '@welshman/lib'
|
||||
import {INBOX_RELAYS, RELAYS, getRelayTags, normalizeRelayUrl, type TrustedEvent} from '@welshman/util'
|
||||
import {type SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {deriveEvents, withGetter} from '@welshman/store'
|
||||
import {INBOX_RELAYS, RELAYS, normalizeRelayUrl, asDecryptedEvent, readList, getListTags, getRelayTags, getRelayTagValues} from '@welshman/util'
|
||||
import type {TrustedEvent, PublishedList, List} from '@welshman/util'
|
||||
import type {SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {deriveEventsMapped} from '@welshman/store'
|
||||
import {load, repository} from './core'
|
||||
import {collection} from './collection'
|
||||
|
||||
export const getRelayUrls = (event?: TrustedEvent): string[] =>
|
||||
uniq(
|
||||
getRelayTags(event?.tags || [])
|
||||
.map((t: string[]) => normalizeRelayUrl(t[1]))
|
||||
)
|
||||
export const getRelayUrls = (list?: List): string[] =>
|
||||
uniq(getRelayTagValues(getListTags(list)).map(normalizeRelayUrl))
|
||||
|
||||
export const getReadRelayUrls = (event?: TrustedEvent): string[] =>
|
||||
export const getReadRelayUrls = (list?: List): string[] =>
|
||||
uniq(
|
||||
getRelayTags(event?.tags || [])
|
||||
getRelayTags(getListTags(list))
|
||||
.filter((t: string[]) => !t[2] || t[2] === "read")
|
||||
.map((t: string[]) => normalizeRelayUrl(t[1]))
|
||||
)
|
||||
|
||||
export const getWriteRelayUrls = (event?: TrustedEvent): string[] =>
|
||||
export const getWriteRelayUrls = (list?: List): string[] =>
|
||||
uniq(
|
||||
getRelayTags(event?.tags || [])
|
||||
getRelayTags(getListTags(list))
|
||||
.filter((t: string[]) => !t[2] || t[2] === "write")
|
||||
.map((t: string[]) => normalizeRelayUrl(t[1]))
|
||||
)
|
||||
|
||||
export const relaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]}))
|
||||
export const relaySelections = deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [RELAYS]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: (event: TrustedEvent) =>
|
||||
readList(asDecryptedEvent(event)),
|
||||
})
|
||||
|
||||
export const {
|
||||
indexStore: relaySelectionsByPubkey,
|
||||
deriveItem: deriveRelaySelections,
|
||||
loadItem: loadRelaySelections,
|
||||
getItem: getRelaySelections,
|
||||
} = collection({
|
||||
name: "relaySelections",
|
||||
store: relaySelections,
|
||||
getKey: relaySelections => relaySelections.pubkey,
|
||||
getKey: relaySelections => relaySelections.event.pubkey,
|
||||
load: (pubkey: string, request: Partial<SubscribeRequestWithHandlers> = {}) =>
|
||||
load({...request, filters: [{kinds: [RELAYS], authors: [pubkey]}]}),
|
||||
})
|
||||
|
||||
export const inboxRelaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [INBOX_RELAYS]}]}))
|
||||
export const inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
|
||||
filters: [{kinds: [INBOX_RELAYS]}],
|
||||
itemToEvent: item => item.event,
|
||||
eventToItem: (event: TrustedEvent) =>
|
||||
readList(asDecryptedEvent(event)),
|
||||
})
|
||||
|
||||
export const {
|
||||
indexStore: inboxRelaySelectionsByPubkey,
|
||||
deriveItem: deriveInboxRelaySelections,
|
||||
loadItem: loadInboxRelaySelections,
|
||||
getItem: getInboxRelaySelections,
|
||||
} = collection({
|
||||
name: "inboxRelaySelections",
|
||||
store: inboxRelaySelections,
|
||||
getKey: inboxRelaySelections => inboxRelaySelections.pubkey,
|
||||
getKey: inboxRelaySelections => inboxRelaySelections.event.pubkey,
|
||||
load: (pubkey: string, request: Partial<SubscribeRequestWithHandlers> = {}) =>
|
||||
load({...request, filters: [{kinds: [INBOX_RELAYS], authors: [pubkey]}]}),
|
||||
})
|
||||
|
||||
+11
-5
@@ -1,11 +1,17 @@
|
||||
import {throttle} from 'throttle-debounce'
|
||||
import {derived, writable} from 'svelte/store'
|
||||
import {addToMapKey, inc, dec} from '@welshman/lib'
|
||||
import {getListValues} from '@welshman/util'
|
||||
import {getListTags, getPubkeyTagValues} from '@welshman/util'
|
||||
import {throttled, withGetter} from '@welshman/store'
|
||||
import {pubkey} from './session'
|
||||
import {follows, getFollows, followsByPubkey} from './follows'
|
||||
import {mutes, getMutes} from './mutes'
|
||||
import {follows, followsByPubkey} from './follows'
|
||||
import {mutes, mutesByPubkey} from './mutes'
|
||||
|
||||
export const getFollows = (pubkey: string) =>
|
||||
getPubkeyTagValues(getListTags(followsByPubkey.get().get(pubkey)))
|
||||
|
||||
export const getMutes = (pubkey: string) =>
|
||||
getPubkeyTagValues(getListTags(mutesByPubkey.get().get(pubkey)))
|
||||
|
||||
export const followersByPubkey = withGetter(
|
||||
derived(
|
||||
@@ -14,7 +20,7 @@ export const followersByPubkey = withGetter(
|
||||
const $followersByPubkey = new Map<string, Set<string>>()
|
||||
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getListValues("p", list)) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($followersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
}
|
||||
@@ -31,7 +37,7 @@ export const mutersByPubkey = withGetter(
|
||||
const $mutersByPubkey = new Map<string, Set<string>>()
|
||||
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getListValues("p", list)) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($mutersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import {writable, derived} from 'svelte/store'
|
||||
import {withGetter} from '@welshman/store'
|
||||
import {type Zapper} from '@welshman/util'
|
||||
import {type SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {ctx, identity, fetchJson, uniq, bech32ToHex, hexToBech32, tryCatch, batcher, postJson} from '@welshman/lib'
|
||||
import {collection} from './collection'
|
||||
import {deriveProfile} from './profiles'
|
||||
|
||||
export const zappers = withGetter(writable<Zapper[]>([]))
|
||||
export const zappers = writable<Zapper[]>([])
|
||||
|
||||
export const fetchZappers = async (lnurls: string[]) => {
|
||||
const base = ctx.app.dufflepudUrl!
|
||||
|
||||
Reference in New Issue
Block a user