applesauce-core
Advanced tools
Comparing version 0.0.0-next-20241122170522 to 0.0.0-next-20241125223343
@@ -9,2 +9,6 @@ import { Filter, NostrEvent } from "nostr-tools"; | ||
add(event: NostrEvent, fromRelay?: string): import("nostr-tools").Event; | ||
protected deletedIds: Set<string>; | ||
protected deletedCoords: Map<string, number>; | ||
protected handleDeleteEvent(deleteEvent: NostrEvent): void; | ||
protected checkDeleted(event: NostrEvent): boolean; | ||
/** Add an event to the store and notifies all subscribes it has updated */ | ||
@@ -11,0 +15,0 @@ update(event: NostrEvent): import("nostr-tools").Event; |
@@ -0,1 +1,2 @@ | ||
import { kinds } from "nostr-tools"; | ||
import { insertEventIntoDescendingList } from "nostr-tools/utils"; | ||
@@ -7,2 +8,4 @@ import { Observable } from "rxjs"; | ||
import { addSeenRelay } from "../helpers/relays.js"; | ||
import { getDeleteIds } from "../helpers/delete.js"; | ||
import { isParameterizedReplaceableKind } from "nostr-tools/kinds"; | ||
export class EventStore { | ||
@@ -15,2 +18,6 @@ database; | ||
add(event, fromRelay) { | ||
if (event.kind === kinds.EventDeletion) | ||
this.handleDeleteEvent(event); | ||
if (this.checkDeleted(event)) | ||
return event; | ||
const inserted = this.database.addEvent(event); | ||
@@ -21,2 +28,32 @@ if (fromRelay) | ||
} | ||
deletedIds = new Set(); | ||
deletedCoords = new Map(); | ||
handleDeleteEvent(deleteEvent) { | ||
const ids = getDeleteIds(deleteEvent); | ||
for (const id of ids) { | ||
this.deletedIds.add(id); | ||
// remove deleted events in the database | ||
const event = this.database.getEvent(id); | ||
if (event) | ||
this.database.deleteEvent(event); | ||
} | ||
const coords = getDeleteIds(deleteEvent); | ||
for (const coord of coords) { | ||
this.deletedCoords.set(coord, Math.max(this.deletedCoords.get(coord) ?? 0, deleteEvent.created_at)); | ||
// remove deleted events in the database | ||
const event = this.database.getEvent(coord); | ||
if (event && event.created_at < deleteEvent.created_at) | ||
this.database.deleteEvent(event); | ||
} | ||
} | ||
checkDeleted(event) { | ||
if (this.deletedIds.has(event.id)) | ||
return true; | ||
if (isParameterizedReplaceableKind(event.kind)) { | ||
const deleted = this.deletedCoords.get(getEventUID(event)); | ||
if (deleted) | ||
return deleted > event.created_at; | ||
} | ||
return false; | ||
} | ||
/** Add an event to the store and notifies all subscribes it has updated */ | ||
@@ -23,0 +60,0 @@ update(event) { |
@@ -19,1 +19,2 @@ export * from "./profile.js"; | ||
export * from "./lnurl.js"; | ||
export * from "./delete.js"; |
@@ -19,1 +19,2 @@ export * from "./profile.js"; | ||
export * from "./lnurl.js"; | ||
export * from "./delete.js"; |
{ | ||
"name": "applesauce-core", | ||
"version": "0.0.0-next-20241122170522", | ||
"version": "0.0.0-next-20241125223343", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
132467
129
3364