Remove indexer from hints, add indexBy
This commit is contained in:
@@ -230,6 +230,16 @@ export const groupBy = <T, K>(f: (x: T) => K, xs: T[]) => {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const indexBy = <T, K>(f: (x: T) => K, xs: T[]) => {
|
||||||
|
const r = new Map<K, T>()
|
||||||
|
|
||||||
|
for (const x of xs) {
|
||||||
|
r.set(f(x), x)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
export const sample = <T>(n: number, xs: T[]) => {
|
export const sample = <T>(n: number, xs: T[]) => {
|
||||||
const result: T[] = []
|
const result: T[] = []
|
||||||
const limit = Math.min(n, xs.length)
|
const limit = Math.min(n, xs.length)
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ export const PRODUCT_SOLD_AS_AUCTION = 30020
|
|||||||
export const WIKI = 30818
|
export const WIKI = 30818
|
||||||
export const LONG_FORM = 30023
|
export const LONG_FORM = 30023
|
||||||
export const LONG_FORM_DRAFT = 30024
|
export const LONG_FORM_DRAFT = 30024
|
||||||
export const APPLICATION = 30078
|
export const APP_DATA = 30078
|
||||||
export const LIVE_EVENT = 30311
|
export const LIVE_EVENT = 30311
|
||||||
export const STATUS = 30315
|
export const STATUS = 30315
|
||||||
export const CLASSIFIED = 30402
|
export const CLASSIFIED = 30402
|
||||||
|
|||||||
@@ -31,11 +31,11 @@ export class Repository extends Emitter {
|
|||||||
|
|
||||||
// Dump/load/clear
|
// Dump/load/clear
|
||||||
|
|
||||||
dump() {
|
dump = () => {
|
||||||
return Array.from(this.eventsById.values())
|
return Array.from(this.eventsById.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
async load(events: TrustedEvent[], chunkSize = 1000) {
|
load = async (events: TrustedEvent[], chunkSize = 1000) => {
|
||||||
this.clear()
|
this.clear()
|
||||||
|
|
||||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
for (const eventsChunk of chunk(chunkSize, events)) {
|
||||||
@@ -49,7 +49,7 @@ export class Repository extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear = () => {
|
||||||
this.eventsById.clear()
|
this.eventsById.clear()
|
||||||
this.eventsByAddress.clear()
|
this.eventsByAddress.clear()
|
||||||
this.eventsByTag.clear()
|
this.eventsByTag.clear()
|
||||||
@@ -74,13 +74,13 @@ export class Repository extends Emitter {
|
|||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
getEvent(idOrAddress: string) {
|
getEvent = (idOrAddress: string) => {
|
||||||
return idOrAddress.includes(':')
|
return idOrAddress.includes(':')
|
||||||
? this.eventsByAddress.get(idOrAddress)
|
? this.eventsByAddress.get(idOrAddress)
|
||||||
: this.eventsById.get(idOrAddress)
|
: this.eventsById.get(idOrAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasEvent(event: TrustedEvent) {
|
hasEvent = (event: TrustedEvent) => {
|
||||||
const duplicate = (
|
const duplicate = (
|
||||||
this.eventsById.get(event.id) ||
|
this.eventsById.get(event.id) ||
|
||||||
this.eventsByAddress.get(getAddress(event))
|
this.eventsByAddress.get(getAddress(event))
|
||||||
@@ -89,7 +89,7 @@ export class Repository extends Emitter {
|
|||||||
return duplicate && duplicate.created_at >= event.created_at
|
return duplicate && duplicate.created_at >= event.created_at
|
||||||
}
|
}
|
||||||
|
|
||||||
query(filters: Filter[], {includeDeleted = false} = {}) {
|
query = (filters: Filter[], {includeDeleted = false} = {}) => {
|
||||||
const result: TrustedEvent[][] = []
|
const result: TrustedEvent[][] = []
|
||||||
for (let filter of filters) {
|
for (let filter of filters) {
|
||||||
let events: TrustedEvent[] = Array.from(this.eventsById.values())
|
let events: TrustedEvent[] = Array.from(this.eventsById.values())
|
||||||
@@ -144,7 +144,7 @@ export class Repository extends Emitter {
|
|||||||
return uniq(flatten(result))
|
return uniq(flatten(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(event: TrustedEvent) {
|
publish = (event: TrustedEvent) => {
|
||||||
if (!isTrustedEvent(event)) {
|
if (!isTrustedEvent(event)) {
|
||||||
throw new Error("Invalid event published to Repository", event)
|
throw new Error("Invalid event published to Repository", event)
|
||||||
}
|
}
|
||||||
@@ -204,7 +204,7 @@ export class Repository extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isDeleted(event: TrustedEvent) {
|
isDeleted = (event: TrustedEvent) => {
|
||||||
const deletedAt = (
|
const deletedAt = (
|
||||||
this.deletes.get(event.id) ||
|
this.deletes.get(event.id) ||
|
||||||
this.deletes.get(getAddress(event)) ||
|
this.deletes.get(getAddress(event)) ||
|
||||||
|
|||||||
@@ -45,12 +45,6 @@ export type RouterOptions = {
|
|||||||
*/
|
*/
|
||||||
getFallbackRelays: () => string[]
|
getFallbackRelays: () => string[]
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves relays likely to return results for kind 0, 3, and 10002.
|
|
||||||
* @returns An array of relay URLs as strings.
|
|
||||||
*/
|
|
||||||
getIndexerRelays?: () => string[]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves relays likely to support NIP-50 search.
|
* Retrieves relays likely to support NIP-50 search.
|
||||||
* @returns An array of relay URLs as strings.
|
* @returns An array of relay URLs as strings.
|
||||||
@@ -263,9 +257,6 @@ export class Router {
|
|||||||
Search = (term: string, relays: string[] = []) =>
|
Search = (term: string, relays: string[] = []) =>
|
||||||
this.product([term], uniq(relays.concat(this.options.getSearchRelays?.() || [])))
|
this.product([term], uniq(relays.concat(this.options.getSearchRelays?.() || [])))
|
||||||
|
|
||||||
Indexers = (relays: string[] = []) =>
|
|
||||||
this.fromRelays(uniq(relays.concat(this.options.getIndexerRelays?.() || [])))
|
|
||||||
|
|
||||||
// Fallback policies
|
// Fallback policies
|
||||||
|
|
||||||
addNoFallbacks = (count: number, redundancy: number) => 0
|
addNoFallbacks = (count: number, redundancy: number) => 0
|
||||||
|
|||||||
Reference in New Issue
Block a user