Tweak duplicate handling in repository

This commit is contained in:
Jon Staab
2024-07-18 16:40:24 -07:00
parent b9494e1787
commit f5ff16a5c0
6 changed files with 19 additions and 19 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/util",
"version": "0.0.17",
"version": "0.0.18",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of nostr-related utilities.",
+11 -11
View File
@@ -136,21 +136,21 @@ export class Repository extends Emitter {
throw new Error("Invalid event published to Repository", event)
}
const address = getAddress(event)
const duplicate = (
this.eventsById.get(event.id) ||
this.eventsByAddress.get(address)
)
// If our duplicate is newer than the event we're adding, we're done
if (duplicate && duplicate.created_at >= event.created_at) {
this.deletes.set(event.id, duplicate.created_at)
// If we've already seen this event, we're done
if (this.eventsById.get(event.id)) {
return
}
// Delete our duplicate
const address = getAddress(event)
const duplicate = this.eventsByAddress.get(address)
if (duplicate) {
// If our event is older than the duplicate, we're done
if (event.created_at <= duplicate.created_at) {
return
}
// If our event is newer than what it's replacing, delete the old version
this.deletes.set(duplicate.id, event.created_at)
}