Build a sync engine that queues form submissions locally when offline and syncs them in order when connectivity is restored, with conflict detection. ```typescript class SubmissionQueue { enqueue(submission: Submission): Promise<void> sync(): Promise<SyncResult> getStatus(): QueueStatus } // Submission: { instanceId: string, formId: string, data: Record<string, unknown>, submittedAt: string } // SyncResult: { synced: number, failed: number, conflicts: ConflictRecord[], errors: string[] } // QueueStatus: { pending: number, failed: number, lastSyncAt: string | null } // ConflictRecord: { instanceId: string, resolution: 'last-write-wins', mergeLog: string } ``` Storage: persist the queue to IndexedDB (use `idb` library). Queue must survive page reloads. Sync strategy: 1. Process queue FIFO order 2. POST each submission to `/api/submissions` 3. On 409 Conflict (same `instanceId` already exists): resolve via last-write-wins (the local version wins), re-POST with `X-Conflict-Resolution: overwrite`, log the conflict in `ConflictRecord` 4. On 5xx or network error: retry up to 3 times with exponential backoff (1s, 2s, 4s), then mark as `failed` 5. On success: remove from queue, update `lastSyncAt` Event emitter: emit `queued`, `syncing`, `synced`, `conflict`, `failed` events so UI can show sync status. Write unit tests for: queue persistence across simulated page reloads, conflict detection and resolution, partial sync recovery (fails on item 3 of 5, resumes from item 3).
No contributions yet.