Make outbox loader useful for non plain replaceables, add tap and bind

This commit is contained in:
Jon Staab
2026-02-18 15:27:34 -08:00
parent ca8a2754b9
commit e2f438799f
3 changed files with 28 additions and 3 deletions
+7 -2
View File
@@ -8,6 +8,7 @@ import {
getRelaysFromList, getRelaysFromList,
RelayMode, RelayMode,
Filter, Filter,
isPlainReplaceableKind,
} from "@welshman/util" } from "@welshman/util"
import { import {
deriveItemsByKey, deriveItemsByKey,
@@ -55,7 +56,7 @@ export const deriveRelayList = makeDeriveItem(relayListsByPubkey, loadRelayList)
// Outbox loader // Outbox loader
export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filter = {}) => { export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filter = {}) => {
const filters = [{...filter, kinds: [kind], authors: [pubkey], limit: 1}] const filters = [{...filter, kinds: [kind], authors: [pubkey]}]
const writeRelays = getRelaysFromList(await loadRelayList(pubkey), RelayMode.Write) const writeRelays = getRelaysFromList(await loadRelayList(pubkey), RelayMode.Write)
const allRelays = Router.get() const allRelays = Router.get()
.FromRelays(writeRelays) .FromRelays(writeRelays)
@@ -63,6 +64,10 @@ export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filt
.limit(8) .limit(8)
.getUrls() .getUrls()
if (isPlainReplaceableKind(kind)) {
filters[0].limit = 1
}
for (const relays of chunk(2, allRelays)) { for (const relays of chunk(2, allRelays)) {
const events = await load({filters, relays}) const events = await load({filters, relays})
@@ -73,7 +78,7 @@ export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filt
} }
export const makeOutboxLoader = export const makeOutboxLoader =
(kind: number, filter: Filter = {}) => (kind: number, filter: Filter = {}, limit = 1) =>
async (pubkey: string, relayHints: string[] = []) => { async (pubkey: string, relayHints: string[] = []) => {
const filters = [{...filter, kinds: [kind], authors: [pubkey]}] const filters = [{...filter, kinds: [kind], authors: [pubkey]}]
const relays = Router.get().FromRelays(relayHints).getUrls() const relays = Router.get().FromRelays(relayHints).getUrls()
+1 -1
View File
@@ -64,7 +64,7 @@ export const requestPage = async ({
onEvent, onEvent,
relays, relays,
filters, filters,
threshold: 0.8, threshold: 0.5,
autoClose, autoClose,
}), }),
), ),
+20
View File
@@ -57,6 +57,26 @@ export const always =
*/ */
export const not = (x: any, ...args: unknown[]) => !x export const not = (x: any, ...args: unknown[]) => !x
/**
* Bind function
* @param f - Function to bind
* @returns bound function
*/
export const bind = <F extends (...args: any[]) => any>(f: F, ...args: Parameters<F>) =>
f.bind(null, ...args)
/**
* Runs the provided function on the given value and returns the value
* @param f - Function to call
* @returns tapped function
*/
export const tap =
<T, F extends (x: T) => unknown>(f: F) =>
(x: T) => {
f(x)
return x
}
/** /**
* Deep equality comparison * Deep equality comparison
* @param a - First value * @param a - First value