|
| 1 | +import type { PersistErrorEvent } from "../types.js"; |
| 2 | + |
| 3 | +// ─── PersistAdapter ─────────────────────────────────────────────────────────── |
| 4 | + |
| 5 | +export interface PersistVersionEntry { |
| 6 | + /** Opaque key identifying this version (adapter-defined format) */ |
| 7 | + key: string; |
| 8 | + content: string; |
| 9 | + timestamp?: number; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Injectable storage adapter — decouples the SDK from the underlying persistence mechanism. |
| 14 | + * Implementations: memory (tests/demos), fs (local dev), S3 (cloud), HTTP (Pacific). |
| 15 | + * |
| 16 | + * Contract: |
| 17 | + * - read() returns undefined for a path never written |
| 18 | + * - write() is idempotent (second write overwrites) |
| 19 | + * - flush() resolves when any queued writes are committed |
| 20 | + * - listVersions() returns entries newest-first |
| 21 | + * - loadFrom() returns content for the given version key (undefined if not found) |
| 22 | + * - on('persist:error') fires when a write fails; the error must not propagate as a thrown exception |
| 23 | + */ |
| 24 | +export interface PersistAdapter { |
| 25 | + read(path: string): Promise<string | undefined>; |
| 26 | + write(path: string, content: string): Promise<void>; |
| 27 | + /** Force all pending writes to commit before returning */ |
| 28 | + flush(): Promise<void>; |
| 29 | + listVersions(path: string): Promise<PersistVersionEntry[]>; |
| 30 | + loadFrom(path: string, versionKey: string): Promise<string | undefined>; |
| 31 | + on(event: "persist:error", handler: (event: PersistErrorEvent) => void): () => void; |
| 32 | +} |
| 33 | + |
| 34 | +// ─── PreviewAdapter ─────────────────────────────────────────────────────────── |
| 35 | + |
| 36 | +export interface ElementAtPointResult { |
| 37 | + id: string; |
| 38 | + tag: string; |
| 39 | +} |
| 40 | + |
| 41 | +export interface DraftProps { |
| 42 | + dx?: number; |
| 43 | + dy?: number; |
| 44 | + width?: number; |
| 45 | + height?: number; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Injectable preview adapter — decouples the SDK from the host preview surface. |
| 50 | + * The null/headless adapter stubs all methods (no browser needed). |
| 51 | + * |
| 52 | + * The SDK is NOT in the 60fps draft loop — consumers call applyDraft() directly on |
| 53 | + * the preview at 60fps; commitPreview() fires once on pointer-up to derive and |
| 54 | + * dispatch the resulting op. |
| 55 | + */ |
| 56 | +export interface PreviewAdapter { |
| 57 | + /** Sync hit-test at composition coordinates. Requires same-origin iframe. */ |
| 58 | + elementAtPoint(x: number, y: number, opts?: { atTime?: number }): ElementAtPointResult | null; |
| 59 | + |
| 60 | + /** Apply draft CSS markers to the preview element (60fps, SDK not involved) */ |
| 61 | + applyDraft(id: string, props: DraftProps): void; |
| 62 | + |
| 63 | + /** Derive op from draft markers, dispatch it, emit patch event, clear markers */ |
| 64 | + commitPreview(): void; |
| 65 | + |
| 66 | + /** Revert draft markers without committing. Model never changed. */ |
| 67 | + cancelPreview(): void; |
| 68 | + |
| 69 | + /** Set preview selection; fires selectionchange on the session */ |
| 70 | + select(ids: string[], opts?: { additive?: boolean }): void; |
| 71 | + |
| 72 | + on(event: "selection", handler: (ids: string[]) => void): () => void; |
| 73 | +} |
0 commit comments