feat: web (#6)

This commit is contained in:
Cody Tseng 2024-11-16 15:44:37 +08:00 committed by GitHub
parent ab667afc30
commit 26c2512d61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
60 changed files with 937 additions and 547 deletions

View file

@ -23,21 +23,21 @@ class ClientService {
private relayUrls: string[] = BIG_RELAY_URLS
private initPromise!: Promise<void>
private eventCache = new LRUCache<string, Promise<NEvent | undefined>>({
private eventByFilterCache = new LRUCache<string, Promise<NEvent | undefined>>({
max: 10000,
fetchMethod: async (filterStr) => {
const events = await this.fetchEvents(
BIG_RELAY_URLS.concat(this.relayUrls),
JSON.parse(filterStr)
)
events.forEach((event) => this.addEventToCache(event))
return events.sort((a, b) => b.created_at - a.created_at)[0]
}
})
private eventByIdCache = new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 })
private eventDataloader = new DataLoader<string, NEvent | undefined>(
this.eventBatchLoadFn.bind(this),
{
cacheMap: new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 })
}
{ cacheMap: this.eventByIdCache }
)
private profileDataloader = new DataLoader<string, TProfile | undefined>(
this.profileBatchLoadFn.bind(this),
@ -126,13 +126,17 @@ class ClientService {
}
async fetchEventByFilter(filter: Filter) {
return this.eventCache.fetch(JSON.stringify({ ...filter, limit: 1 }))
return this.eventByFilterCache.fetch(JSON.stringify({ ...filter, limit: 1 }))
}
async fetchEventById(id: string): Promise<NEvent | undefined> {
return this.eventDataloader.load(id)
}
addEventToCache(event: NEvent) {
this.eventByIdCache.set(event.id, Promise.resolve(event))
}
async fetchProfile(pubkey: string): Promise<TProfile | undefined> {
return this.profileDataloader.load(pubkey)
}