import type { Collection } from "@valkyr/db"; import type { Snapshot, SnapshotsProvider } from "../../../types/adapter.ts"; export class BrowserSnapshotsProvider implements SnapshotsProvider { constructor(readonly snapshots: Collection) {} /** * Add snapshot state under given reducer stream. * * @param name - Name of the reducer the snapshot is attached to. * @param stream - Stream the snapshot is attached to. * @param cursor - Cursor timestamp for the last event used in the snapshot. * @param state - State of the reduced events. */ async insert(name: string, stream: string, cursor: string, state: Record): Promise { await this.snapshots.insertOne({ name, stream, cursor, state }); } /** * Get snapshot state by stream. * * @param name - Name of the reducer which the state was created. * @param stream - Stream the state was reduced for. */ async getByStream(name: string, stream: string): Promise { return this.snapshots.findOne({ name, stream }); } /** * Removes a snapshot for the given reducer stream. * * @param name - Name of the reducer the snapshot is attached to. * @param stream - Stream to remove from snapshots. */ async remove(name: string, stream: string): Promise { await this.snapshots.remove({ name, stream }); } }