Remove default throttle on event stores, add indexer relays to router
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import {first, switcher, throttleWithValue, clamp, last, splitAt, identity, sortBy, uniq, shuffle, pushToMapKey} from '@welshman/lib'
|
||||
import {Tags, getFilterId, unionFilters, isShareableRelayUrl, isCommunityAddress, isGroupAddress, isContextAddress} from '@welshman/util'
|
||||
import {
|
||||
intersection, first, switcher, throttleWithValue, clamp, last, splitAt, identity, sortBy, uniq, shuffle,
|
||||
pushToMapKey,
|
||||
} from '@welshman/lib'
|
||||
import {
|
||||
Tags, getFilterId, unionFilters, isShareableRelayUrl, isCommunityAddress, isGroupAddress, isContextAddress,
|
||||
PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS,
|
||||
} from '@welshman/util'
|
||||
import type {TrustedEvent, Filter} from '@welshman/util'
|
||||
import {NetworkContext, ConnectionStatus} from '@welshman/net'
|
||||
import {AppContext} from './core'
|
||||
@@ -7,6 +13,14 @@ import {pubkey} from './session'
|
||||
import {relaySelectionsByPubkey, getReadRelayUrls, getWriteRelayUrls, getRelayUrls} from './relaySelections'
|
||||
import {relays, relaysByUrl} from './relays'
|
||||
|
||||
export const INDEXED_KINDS = [PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS]
|
||||
|
||||
export const INDEXER_RELAYS = [
|
||||
'wss://purplepag.es/',
|
||||
'wss://relay.damus.io/',
|
||||
'wss://relay.nostr.band/',
|
||||
]
|
||||
|
||||
export enum RelayMode {
|
||||
Read = "read",
|
||||
Write = "write",
|
||||
@@ -48,6 +62,12 @@ export type RouterOptions = {
|
||||
*/
|
||||
getFallbackRelays: () => string[]
|
||||
|
||||
/**
|
||||
* Retrieves relays that index profiles and relay selections.
|
||||
* @returns An array of relay URLs as strings.
|
||||
*/
|
||||
getIndexerRelays?: () => string[]
|
||||
|
||||
/**
|
||||
* Retrieves relays likely to support NIP-50 search.
|
||||
* @returns An array of relay URLs as strings.
|
||||
@@ -419,6 +439,8 @@ export const getPubkeyRelays = (pubkey: string, mode?: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const getIndexerRelays = () => INDEXER_RELAYS
|
||||
|
||||
export const getFallbackRelays = throttleWithValue(300, () =>
|
||||
relays.get().filter(r => getRelayQuality(r.url) >= 0.5).map(r => r.url)
|
||||
)
|
||||
@@ -430,6 +452,7 @@ export const getSearchRelays = throttleWithValue(300, () =>
|
||||
export const makeRouter = (options: Partial<RouterOptions> = {}) =>
|
||||
new Router({
|
||||
getPubkeyRelays,
|
||||
getIndexerRelays,
|
||||
getFallbackRelays,
|
||||
getSearchRelays,
|
||||
getRelayQuality,
|
||||
@@ -463,6 +486,15 @@ export const getFilterSelectionsForSearch = (filter: Filter) => {
|
||||
return [makeFilterSelection(id, filter, scenario)]
|
||||
}
|
||||
|
||||
export const getFilterSelectionsForIndexedKinds = (filter: Filter) => {
|
||||
const kinds = intersection(INDEXED_KINDS, filter.kinds!)
|
||||
const id = getFilterId({...filter, kinds})
|
||||
const relays = AppContext.router.options.getIndexerRelays?.() || []
|
||||
const scenario = AppContext.router.product([id], relays)
|
||||
|
||||
return [makeFilterSelection(id, filter, scenario)]
|
||||
}
|
||||
|
||||
export const getFilterSelectionsForContext = (filter: Filter) => {
|
||||
const filterSelections = []
|
||||
const contexts = filter["#a"].filter(isContextAddress)
|
||||
@@ -536,6 +568,10 @@ export const getFilterSelections = (filters: Filter[]): RelayFilters[] => {
|
||||
addSelections(getFilterSelectionsForSearch(filter))
|
||||
}
|
||||
|
||||
if (filter.kinds?.some(k => INDEXED_KINDS.includes(k))) {
|
||||
addSelections(getFilterSelectionsForIndexedKinds(filter))
|
||||
}
|
||||
|
||||
if (filter["#a"]?.some(isContextAddress)) {
|
||||
addSelections(getFilterSelectionsForContext(filter))
|
||||
}
|
||||
|
||||
@@ -100,13 +100,13 @@ export const throttled = <T>(delay: number, store: Readable<T>) =>
|
||||
export const createEventStore = (repository: Repository): Writable<TrustedEvent[]> => {
|
||||
let subs: Subscriber<TrustedEvent[]>[] = []
|
||||
|
||||
const onUpdate = throttle(300, () => {
|
||||
const onUpdate = () => {
|
||||
const $events = repository.dump()
|
||||
|
||||
for (const sub of subs) {
|
||||
sub($events)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
set: (events: TrustedEvent[]) => repository.load(events),
|
||||
@@ -135,7 +135,7 @@ export const deriveEventsMapped = <T>(repository: Repository, {
|
||||
filters,
|
||||
eventToItem,
|
||||
itemToEvent,
|
||||
throttle = 300,
|
||||
throttle = 0,
|
||||
includeDeleted = false,
|
||||
}: {
|
||||
filters: Filter[]
|
||||
|
||||
Reference in New Issue
Block a user