Something went wrong. Try again.
WIP generalized fluoddity web viewer and evolution app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556// In-memory Repository — a Map, nothing more. The point is to settle the record shape and the// lineage queries with zero persistence friction before IndexedDB / atproto. Same interface, so// those drop in later without touching callers.
import type { Repository, StoredRecord } from "./Repository";import type { OrganismRecord } from "./record";
export class MemoryRepository implements Repository { private store = new Map<string, OrganismRecord>(); private seq = 0;
async save(record: OrganismRecord): Promise<string> { const id = `org-${(++this.seq).toString(36)}-${Date.now().toString(36)}`; this.store.set(id, record); return id; }
async get(id: string): Promise<OrganismRecord | undefined> { return this.store.get(id); }
async delete(id: string): Promise<void> { this.store.delete(id); }
async list(): Promise<StoredRecord[]> { return [...this.store.entries()].map(([id, record]) => ({ id, record })); }
async children(id: string): Promise<StoredRecord[]> { return [...this.store.entries()] .filter(([, record]) => record.parents.includes(id)) .map(([cid, record]) => ({ id: cid, record })); }
async ancestry(id: string): Promise<StoredRecord[]> { // BFS up the DAG; dedupe (a node can be reached via two parents), nearest-first. const out: StoredRecord[] = []; const seen = new Set<string>(); let frontier = this.store.get(id)?.parents ?? []; while (frontier.length) { const next: string[] = []; for (const pid of frontier) { if (seen.has(pid)) continue; seen.add(pid); const record = this.store.get(pid); if (!record) continue; out.push({ id: pid, record }); next.push(...record.parents); } frontier = next; } return out; }}