Make outbox loader useful for non plain replaceables, add tap and bind
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
getRelaysFromList,
|
||||
RelayMode,
|
||||
Filter,
|
||||
isPlainReplaceableKind,
|
||||
} from "@welshman/util"
|
||||
import {
|
||||
deriveItemsByKey,
|
||||
@@ -55,7 +56,7 @@ export const deriveRelayList = makeDeriveItem(relayListsByPubkey, loadRelayList)
|
||||
// Outbox loader
|
||||
|
||||
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 allRelays = Router.get()
|
||||
.FromRelays(writeRelays)
|
||||
@@ -63,6 +64,10 @@ export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filt
|
||||
.limit(8)
|
||||
.getUrls()
|
||||
|
||||
if (isPlainReplaceableKind(kind)) {
|
||||
filters[0].limit = 1
|
||||
}
|
||||
|
||||
for (const relays of chunk(2, allRelays)) {
|
||||
const events = await load({filters, relays})
|
||||
|
||||
@@ -73,7 +78,7 @@ export const loadUsingOutbox = async (kind: number, pubkey: string, filter: Filt
|
||||
}
|
||||
|
||||
export const makeOutboxLoader =
|
||||
(kind: number, filter: Filter = {}) =>
|
||||
(kind: number, filter: Filter = {}, limit = 1) =>
|
||||
async (pubkey: string, relayHints: string[] = []) => {
|
||||
const filters = [{...filter, kinds: [kind], authors: [pubkey]}]
|
||||
const relays = Router.get().FromRelays(relayHints).getUrls()
|
||||
|
||||
@@ -64,7 +64,7 @@ export const requestPage = async ({
|
||||
onEvent,
|
||||
relays,
|
||||
filters,
|
||||
threshold: 0.8,
|
||||
threshold: 0.5,
|
||||
autoClose,
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -57,6 +57,26 @@ export const always =
|
||||
*/
|
||||
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
|
||||
* @param a - First value
|
||||
|
||||
Reference in New Issue
Block a user