mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-07-30 19:26:14 +00:00
relay.countWithHLL()
This commit is contained in:
+11
-4
@@ -341,9 +341,16 @@ export class AbstractRelay {
|
||||
}
|
||||
|
||||
public async count(filters: Filter[], params: { id?: string | null }): Promise<number> {
|
||||
return (await this.countWithHLL(filters, params)).count
|
||||
}
|
||||
|
||||
public async countWithHLL(
|
||||
filters: Filter[],
|
||||
params: { id?: string | null },
|
||||
): Promise<{ count: number; hll?: string }> {
|
||||
this.serial++
|
||||
const id = params?.id || 'count:' + this.serial
|
||||
const ret = new Promise<number>((resolve, reject) => {
|
||||
const ret = new Promise<{ count: number; hll?: string }>((resolve, reject) => {
|
||||
this.openCountRequests.set(id, { resolve, reject })
|
||||
})
|
||||
try {
|
||||
@@ -459,10 +466,10 @@ export class AbstractRelay {
|
||||
}
|
||||
case 'COUNT': {
|
||||
const id: string = data[1]
|
||||
const payload = data[2] as { count: number }
|
||||
const payload = data[2] as { count: number; hll?: string }
|
||||
const cr = this.openCountRequests.get(id) as CountResolver
|
||||
if (cr) {
|
||||
cr.resolve(payload.count)
|
||||
cr.resolve(payload)
|
||||
this.openCountRequests.delete(id)
|
||||
}
|
||||
return
|
||||
@@ -626,7 +633,7 @@ export type SubscriptionParams = {
|
||||
}
|
||||
|
||||
export type CountResolver = {
|
||||
resolve: (count: number) => void
|
||||
resolve: (payload: { count: number; hll?: string }) => void
|
||||
reject: (err: Error) => void
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user