Fix reporting duplicate events from load

This commit is contained in:
Jon Staab
2024-10-22 10:16:52 -07:00
parent ba4deb8ad5
commit 672a679e01
+17 -4
View File
@@ -32,8 +32,7 @@ export class Repository<E extends HashedEvent = TrustedEvent> extends Emitter {
} }
load = (events: E[], chunkSize = 1000) => { load = (events: E[], chunkSize = 1000) => {
const added = [] const stale = new Set(this.eventsById.keys())
const removed = new Set(this.eventsById.keys())
this.eventsById.clear() this.eventsById.clear()
this.eventsByWrap.clear() this.eventsByWrap.clear()
@@ -43,15 +42,29 @@ export class Repository<E extends HashedEvent = TrustedEvent> extends Emitter {
this.eventsByAuthor.clear() this.eventsByAuthor.clear()
this.deletes.clear() this.deletes.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) {
if (this.publish(event, {shouldNotify: false})) { if (this.publish(event, {shouldNotify: false})) {
added.push(event) // Don't send duplicate events to subscribers
removed.delete(event.id) if (!stale.has(event.id)) {
added.push(event)
}
} }
} }
} }
const removed = new Set<string>()
// Anything we had before clearing the repository has been removed
for (const id of stale) {
if (!this.eventsById.has(id)) {
removed.add(id)
}
}
// Anything removed via delete or replace has been removed
for (const id of this.deletes.keys()) { for (const id of this.deletes.keys()) {
removed.add(id) removed.add(id)
} }