Refactor a few things, add policies to repository
This commit is contained in:
@@ -1,26 +1,20 @@
|
||||
import {readable, derived, type Readable, type Subscriber} from "svelte/store"
|
||||
import {indexBy, remove, now} from "@welshman/lib"
|
||||
import {withGetter} from "@welshman/store"
|
||||
import {ReadableWithGetter, withGetter} from "@welshman/store"
|
||||
import {getFreshness, setFreshnessThrottled} from "./freshness.js"
|
||||
|
||||
export const collection = <T>({
|
||||
name,
|
||||
store,
|
||||
getKey,
|
||||
load,
|
||||
}: {
|
||||
export type CachedLoaderOptions<T> = {
|
||||
name: string
|
||||
store: Readable<T[]>
|
||||
getKey: (item: T) => string
|
||||
load?: (key: string, relays: string[]) => Promise<any>
|
||||
}) => {
|
||||
const indexStore = withGetter(derived(store, $items => indexBy(getKey, $items)))
|
||||
indexStore: ReadableWithGetter<Map<string, T>>
|
||||
load: (key: string, relays: string[]) => Promise<any>
|
||||
subscribers?: Subscriber<T>[]
|
||||
}
|
||||
|
||||
export const makeCachedLoader = <T>({name, load, indexStore, subscribers = []}: CachedLoaderOptions<T>) => {
|
||||
const pending = new Map<string, Promise<T | void>>()
|
||||
const loadAttempts = new Map<string, number>()
|
||||
|
||||
let subscribers: Subscriber<T>[] = []
|
||||
|
||||
const loadItem = async (key: string, relays: string[] = []) => {
|
||||
return async (key: string, relays: string[] = []) => {
|
||||
const stale = indexStore.get().get(key)
|
||||
|
||||
// If we have no loader function, nothing we can do
|
||||
@@ -75,6 +69,21 @@ export const collection = <T>({
|
||||
|
||||
return fresh
|
||||
}
|
||||
}
|
||||
|
||||
export type CollectionOptions<T> = {
|
||||
name: string
|
||||
store: Readable<T[]>
|
||||
getKey: (item: T) => string
|
||||
load: (key: string, relays: string[]) => Promise<any>
|
||||
}
|
||||
|
||||
export const collection = <T>({name, store, getKey, load}: CollectionOptions<T>) => {
|
||||
const indexStore = withGetter(derived(store, $items => indexBy(getKey, $items)))
|
||||
|
||||
let subscribers: Subscriber<T>[] = []
|
||||
|
||||
const loadItem = makeCachedLoader({name, load, indexStore, subscribers})
|
||||
|
||||
const deriveItem = (key: string | undefined, relays: string[] = []) => {
|
||||
if (!key) {
|
||||
|
||||
@@ -10,21 +10,21 @@ export const urlIsMedia = (url: string) =>
|
||||
|
||||
// Copy some types from nostr-tools because I can't import them
|
||||
|
||||
type AddressPointer = {
|
||||
export type AddressPointer = {
|
||||
identifier: string
|
||||
pubkey: string
|
||||
kind: number
|
||||
relays?: string[]
|
||||
}
|
||||
|
||||
type EventPointer = {
|
||||
export type EventPointer = {
|
||||
id: string
|
||||
relays?: string[]
|
||||
author?: string
|
||||
kind?: number
|
||||
}
|
||||
|
||||
type ProfilePointer = {
|
||||
export type ProfilePointer = {
|
||||
pubkey: string
|
||||
relays?: string[]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {npubEncode} from "nostr-tools/nip19"
|
||||
import {ellipsize, parseJson} from "@welshman/lib"
|
||||
import {TrustedEvent} from "./Events.js"
|
||||
import {TrustedEvent, EventTemplate} from "./Events.js"
|
||||
import {getLnUrl} from "./Zaps.js"
|
||||
import {PROFILE} from "./Kinds.js"
|
||||
|
||||
@@ -50,12 +50,13 @@ export const readProfile = (event: TrustedEvent): PublishedProfile => ({
|
||||
event,
|
||||
})
|
||||
|
||||
export const createProfile = ({event, ...profile}: Profile) => ({
|
||||
export const createProfile = ({event, ...profile}: Profile): EventTemplate => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
tags: [],
|
||||
})
|
||||
|
||||
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
|
||||
export const editProfile = ({event, ...profile}: PublishedProfile): EventTemplate => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
tags: event.tags,
|
||||
|
||||
Reference in New Issue
Block a user