-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathtypes.ts
More file actions
156 lines (138 loc) · 3.88 KB
/
types.ts
File metadata and controls
156 lines (138 loc) · 3.88 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type {
Collection,
MutationFnParams,
PendingMutation,
} from '@tanstack/db'
// Extended mutation function that includes idempotency key
export type OfflineMutationFnParams<
T extends object = Record<string, unknown>,
> = MutationFnParams<T> & {
idempotencyKey: string
}
export type OfflineMutationFn<T extends object = Record<string, unknown>> = (
params: OfflineMutationFnParams<T>,
) => Promise<any>
// Simplified mutation structure for serialization
export interface SerializedMutation {
globalKey: string
type: string
modified: any
original: any
changes: any
collectionId: string
}
export interface SerializedError {
name: string
message: string
stack?: string
}
export interface SerializedSpanContext {
traceId: string
spanId: string
traceFlags: number
traceState?: string
}
// In-memory representation with full PendingMutation objects
export interface OfflineTransaction {
id: string
mutationFnName: string
mutations: Array<PendingMutation>
keys: Array<string>
idempotencyKey: string
createdAt: Date
retryCount: number
nextAttemptAt: number
lastError?: SerializedError
metadata?: Record<string, any>
spanContext?: SerializedSpanContext
version: 1
}
// Serialized representation for storage
export interface SerializedOfflineTransaction {
id: string
mutationFnName: string
mutations: Array<SerializedMutation>
keys: Array<string>
idempotencyKey: string
createdAt: string
retryCount: number
nextAttemptAt: number
lastError?: SerializedError
metadata?: Record<string, any>
spanContext?: SerializedSpanContext
version: 1
}
// Storage diagnostics and mode
export type OfflineMode = `offline` | `online-only`
export type StorageDiagnosticCode =
| `STORAGE_AVAILABLE`
| `INDEXEDDB_UNAVAILABLE`
| `LOCALSTORAGE_UNAVAILABLE`
| `STORAGE_BLOCKED`
| `QUOTA_EXCEEDED`
| `UNKNOWN_ERROR`
export interface StorageDiagnostic {
code: StorageDiagnosticCode
mode: OfflineMode
message: string
error?: Error
}
export interface OfflineConfig {
collections: Record<string, Collection<any, any, any, any, any>>
mutationFns: Record<string, OfflineMutationFn>
storage?: StorageAdapter
maxConcurrency?: number
jitter?: boolean
beforeRetry?: (
transactions: Array<OfflineTransaction>,
) => Array<OfflineTransaction>
onUnknownMutationFn?: (name: string, tx: OfflineTransaction) => void
onLeadershipChange?: (isLeader: boolean) => void
onStorageFailure?: (diagnostic: StorageDiagnostic) => void
leaderElection?: LeaderElection
/**
* Custom online detector implementation.
* Defaults to WebOnlineDetector for browser environments.
* Use ReactNativeOnlineDetector from '@tanstack/offline-transactions/react-native' for RN/Expo.
*/
onlineDetector?: OnlineDetector
}
export interface StorageAdapter {
get: (key: string) => Promise<string | null>
set: (key: string, value: string) => Promise<void>
delete: (key: string) => Promise<void>
keys: () => Promise<Array<string>>
clear: () => Promise<void>
}
export interface RetryPolicy {
calculateDelay: (retryCount: number) => number
shouldRetry: (error: Error, retryCount: number) => boolean
}
export interface LeaderElection {
requestLeadership: () => Promise<boolean>
releaseLeadership: () => void
isLeader: () => boolean
onLeadershipChange: (callback: (isLeader: boolean) => void) => () => void
}
export interface OnlineDetector {
subscribe: (callback: () => void) => () => void
notifyOnline: () => void
dispose: () => void
}
export interface CreateOfflineTransactionOptions {
id?: string
mutationFnName: string
autoCommit?: boolean
idempotencyKey?: string
metadata?: Record<string, any>
}
export interface CreateOfflineActionOptions<T> {
mutationFnName: string
onMutate: (variables: T) => void
}
export class NonRetriableError extends Error {
constructor(message: string) {
super(message)
this.name = `NonRetriableError`
}
}