import { Observable, Subject } from "rxjs"; import { NostrEvent } from "../helpers/event.js"; import { Filter } from "../helpers/filter.js"; import { AddressPointer, AddressPointerWithoutD, EventPointer } from "../helpers/pointers.js"; import { EventMemory } from "./event-memory.js"; import { EventModels } from "./event-models.js"; import { IAsyncDeleteManager, IAsyncEventDatabase, IAsyncEventStore, IExpirationManager } from "./interface.js"; export type AsyncEventStoreOptions = { /** Keep deleted events in the store */ keepDeleted?: boolean; /** Keep expired events in the store */ keepExpired?: boolean; /** Enable this to keep old versions of replaceable events */ keepOldVersions?: boolean; /** The database to use for storing events */ database: IAsyncEventDatabase; /** Custom {@link IAsyncDeleteManager} implementation */ deleteManager?: IAsyncDeleteManager; /** Custom {@link IExpirationManager} implementation */ expirationManager?: IExpirationManager; /** The method used to verify events */ verifyEvent?: (event: NostrEvent) => boolean; }; /** An async wrapper around an async event database that handles replaceable events, deletes, and models */ export declare class AsyncEventStore extends EventModels implements IAsyncEventStore { database: IAsyncEventDatabase; /** Optional memory database for ensuring single event instances */ memory: EventMemory; /** Manager for handling event deletions with authorization */ private deletes; /** Manager for handling event expirations */ private expiration; /** Enable this to keep old versions of replaceable events */ keepOldVersions: boolean; /** Keep expired events in the store */ keepExpired: boolean; /** Keep deleted events in the store */ keepDeleted: boolean; /** The method used to verify events */ private _verifyEventMethod?; /** Get the method used to verify events */ get verifyEvent(): undefined | ((event: NostrEvent) => boolean); /** Sets the method used to verify events */ set verifyEvent(method: undefined | ((event: NostrEvent) => boolean)); /** A stream of new events added to the store */ insert$: Subject; /** A stream of events that have been updated (Warning: this is a very noisy stream, use with caution) */ update$: Subject; /** A stream of events that have been removed */ remove$: Subject; /** * A method that will be called when an event isn't found in the store */ eventLoader?: (pointer: EventPointer | AddressPointer | AddressPointerWithoutD) => Observable | Promise; /** Internal subscriptions (delete + expiration managers) torn down on dispose */ private internalSubscriptions; constructor(options: AsyncEventStoreOptions); /** A method to add all events to memory to ensure there is only ever a single instance of an event */ private mapToMemory; /** Handle a delete event by pointer */ private handleDeleteNotification; /** Handle an expired event by id */ private handleExpiredNotification; /** * Adds an event to the store and update subscriptions * @returns The existing event or the event that was added, if it was ignored returns null */ add(event: NostrEvent, fromRelay?: string): Promise; /** Removes an event from the store and updates subscriptions */ remove(event: string | NostrEvent): Promise; /** Remove multiple events that match the given filters */ removeByFilters(filters: Filter | Filter[]): Promise; /** Add an event to the store and notifies all subscribes it has updated */ update(event: NostrEvent): Promise; /** Check if the store has an event by id */ hasEvent(id: string | EventPointer | AddressPointer | AddressPointerWithoutD): Promise; /** Get an event by id from the store */ getEvent(id: string | EventPointer | AddressPointer | AddressPointerWithoutD): Promise; /** Check if the store has a replaceable event */ hasReplaceable(kind: number, pubkey: string, d?: string): Promise; /** Gets the latest version of a replaceable event */ getReplaceable(kind: number, pubkey: string, identifier?: string): Promise; /** Returns all versions of a replaceable event */ getReplaceableHistory(kind: number, pubkey: string, identifier?: string): Promise; /** Get all events matching a filter */ getByFilters(filters: Filter | Filter[]): Promise; /** Returns a timeline of events that match filters */ getTimeline(filters: Filter | Filter[]): Promise; /** Passthrough method for the database.touch */ touch(event: NostrEvent): void; /** Increments the claim count on the event and touches it */ claim(event: NostrEvent): void; /** Checks if an event is claimed by anything */ isClaimed(event: NostrEvent): boolean; /** Decrements the claim count on an event */ removeClaim(event: NostrEvent): void; /** Removes all claims on an event */ clearClaim(event: NostrEvent): void; /** Pass through method for the database.unclaimed */ unclaimed(): Generator; /** Removes any event that is not being used by a subscription */ prune(limit?: number): number; /** * Tears down the store: disposes the attached event loader, completes the event streams, releases * model keep-warm timers, unsubscribes internal manager listeners, and cancels any pending expiration timer. * @note This is a terminal operation; the store should be discarded after calling it. */ dispose(): void; /** Allows the store to be used with the `using` keyword */ [Symbol.dispose](): void; }