All Challenges
8 PointsExpertState Management
00:00
Offline-First Optimistic Mutation Queue with Rollback & State Reconciliation
Build an optimistic UI mutation orchestrator managing local cache patching, rollback snapshots on server error, and offline sync replay with conflict resolution.
### Problem Overview
Modern apps (Linear, Notion, Superhuman) give instantaneous UI feedback by applying mutations optimistically before server confirmation.
If the server rejects a change, the UI must rollback without overwriting newer concurrent user edits.
Build an **Optimistic Mutation Engine** supporting snapshot rollbacks and conflict resolution.
### Functional Requirements
1. **`mutate<T>(mutation: MutationConfig<T>): Promise<T>`**:
- Takes current state, creates an immutable rollback snapshot, immediately applies `optimisticUpdater(state)`, and fires network request.
2. **Rollback on Server Error**:
- If network call fails, reverts state back accurately while preserving un-related mutations applied concurrently.
3. **Sequential Replay of Offline Mutations**:
- If device is offline, mutations queue up in order. When connectivity resumes, replays mutations sequentially.
4. **Conflict Resolution Strategy**:
- Supports **Last-Write-Wins (LWW)** and custom `resolveConflict(local, remote)` resolvers.
### Non-Functional Requirements
- **State Immutability**: Guarantee state is never directly mutated in place.
- **Transactional Consistency**: Partial failures must not corrupt the state tree.
AI Review Rubric Dimensions (1–10 Points):
- Snapshot rollback integrity
- Immutability enforcement
- Subscriber notification lifecycle
- Conflict resolution
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
29 lines•631 characters•
AI Review Ready