Make repository.query sync

This commit is contained in:
Jon Staab
2024-05-16 15:07:02 -07:00
parent 5d3265c15a
commit 42b3b8b5e7
10 changed files with 36 additions and 36 deletions
+8 -8
View File
@@ -3059,10 +3059,10 @@
}, },
"packages/feeds": { "packages/feeds": {
"name": "@welshman/feeds", "name": "@welshman/feeds",
"version": "0.0.4", "version": "0.0.5",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/util": "0.0.5" "@welshman/util": "0.0.6"
}, },
"devDependencies": { "devDependencies": {
"gts": "^5.0.1", "gts": "^5.0.1",
@@ -3072,7 +3072,7 @@
}, },
"packages/lib": { "packages/lib": {
"name": "@welshman/lib", "name": "@welshman/lib",
"version": "0.0.4", "version": "0.0.5",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@scure/base": "^1.1.6", "@scure/base": "^1.1.6",
@@ -3096,11 +3096,11 @@
}, },
"packages/net": { "packages/net": {
"name": "@welshman/net", "name": "@welshman/net",
"version": "0.0.4", "version": "0.0.5",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.4", "@welshman/lib": "0.0.5",
"@welshman/util": "0.0.5", "@welshman/util": "0.0.6",
"isomorphic-ws": "^5.0.0", "isomorphic-ws": "^5.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
}, },
@@ -3112,10 +3112,10 @@
}, },
"packages/util": { "packages/util": {
"name": "@welshman/util", "name": "@welshman/util",
"version": "0.0.5", "version": "0.0.6",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.4", "@welshman/lib": "0.0.5",
"nostr-tools": "^2.3.2" "nostr-tools": "^2.3.2"
}, },
"devDependencies": { "devDependencies": {
-5
View File
@@ -39,11 +39,6 @@ export class FeedLoader<E extends TrustedEvent> {
} }
async _getRequestsLoader(requests: RequestItem[], {onEvent, onExhausted}: LoadOpts<E>) { 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 seen = new Set()
const exhausted = new Set() const exhausted = new Set()
const loaders = await Promise.all( const loaders = await Promise.all(
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/feeds", "name": "@welshman/feeds",
"version": "0.0.4", "version": "0.0.5",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "Utilities for building dynamic nostr feeds.", "description": "Utilities for building dynamic nostr feeds.",
@@ -31,6 +31,6 @@
"typescript": "~5.1.6" "typescript": "~5.1.6"
}, },
"dependencies": { "dependencies": {
"@welshman/util": "0.0.5" "@welshman/util": "0.0.6"
} }
} }
+7 -5
View File
@@ -186,17 +186,19 @@ export const uniqBy = <T>(f: (x: T) => any, xs: T[]) => {
export const sortBy = <T>(f: (x: T) => number, xs: T[]) => export const sortBy = <T>(f: (x: T) => number, xs: T[]) =>
xs.sort((a: T, b: T) => f(a) - f(b)) xs.sort((a: T, b: T) => f(a) - f(b))
export const groupBy = <T>(f: (x: T) => string, xs: T[]) => { export const groupBy = <T, K>(f: (x: T) => K, xs: T[]) => {
const r: Record<string, T[]> = {} const r = new Map<K, T[]>()
for (const x of xs) { for (const x of xs) {
const k = f(x) const k = f(x)
let v = r.get(k)
if (!r[k]) { if (!v) {
r[k] = [] v = []
r.set(k, v)
} }
r[k].push(x) v.push(x)
} }
return r return r
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/lib", "name": "@welshman/lib",
"version": "0.0.4", "version": "0.0.5",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "A collection of utilities.", "description": "A collection of utilities.",
+1 -1
View File
@@ -78,7 +78,7 @@ export const mergeSubscriptions = (subs: Subscription[]) => {
const completedRelays = new Set() const completedRelays = new Set()
const mergedSubscriptions = [] 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))) { for (const relay of uniq(group.flatMap((sub: Subscription) => sub.request.relays))) {
const abortedSubs = new Set() const abortedSubs = new Set()
const callerSubs = group.filter((sub: Subscription) => sub.request.relays.includes(relay)) const callerSubs = group.filter((sub: Subscription) => sub.request.relays.includes(relay))
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/net", "name": "@welshman/net",
"version": "0.0.4", "version": "0.0.5",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "Utilities for connecting with nostr relays.", "description": "Utilities for connecting with nostr relays.",
@@ -31,8 +31,8 @@
"typescript": "~5.1.6" "typescript": "~5.1.6"
}, },
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.4", "@welshman/lib": "0.0.5",
"@welshman/util": "0.0.5", "@welshman/util": "0.0.6",
"isomorphic-ws": "^5.0.0", "isomorphic-ws": "^5.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
} }
+1 -1
View File
@@ -79,7 +79,7 @@ export const calculateFilterGroup = ({since, until, limit, search, ...filter}: F
export const unionFilters = (filters: Filter[]) => { export const unionFilters = (filters: Filter[]) => {
const result = [] const result = []
for (const group of Object.values(groupBy(calculateFilterGroup, filters))) { for (const group of groupBy(calculateFilterGroup, filters).values()) {
const newFilter: Record<string, any> = {} const newFilter: Record<string, any> = {}
for (const k of Object.keys(group[0])) { for (const k of Object.keys(group[0])) {
+11 -8
View File
@@ -1,6 +1,6 @@
import {throttle} from 'throttle-debounce' import {throttle} from 'throttle-debounce'
import type {IReadable, Subscriber, Invalidator} from '@welshman/lib' 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 {DELETE} from './Kinds'
import {EPOCH, matchFilter, getIdFilters, matchFilters} from './Filters' import {EPOCH, matchFilter, getIdFilters, matchFilters} from './Filters'
import {isReplaceable, isTrustedEvent} from './Events' import {isReplaceable, isTrustedEvent} from './Events'
@@ -67,7 +67,7 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
} }
filter(filters: Filter[], {includeDeleted = false} = {}) { filter(filters: Filter[], {includeDeleted = false} = {}) {
const getValue = () => Array.from(this.query(filters, {includeDeleted})) const getValue = () => this.query(filters, {includeDeleted})
return customStore<TrustedEvent[]>({ return customStore<TrustedEvent[]>({
get: getValue, get: getValue,
@@ -132,7 +132,8 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
return this.filter(getIdFilters([idOrAddress]), {includeDeleted: true}).derived(first) 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) { for (let filter of filters) {
let events: TrustedEvent[] = Array.from(this.eventsById.values()) 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)) { 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 break
} }
@@ -177,11 +177,14 @@ export class Repository extends Emitter implements IReadable<TrustedEvent[]> {
} }
if (matchFilter(filter, event)) { if (matchFilter(filter, event)) {
yield event chunk.push(event)
i += 1
} }
} }
result.push(chunk)
} }
return uniq(flatten(result))
} }
publish(event: TrustedEvent) { publish(event: TrustedEvent) {
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/util", "name": "@welshman/util",
"version": "0.0.5", "version": "0.0.6",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "A collection of nostr-related utilities.", "description": "A collection of nostr-related utilities.",
@@ -31,7 +31,7 @@
"typescript": "~5.1.6" "typescript": "~5.1.6"
}, },
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.4", "@welshman/lib": "0.0.5",
"nostr-tools": "^2.3.2" "nostr-tools": "^2.3.2"
} }
} }