Make repository.query sync
This commit is contained in:
@@ -39,11 +39,6 @@ export class FeedLoader<E extends TrustedEvent> {
|
||||
}
|
||||
|
||||
async _getRequestsLoader(requests: RequestItem[], {onEvent, onExhausted}: LoadOpts<E>) {
|
||||
// Empty requests are not a no-op, they're a global feed
|
||||
if (requests.length === 0) {
|
||||
requests = [{}]
|
||||
}
|
||||
|
||||
const seen = new Set()
|
||||
const exhausted = new Set()
|
||||
const loaders = await Promise.all(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/feeds",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "Utilities for building dynamic nostr feeds.",
|
||||
@@ -31,6 +31,6 @@
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@welshman/util": "0.0.5"
|
||||
"@welshman/util": "0.0.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,17 +186,19 @@ export const uniqBy = <T>(f: (x: T) => any, xs: T[]) => {
|
||||
export const sortBy = <T>(f: (x: T) => number, xs: T[]) =>
|
||||
xs.sort((a: T, b: T) => f(a) - f(b))
|
||||
|
||||
export const groupBy = <T>(f: (x: T) => string, xs: T[]) => {
|
||||
const r: Record<string, T[]> = {}
|
||||
export const groupBy = <T, K>(f: (x: T) => K, xs: T[]) => {
|
||||
const r = new Map<K, T[]>()
|
||||
|
||||
for (const x of xs) {
|
||||
const k = f(x)
|
||||
let v = r.get(k)
|
||||
|
||||
if (!r[k]) {
|
||||
r[k] = []
|
||||
if (!v) {
|
||||
v = []
|
||||
r.set(k, v)
|
||||
}
|
||||
|
||||
r[k].push(x)
|
||||
v.push(x)
|
||||
}
|
||||
|
||||
return r
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/lib",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities.",
|
||||
|
||||
@@ -78,7 +78,7 @@ export const mergeSubscriptions = (subs: Subscription[]) => {
|
||||
const completedRelays = new Set()
|
||||
const mergedSubscriptions = []
|
||||
|
||||
for (const group of Object.values(groupBy(calculateSubscriptionGroup, subs))) {
|
||||
for (const group of groupBy(calculateSubscriptionGroup, subs).values()) {
|
||||
for (const relay of uniq(group.flatMap((sub: Subscription) => sub.request.relays))) {
|
||||
const abortedSubs = new Set()
|
||||
const callerSubs = group.filter((sub: Subscription) => sub.request.relays.includes(relay))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/net",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "Utilities for connecting with nostr relays.",
|
||||
@@ -31,8 +31,8 @@
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@welshman/lib": "0.0.4",
|
||||
"@welshman/util": "0.0.5",
|
||||
"@welshman/lib": "0.0.5",
|
||||
"@welshman/util": "0.0.6",
|
||||
"isomorphic-ws": "^5.0.0",
|
||||
"ws": "^8.16.0"
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ export const calculateFilterGroup = ({since, until, limit, search, ...filter}: F
|
||||
export const unionFilters = (filters: Filter[]) => {
|
||||
const result = []
|
||||
|
||||
for (const group of Object.values(groupBy(calculateFilterGroup, filters))) {
|
||||
for (const group of groupBy(calculateFilterGroup, filters).values()) {
|
||||
const newFilter: Record<string, any> = {}
|
||||
|
||||
for (const k of Object.keys(group[0])) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {throttle} from 'throttle-debounce'
|
||||
import type {IReadable, Subscriber, Invalidator} from '@welshman/lib'
|
||||
import {Derived, Emitter, sortBy, customStore, inc, first, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
||||
import {Derived, flatten, Emitter, sortBy, customStore, inc, first, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
||||
import {DELETE} from './Kinds'
|
||||
import {EPOCH, matchFilter, getIdFilters, matchFilters} from './Filters'
|
||||
import {isReplaceable, isTrustedEvent} from './Events'
|
||||
@@ -67,7 +67,7 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
|
||||
}
|
||||
|
||||
filter(filters: Filter[], {includeDeleted = false} = {}) {
|
||||
const getValue = () => Array.from(this.query(filters, {includeDeleted}))
|
||||
const getValue = () => this.query(filters, {includeDeleted})
|
||||
|
||||
return customStore<TrustedEvent[]>({
|
||||
get: getValue,
|
||||
@@ -132,7 +132,8 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
|
||||
return this.filter(getIdFilters([idOrAddress]), {includeDeleted: true}).derived(first)
|
||||
}
|
||||
|
||||
*query(filters: Filter[], {includeDeleted = false} = {}) {
|
||||
query(filters: Filter[], {includeDeleted = false} = {}) {
|
||||
const result: TrustedEvent[][] = []
|
||||
for (let filter of filters) {
|
||||
let events: TrustedEvent[] = Array.from(this.eventsById.values())
|
||||
|
||||
@@ -165,10 +166,9 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
|
||||
}
|
||||
}
|
||||
|
||||
let i = 0
|
||||
|
||||
const chunk: TrustedEvent[] = []
|
||||
for (const event of sortBy((e: TrustedEvent) => -e.created_at, events)) {
|
||||
if (filter.limit && i > filter.limit) {
|
||||
if (filter.limit && chunk.length >= filter.limit) {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -177,11 +177,14 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
|
||||
}
|
||||
|
||||
if (matchFilter(filter, event)) {
|
||||
yield event
|
||||
i += 1
|
||||
chunk.push(event)
|
||||
}
|
||||
}
|
||||
|
||||
result.push(chunk)
|
||||
}
|
||||
|
||||
return uniq(flatten(result))
|
||||
}
|
||||
|
||||
publish(event: TrustedEvent) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/util",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of nostr-related utilities.",
|
||||
@@ -31,7 +31,7 @@
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@welshman/lib": "0.0.4",
|
||||
"@welshman/lib": "0.0.5",
|
||||
"nostr-tools": "^2.3.2"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user