import { Cid } from '@atproto/lex-data' import { BlockMap } from '../block-map' import { ReadableBlockstore } from './readable-blockstore' export class SyncStorage extends ReadableBlockstore { constructor( public staged: ReadableBlockstore, public saved: ReadableBlockstore, ) { super() } async getBytes(cid: Cid): Promise { const got = await this.staged.getBytes(cid) if (got) return got return this.saved.getBytes(cid) } async getBlocks(cids: Cid[]): Promise<{ blocks: BlockMap; missing: Cid[] }> { const fromStaged = await this.staged.getBlocks(cids) const fromSaved = await this.saved.getBlocks(fromStaged.missing) const blocks = fromStaged.blocks blocks.addMap(fromSaved.blocks) return { blocks, missing: fromSaved.missing, } } async has(cid: Cid): Promise { return (await this.staged.has(cid)) || (await this.saved.has(cid)) } } export default SyncStorage