Only notify about events that were actually loaded on initial repository load

This commit is contained in:
Jon Staab
2024-07-19 11:44:58 -07:00
parent 68ee77540f
commit f6b10ee7b6
6 changed files with 20 additions and 16 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
"dependencies": {
"@welshman/lib": "0.0.11",
"@welshman/net": "0.0.14",
"@welshman/util": "0.0.19",
"@welshman/util": "0.0.20",
"nostr-tools": "^2.7.0"
}
}
+1 -1
View File
@@ -31,6 +31,6 @@
"typescript": "~5.1.6"
},
"dependencies": {
"@welshman/util": "0.0.19"
"@welshman/util": "0.0.20"
}
}
+1 -1
View File
@@ -32,7 +32,7 @@
},
"dependencies": {
"@welshman/lib": "0.0.11",
"@welshman/util": "0.0.19",
"@welshman/util": "0.0.20",
"isomorphic-ws": "^5.0.0",
"ws": "^8.16.0"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/util",
"version": "0.0.19",
"version": "0.0.20",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of nostr-related utilities.",
+12 -8
View File
@@ -28,9 +28,13 @@ export class Repository extends Emitter {
load = async (events: TrustedEvent[], chunkSize = 1000) => {
this.clear()
const added = []
for (const eventsChunk of chunk(chunkSize, events)) {
for (const event of eventsChunk) {
this.publish(event, {shouldNotify: false})
if (this.publish(event, {shouldNotify: false})) {
added.push(event)
}
}
if (eventsChunk.length === chunkSize) {
@@ -38,11 +42,9 @@ export class Repository extends Emitter {
}
}
const removed = new Set(this.deletes.keys())
this.emit('update', {
added: events,
removed: new Set(this.deletes.keys()),
})
this.emit('update', {added, removed})
}
clear = () => {
@@ -131,14 +133,14 @@ export class Repository extends Emitter {
return uniq(flatten(result))
}
publish = (event: TrustedEvent, {shouldNotify = true} = {}) => {
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
if (!isTrustedEvent(event)) {
throw new Error("Invalid event published to Repository", event)
}
// 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
return false
}
const removed = new Set<string>()
@@ -148,7 +150,7 @@ export class Repository extends Emitter {
if (duplicate) {
// If our event is older than the duplicate, we're done
if (event.created_at <= duplicate.created_at) {
return
return false
}
// If our event is newer than what it's replacing, delete the old version
@@ -197,6 +199,8 @@ export class Repository extends Emitter {
if (shouldNotify) {
this.emit('update', {added: [event], removed})
}
return true
}
isDeleted = (event: TrustedEvent) =>