Open History to see the last commit
1import type {
2 Collection,
3 MutationFnParams,
4 PendingMutation,
5 Transaction,
6} from '@tanstack/db'
7
8// Extended mutation function that includes idempotency key
9export type OfflineMutationFnParams<
10 T extends object = Record<string, unknown>,
11> = MutationFnParams<T> & {
12 idempotencyKey: string
13}
14
15export type OfflineMutationFn<T extends object = Record<string, unknown>> = (
16 params: OfflineMutationFnParams<T>,
17) => Promise<any>
18
19// Simplified mutation structure for serialization
20export interface SerializedMutation {
21 globalKey: string
22 type: string
23 modified: any
24 original: any
25 changes: any
26 collectionId: string
27}
28
29export interface SerializedError {
30 name: string
31 message: string
32 stack?: string
33}
34
35export interface SerializedSpanContext {
36 traceId: string
37 spanId: string
38 traceFlags: number
39 traceState?: string
40}
41
42// In-memory representation with full PendingMutation objects
43export interface OfflineTransaction {
44 id: string
45 mutationFnName: string
46 mutations: Array<PendingMutation>
47 keys: Array<string>
48 idempotencyKey: string
49 createdAt: Date
50 retryCount: number
51 nextAttemptAt: number
52 /** Provider work has settled; only durable outbox deletion remains. */
53 outboxPhase?: `deletion-pending` | `rejection-pending`
54 lastError?: SerializedError
55 metadata?: Record<string, any>
56 spanContext?: SerializedSpanContext
57 version: 1
58}
59
60// Serialized representation for storage
61export interface SerializedOfflineTransaction {
62 /** Absent for the original Date-marker format. */
63 valueEncoding?: 2 | 3
64 id: string
65 mutationFnName: string
66 mutations: Array<SerializedMutation>
67 keys: Array<string>
68 idempotencyKey: string
69 createdAt: string
70 retryCount: number
71 nextAttemptAt: number
72 /** Absent in older records and while mutationFn may still need to run. */
73 outboxPhase?: `deletion-pending` | `rejection-pending`
74 lastError?: SerializedError
75 metadata?: Record<string, any>
76 spanContext?: SerializedSpanContext
77 version: 1
78}
79
80// Storage diagnostics and mode
81export type OfflineMode = `offline` | `online-only`
82
83export type StorageDiagnosticCode =
84 | `STORAGE_AVAILABLE`
85 | `INDEXEDDB_UNAVAILABLE`
86 | `LOCALSTORAGE_UNAVAILABLE`
87 | `STORAGE_BLOCKED`
88 | `QUOTA_EXCEEDED`
89 | `UNKNOWN_ERROR`
90
91export interface StorageDiagnostic {
92 code: StorageDiagnosticCode
93 mode: OfflineMode
94 message: string
95 error?: Error
96}
97
98export interface OfflineConfig {
99 collections: Record<string, Collection<any, any, any, any, any>>
100 mutationFns: Record<string, OfflineMutationFn>
101 storage?: StorageAdapter
102 maxConcurrency?: number
103 jitter?: boolean
104 beforeRetry?: (
105 transactions: Array<OfflineTransaction>,
106 ) => Array<OfflineTransaction>
107 onUnknownMutationFn?: (name: string, tx: OfflineTransaction) => void
108 onLeadershipChange?: (isLeader: boolean) => void
109 onStorageFailure?: (diagnostic: StorageDiagnostic) => void
110 leaderElection?: LeaderElection
111 /**
112 * Custom online detector implementation.
113 * Defaults to WebOnlineDetector for browser environments.
114 * The '@tanstack/offline-transactions/react-native' entry point uses ReactNativeOnlineDetector automatically.
115 */
116 onlineDetector?: OnlineDetector
117}
118
119export interface StorageAdapter {
120 get: (key: string) => Promise<string | null>
121 set: (key: string, value: string) => Promise<void>
122 delete: (key: string) => Promise<void>
123 keys: () => Promise<Array<string>>
124 clear: () => Promise<void>
125}
126
127export interface RetryPolicy {
128 calculateDelay: (retryCount: number) => number
129 shouldRetry: (error: Error, retryCount: number) => boolean
130}
131
132export interface LeaderElection {
133 requestLeadership: () => Promise<boolean>
134 releaseLeadership: () => void
135 isLeader: () => boolean
136 onLeadershipChange: (callback: (isLeader: boolean) => void) => () => void
137}
138
139export interface TransactionSignaler {
140 readonly isOfflineEnabled: boolean
141 resolveTransaction: (transactionId: string, result: any) => void
142 rejectTransaction: (transactionId: string, error: Error) => void
143 registerRestorationTransaction: (
144 offlineTransactionId: string,
145 restorationTransaction: Transaction,
146 ) => void
147 isOnline: () => boolean
148}
149
150export interface OnlineDetector {
151 subscribe: (callback: () => void) => () => void
152 notifyOnline: () => void
153 isOnline: () => boolean
154 dispose: () => void
155}
156
157export interface CreateOfflineTransactionOptions {
158 id?: string
159 mutationFnName: string
160 autoCommit?: boolean
161 idempotencyKey?: string
162 metadata?: Record<string, any>
163}
164
165export interface CreateOfflineActionOptions<T> {
166 mutationFnName: string
167 onMutate: (variables: T) => void
168}
169
170export class NonRetriableError extends Error {
171 constructor(message: string) {
172 super(message)
173 this.name = `NonRetriableError`
174 }
175}