Remove generics for event type

This commit is contained in:
Jon Staab
2024-08-13 09:30:55 -07:00
parent 0576c3c0e0
commit 5a63273b9d
18 changed files with 154 additions and 136 deletions
+8 -8
View File
@@ -1,12 +1,12 @@
import {uniq, identity, flatten, pushToMapKey, intersection, tryCatch, now} from '@welshman/lib'
import type {TrustedEvent, Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
import {Tags, intersectFilters, matchFilter, getAddress, getIdFilters, unionFilters} from '@welshman/util'
import type {CreatedAtItem, RequestItem, ListItem, LabelItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core'
import {getFeedArgs, feedsFromTags} from './utils'
import {FeedType} from './core'
export class FeedCompiler<E extends TrustedEvent> {
constructor(readonly options: FeedOptions<E>) {}
export class FeedCompiler {
constructor(readonly options: FeedOptions) {}
canCompile(feed: Feed): boolean {
switch(feed[0]) {
@@ -109,7 +109,7 @@ export class FeedCompiler<E extends TrustedEvent> {
items.map(({mappings, ...request}) =>
this.options.requestDVM({
...request,
onEvent: async (e: E) => {
onEvent: async (e: ExtensibleTrustedEvent) => {
const tags = Tags.wrap(await tryCatch(() => JSON.parse(e.content)) || [])
for (const feed of feedsFromTags(tags, mappings)) {
@@ -215,11 +215,11 @@ export class FeedCompiler<E extends TrustedEvent> {
async _compileLists(listItems: ListItem[]): Promise<RequestItem[]> {
const addresses = uniq(listItems.flatMap(({addresses}) => addresses))
const eventsByAddress = new Map<string, E>()
const eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
await this.options.request({
filters: getIdFilters(addresses),
onEvent: (e: E) => eventsByAddress.set(getAddress(e), e),
onEvent: (e: ExtensibleTrustedEvent) => eventsByAddress.set(getAddress(e), e),
})
const feeds = flatten(
@@ -246,14 +246,14 @@ export class FeedCompiler<E extends TrustedEvent> {
}
async _compileLabels(labelItems: LabelItem[]): Promise<RequestItem[]> {
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
await Promise.all(
labelItems.map(({mappings, relays, ...filter}) =>
this.options.request({
relays,
filters: [{kinds: [1985], ...filter}],
onEvent: (e: E) => events.push(e),
onEvent: (e: ExtensibleTrustedEvent) => events.push(e),
})
)
)
+8 -8
View File
@@ -1,4 +1,4 @@
import type {Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
export enum FeedType {
Address = "address",
@@ -109,8 +109,8 @@ export type RequestItem = {
filters?: Filter[]
}
export type RequestOpts<E> = RequestItem & {
onEvent: (event: E) => void
export type RequestOpts = RequestItem & {
onEvent: (event: ExtensibleTrustedEvent) => void
}
export type DVMRequest = {
@@ -119,13 +119,13 @@ export type DVMRequest = {
relays?: string[],
}
export type DVMOpts<E> = DVMRequest & {
onEvent: (event: E) => void
export type DVMOpts = DVMRequest & {
onEvent: (event: ExtensibleTrustedEvent) => void
}
export type FeedOptions<E> = {
request: (opts: RequestOpts<E>) => Promise<void>
requestDVM: (opts: DVMOpts<E>) => Promise<void>
export type FeedOptions = {
request: (opts: RequestOpts) => Promise<void>
requestDVM: (opts: DVMOpts) => Promise<void>
getPubkeysForScope: (scope: Scope) => string[]
getPubkeysForWOTRange: (minWOT: number, maxWOT: number) => string[]
}
+18 -18
View File
@@ -1,26 +1,26 @@
import {inc, max, min, now} from '@welshman/lib'
import type {TrustedEvent, Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
import {EPOCH, trimFilters, guessFilterDelta} from '@welshman/util'
import type {Feed, RequestItem, FeedOptions} from './core'
import {FeedType} from './core'
import {FeedCompiler} from './compiler'
export type LoadOpts<E> = {
onEvent?: (event: E) => void
export type LoadOpts = {
onEvent?: (event: ExtensibleTrustedEvent) => void
onExhausted?: () => void
useWindowing?: boolean
}
export type Loader = (limit: number) => Promise<void>
export class FeedLoader<E extends TrustedEvent> {
compiler: FeedCompiler<E>
export class FeedLoader {
compiler: FeedCompiler
constructor(readonly options: FeedOptions<E>) {
constructor(readonly options: FeedOptions) {
this.compiler = new FeedCompiler(options)
}
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts<E>) {
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts) {
if (this.compiler.canCompile([type, ...feed] as Feed)) {
return this.getRequestsLoader(await this.compiler.compile([type, ...feed] as Feed), loadOpts)
}
@@ -37,7 +37,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts<E>) {
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts) {
const seen = new Set()
const exhausted = new Set()
const loaders = await Promise.all(
@@ -64,7 +64,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts<E>) {
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts) {
// Make sure we have some kind of filter to send if we've been given an empty one, as happens with relay feeds
if (!filters || filters.length === 0) {
filters = [{}]
@@ -101,7 +101,7 @@ export class FeedLoader<E extends TrustedEvent> {
await this.options.request({
relays,
filters: trimFilters(requestFilters),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
count += 1
until = Math.min(until, event.created_at - 1)
onEvent?.(event)
@@ -127,17 +127,17 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const skip = new Set<string>()
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
const seen = new Set()
const loaders = await Promise.all(
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
if (i === 0) {
events.push(event)
} else {
@@ -172,17 +172,17 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const counts = new Map<string, number>()
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
const seen = new Set()
const loaders = await Promise.all(
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
events.push(event)
counts.set(event.id, inc(counts.get(event.id)))
},
@@ -214,7 +214,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const seen = new Set()
@@ -222,7 +222,7 @@ export class FeedLoader<E extends TrustedEvent> {
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
if (!seen.has(event.id)) {
onEvent?.(event)
seen.add(event.id)