import type { Store, CASResult, StoreQuery, QueryResult } from '@matatbread/matbot-plugin-api'; import { executeQuery } from './query/index.js'; /** A `Store` held in a `Map`: nothing persists, nothing is announced, nothing outlives the instance. */ export class MemoryStore implements Store { private readonly items = new Map(); get size(): number { return this.items.size; } async get(id: string): Promise { return this.items.get(id) ?? null; } async set(id: string, value: T): Promise { this.items.set(id, value); } async cas(id: string, expected: string, next: T): Promise> { const current = this.items.get(id) ?? null; if (current === null || current.version !== expected) return { ok: false, current }; this.items.set(id, next); return { ok: true, doc: next }; } async delete(id: string, expectedVersion?: string): Promise { if (expectedVersion !== undefined) { const current = this.items.get(id); if (current === undefined || current.version !== expectedVersion) return false; } return this.items.delete(id); } async query(q: StoreQuery): Promise> { return executeQuery([...this.items.values()], q); } }