Only notify about events that were actually loaded on initial repository load
This commit is contained in:
Generated
+4
-4
@@ -3097,7 +3097,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"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -3111,7 +3111,7 @@
|
||||
"version": "0.0.12",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@welshman/util": "0.0.19"
|
||||
"@welshman/util": "0.0.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gts": "^5.0.1",
|
||||
@@ -3149,7 +3149,7 @@
|
||||
"license": "MIT",
|
||||
"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"
|
||||
},
|
||||
@@ -3161,7 +3161,7 @@
|
||||
},
|
||||
"packages/util": {
|
||||
"name": "@welshman/util",
|
||||
"version": "0.0.19",
|
||||
"version": "0.0.20",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@welshman/lib": "0.0.11",
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,6 @@
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@welshman/util": "0.0.19"
|
||||
"@welshman/util": "0.0.20"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,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.",
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in New Issue
Block a user