import { Encoder } from 'msgpackr'; import type { AztecAsyncSingleton } from '../interfaces/singleton.js'; import type { AztecLMDBStoreV2 } from './store.js'; import { execInReadTx, execInWriteTx } from './tx-helpers.js'; import { serializeKey } from './utils.js'; export class LMDBSingleValue implements AztecAsyncSingleton { private key: Uint8Array; private encoder = new Encoder(); constructor( private store: AztecLMDBStoreV2, name: string, ) { this.key = serializeKey(`singleton:${name}`, 'value'); } getAsync(): Promise { return execInReadTx(this.store, async tx => { const val = await tx.get(this.key); return val ? this.encoder.unpack(val) : undefined; }); } set(val: T): Promise { return execInWriteTx(this.store, async tx => { await tx.set(this.key, this.encoder.pack(val)); return true; }); } delete(): Promise { return execInWriteTx(this.store, async tx => { await tx.remove(this.key); return true; }); } }