Skip to content

Build optimistic UI cache with automatic revert on transaction failure #443

Description

@Kingsman-99

Description

Consumer UIs built on the SDK must re-fetch state after every mutation, causing visible flicker. This issue introduces an OptimisticCache that applies a predicted outcome to cached Invoice and Payment objects immediately upon submission, then either confirms or rolls back the prediction based on the settled transaction result. The cache integrates with the existing fetch methods in SplitClient so that reads during a pending transaction return the optimistic version.

Technical Context

  • New file: src/cache/OptimisticCache.ts; extends the internal ResponseCache already used by SplitClient
  • OptimisticCache.applyOptimistic(key, predictedValue, rollbackValue) stores both values and returns a commit() / rollback() function pair
  • SplitClient passes the pair into the transaction lifecycle hooks (onSettled, onFailed) from the retry policy
  • The cache must be keyed by a composite of (invoiceId, version) to avoid stale reads across concurrent mutations
  • Expose cache.pendingCount and cache.onRollback(handler) for UI integration

Acceptance Criteria

  • After applyOptimistic(), reads for the same key return the predicted value until commit() or rollback() is called
  • rollback() restores the previous value, emits an onRollback event, and leaves no stale optimistic entries
  • Concurrent optimistic mutations to the same invoice are queued; rolling back mutation N does not discard mutations N+1 through M
  • Integration test: submit a payment that fails simulation; assert the UI-facing cache value reverts to pre-submission state within one event loop tick after failure
  • OptimisticCache is exported from the package index and documented with JSDoc types
  • All CI checks (npm test, npm run build, ESLint) pass and the branch has no merge conflicts

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions