Handle empty tags in repository, tweak dvm types

This commit is contained in:
Jon Staab
2024-07-19 09:14:04 -07:00
parent f5ff16a5c0
commit 68ee77540f
7 changed files with 22 additions and 24 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/dvm",
"version": "0.0.1",
"version": "0.0.2",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of utilities for building nostr DVMs.",
@@ -33,7 +33,7 @@
"dependencies": {
"@welshman/lib": "0.0.11",
"@welshman/net": "0.0.14",
"@welshman/util": "0.0.18",
"@welshman/util": "0.0.19",
"nostr-tools": "^2.7.0"
}
}
+4 -3
View File
@@ -16,13 +16,14 @@ export type DVMRequestOptions = {
reportProgress?: boolean
}
export type DVMRequest = DVMRequestOptions & {
export type DVMRequest = {
request: DVMRequestOptions
emitter: Emitter,
sub: Subscription
pub: Publish
}
export const makeDvmRequest = (request: DVMRequest) => {
export const makeDvmRequest = (request: DVMRequestOptions) => {
const emitter = new Emitter()
const {event, relays, timeout = 30_000, autoClose = true, reportProgress = true} = request
const kind = event.kind + 1000
@@ -43,5 +44,5 @@ export const makeDvmRequest = (request: DVMRequest) => {
}
})
return {request, emitter, sub, pub}
return {request, emitter, sub, pub} as DVMRequest
}
+1 -1
View File
@@ -31,6 +31,6 @@
"typescript": "~5.1.6"
},
"dependencies": {
"@welshman/util": "0.0.18"
"@welshman/util": "0.0.19"
}
}
+1 -1
View File
@@ -32,7 +32,7 @@
},
"dependencies": {
"@welshman/lib": "0.0.11",
"@welshman/util": "0.0.18",
"@welshman/util": "0.0.19",
"isomorphic-ws": "^5.0.0",
"ws": "^8.16.0"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/util",
"version": "0.0.18",
"version": "0.0.19",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of nostr-related utilities.",
+8 -11
View File
@@ -136,11 +136,12 @@ export class Repository extends Emitter {
throw new Error("Invalid event published to Repository", event)
}
// If we've already seen this event, we're done
if (this.eventsById.get(event.id)) {
// If we've already seen this event, or it's been deleted, we're done
if (this.eventsById.get(event.id) || this.isDeleted(event)) {
return
}
const removed = new Set<string>()
const address = getAddress(event)
const duplicate = this.eventsByAddress.get(address)
@@ -152,6 +153,9 @@ export class Repository extends Emitter {
// If our event is newer than what it's replacing, delete the old version
this.deletes.set(duplicate.id, event.created_at)
// Notify listeners that it's been removed
removed.add(duplicate.id)
}
// Add our new event by id
@@ -171,12 +175,9 @@ export class Repository extends Emitter {
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate)
// Keep track of deleted events to notify about
const removed = new Set<string>()
// Update our tag indexes
for (const tag of event.tags) {
if (tag[0].length === 1) {
if (tag[0]?.length === 1) {
this._updateIndex(this.eventsByTag, tag.slice(0, 2).join(':'), event, duplicate)
// If this is a delete event, the tag value is an id or address. Track when it was
@@ -193,12 +194,8 @@ export class Repository extends Emitter {
}
}
if (duplicate) {
removed.add(duplicate.id)
}
if (shouldNotify) {
this.emit('update', {added: this.isDeleted(event) ? [] : [event], removed})
this.emit('update', {added: [event], removed})
}
}