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
+4 -4
View File
@@ -3097,7 +3097,7 @@
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.11", "@welshman/lib": "0.0.11",
"@welshman/net": "0.0.14", "@welshman/net": "0.0.14",
"@welshman/util": "0.0.19", "@welshman/util": "0.0.20",
"nostr-tools": "^2.7.0" "nostr-tools": "^2.7.0"
}, },
"devDependencies": { "devDependencies": {
@@ -3111,7 +3111,7 @@
"version": "0.0.12", "version": "0.0.12",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/util": "0.0.19" "@welshman/util": "0.0.20"
}, },
"devDependencies": { "devDependencies": {
"gts": "^5.0.1", "gts": "^5.0.1",
@@ -3149,7 +3149,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.11", "@welshman/lib": "0.0.11",
"@welshman/util": "0.0.19", "@welshman/util": "0.0.20",
"isomorphic-ws": "^5.0.0", "isomorphic-ws": "^5.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
}, },
@@ -3161,7 +3161,7 @@
}, },
"packages/util": { "packages/util": {
"name": "@welshman/util", "name": "@welshman/util",
"version": "0.0.19", "version": "0.0.20",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.11", "@welshman/lib": "0.0.11",
+1 -1
View File
@@ -33,7 +33,7 @@
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.11", "@welshman/lib": "0.0.11",
"@welshman/net": "0.0.14", "@welshman/net": "0.0.14",
"@welshman/util": "0.0.19", "@welshman/util": "0.0.20",
"nostr-tools": "^2.7.0" "nostr-tools": "^2.7.0"
} }
} }
+1 -1
View File
@@ -31,6 +31,6 @@
"typescript": "~5.1.6" "typescript": "~5.1.6"
}, },
"dependencies": { "dependencies": {
"@welshman/util": "0.0.19" "@welshman/util": "0.0.20"
} }
} }
+1 -1
View File
@@ -32,7 +32,7 @@
}, },
"dependencies": { "dependencies": {
"@welshman/lib": "0.0.11", "@welshman/lib": "0.0.11",
"@welshman/util": "0.0.19", "@welshman/util": "0.0.20",
"isomorphic-ws": "^5.0.0", "isomorphic-ws": "^5.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/util", "name": "@welshman/util",
"version": "0.0.19", "version": "0.0.20",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "A collection of nostr-related utilities.", "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) => { load = async (events: TrustedEvent[], chunkSize = 1000) => {
this.clear() this.clear()
const added = []
for (const eventsChunk of chunk(chunkSize, events)) { for (const eventsChunk of chunk(chunkSize, events)) {
for (const event of eventsChunk) { for (const event of eventsChunk) {
this.publish(event, {shouldNotify: false}) if (this.publish(event, {shouldNotify: false})) {
added.push(event)
}
} }
if (eventsChunk.length === chunkSize) { if (eventsChunk.length === chunkSize) {
@@ -38,11 +42,9 @@ export class Repository extends Emitter {
} }
} }
const removed = new Set(this.deletes.keys())
this.emit('update', { this.emit('update', {added, removed})
added: events,
removed: new Set(this.deletes.keys()),
})
} }
clear = () => { clear = () => {
@@ -131,14 +133,14 @@ export class Repository extends Emitter {
return uniq(flatten(result)) return uniq(flatten(result))
} }
publish = (event: TrustedEvent, {shouldNotify = true} = {}) => { publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
if (!isTrustedEvent(event)) { if (!isTrustedEvent(event)) {
throw new Error("Invalid event published to Repository", 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 we've already seen this event, or it's been deleted, we're done
if (this.eventsById.get(event.id) || this.isDeleted(event)) { if (this.eventsById.get(event.id) || this.isDeleted(event)) {
return return false
} }
const removed = new Set<string>() const removed = new Set<string>()
@@ -148,7 +150,7 @@ export class Repository extends Emitter {
if (duplicate) { if (duplicate) {
// If our event is older than the duplicate, we're done // If our event is older than the duplicate, we're done
if (event.created_at <= duplicate.created_at) { if (event.created_at <= duplicate.created_at) {
return return false
} }
// If our event is newer than what it's replacing, delete the old version // 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) { if (shouldNotify) {
this.emit('update', {added: [event], removed}) this.emit('update', {added: [event], removed})
} }
return true
} }
isDeleted = (event: TrustedEvent) => isDeleted = (event: TrustedEvent) =>