-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteSyncProviderProps.ts
More file actions
207 lines (184 loc) · 5.36 KB
/
Copy pathSQLiteSyncProviderProps.ts
File metadata and controls
207 lines (184 loc) · 5.36 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import type { ReactNode } from 'react';
import type { TableConfig } from './TableConfig';
import type { DB } from '@op-engineering/op-sqlite';
/**
* Sync mode determines how the provider checks for remote changes
*/
export type SyncMode = 'polling' | 'push';
/**
* Controls when push notifications trigger sync
*/
export type NotificationListeningMode =
| 'foreground' // Only sync when app is in foreground
| 'always'; // Sync in foreground, background, and when app was terminated
/**
* Configuration for adaptive polling behavior
*/
export interface AdaptivePollingConfig {
/**
* Base interval for polling in milliseconds (default: 5000ms / 5s)
* Used when app is active and no special conditions apply
*/
baseInterval?: number;
/**
* Maximum interval when app is idle (default: 300000ms / 5 min)
* Caps the backoff interval for idle periods and errors
*/
maxInterval?: number;
/**
* Number of consecutive empty syncs before backing off (default: 5)
* After this many syncs with no changes, interval will increase
*/
emptyThreshold?: number;
/**
* Idle backoff multiplier for exponential backoff (default: 1.5)
* When consecutive empty syncs exceed threshold, interval multiplies by this factor
* Gentler backoff assumes quiet periods are temporary
* Example: 5s → 7.5s → 11s → 17s → 25s ... (caps at maxInterval)
*/
idleBackoffMultiplier?: number;
/**
* Error backoff multiplier for exponential backoff (default: 2.0)
* When sync errors occur, interval multiplies by this factor
* Aggressive backoff protects server and battery during persistent failures
* Example: 5s → 10s → 20s → 40s → 80s ... (caps at maxInterval)
*/
errorBackoffMultiplier?: number;
}
/**
* Common properties shared across all provider configurations
*/
interface CommonProviderProps {
/**
* CloudSync database ID used by runtime sync APIs and native network init.
*/
databaseId: string;
/**
* Name of the local database file
*/
databaseName: string;
/**
* Array of tables to be synced with SQLite Cloud
* Each table must include its schema for initial creation
*/
tablesToBeSynced: TableConfig[];
/**
* Enable debug logging (default: false)
* When true, logs detailed sync operations to console
*/
debug?: boolean;
/**
* Callback invoked after database is opened but before sync initialization.
* Use this to run migrations or other database setup.
*
* @param db - The write database connection
*
* @example
* ```tsx
* onDatabaseReady={async (db) => {
* const { rows } = await db.execute('PRAGMA user_version');
* const version = rows?.[0]?.user_version ?? 0;
*
* if (version < 1) {
* await db.execute('ALTER TABLE tasks ADD COLUMN priority INTEGER DEFAULT 0');
* await db.execute('PRAGMA user_version = 1');
* }
* }}
* ```
*/
onDatabaseReady?: (db: DB) => Promise<void>;
/**
* Children components
*/
children: React.ReactNode;
}
/**
* Authentication with API key (for apps without RLS)
*/
interface WithApiKey {
/**
* API key for simple authentication (if not using RLS)
*/
apiKey: string;
accessToken?: never;
}
/**
* Authentication with access token (for apps with RLS)
*/
interface WithAccessToken {
/**
* Access token for user-level authentication (when using RLS)
*/
accessToken: string;
apiKey?: never;
}
/**
* Polling mode configuration
* Adaptive polling is optional and falls back to runtime defaults
*/
interface PollingMode {
/**
* Sync mode: polling (default)
* Uses adaptive polling to periodically check for changes
*/
syncMode: 'polling';
/**
* Adaptive polling configuration (optional in polling mode)
* When omitted, runtime defaults are used
*/
adaptivePolling?: AdaptivePollingConfig;
/**
* Not available in polling mode
*/
notificationListening?: never;
renderPushPermissionPrompt?: never;
}
/**
* Push mode configuration
* Adaptive polling is not used in push mode
*/
interface PushMode {
/**
* Sync mode: push
* Relies on push notifications from SQLite Cloud (still syncs on foreground/network)
*/
syncMode: 'push';
/**
* Controls when push notifications trigger sync (default: 'foreground')
* - 'foreground': Only sync when app is in foreground
* - 'always': Sync in foreground, background, and when app was terminated
*/
notificationListening?: NotificationListeningMode;
/**
* Render prop for showing a custom permission prompt before requesting push notification permissions.
* Receives `allow` and `deny` callbacks to resolve the permission request.
*
* @example
* ```tsx
* renderPushPermissionPrompt={({ allow, deny }) => (
* <Modal visible animationType="fade" transparent>
* <View>
* <Text>Enable Real-time Sync?</Text>
* <Button title="Allow" onPress={allow} />
* <Button title="Deny" onPress={deny} />
* </View>
* </Modal>
* )}
* ```
*/
renderPushPermissionPrompt?: (props: {
allow: () => void;
deny: () => void;
}) => ReactNode;
/**
* Not available in push mode
*/
adaptivePolling?: never;
}
/**
* SQLiteSyncProvider props
* Combines common props with authentication and sync mode variants
*/
export type SQLiteSyncProviderProps = CommonProviderProps &
(WithApiKey | WithAccessToken) &
(PollingMode | PushMode);