@syncular/client 0.0.1-100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/dist/blobs/index.d.ts +7 -0
  2. package/dist/blobs/index.d.ts.map +1 -0
  3. package/dist/blobs/index.js +7 -0
  4. package/dist/blobs/index.js.map +1 -0
  5. package/dist/blobs/manager.d.ts +345 -0
  6. package/dist/blobs/manager.d.ts.map +1 -0
  7. package/dist/blobs/manager.js +749 -0
  8. package/dist/blobs/manager.js.map +1 -0
  9. package/dist/blobs/migrate.d.ts +14 -0
  10. package/dist/blobs/migrate.d.ts.map +1 -0
  11. package/dist/blobs/migrate.js +59 -0
  12. package/dist/blobs/migrate.js.map +1 -0
  13. package/dist/blobs/types.d.ts +62 -0
  14. package/dist/blobs/types.d.ts.map +1 -0
  15. package/dist/blobs/types.js +5 -0
  16. package/dist/blobs/types.js.map +1 -0
  17. package/dist/client.d.ts +339 -0
  18. package/dist/client.d.ts.map +1 -0
  19. package/dist/client.js +881 -0
  20. package/dist/client.js.map +1 -0
  21. package/dist/conflicts.d.ts +31 -0
  22. package/dist/conflicts.d.ts.map +1 -0
  23. package/dist/conflicts.js +112 -0
  24. package/dist/conflicts.js.map +1 -0
  25. package/dist/create-client.d.ts +115 -0
  26. package/dist/create-client.d.ts.map +1 -0
  27. package/dist/create-client.js +162 -0
  28. package/dist/create-client.js.map +1 -0
  29. package/dist/engine/SyncEngine.d.ts +216 -0
  30. package/dist/engine/SyncEngine.d.ts.map +1 -0
  31. package/dist/engine/SyncEngine.js +1141 -0
  32. package/dist/engine/SyncEngine.js.map +1 -0
  33. package/dist/engine/index.d.ts +6 -0
  34. package/dist/engine/index.d.ts.map +1 -0
  35. package/dist/engine/index.js +6 -0
  36. package/dist/engine/index.js.map +1 -0
  37. package/dist/engine/types.d.ts +230 -0
  38. package/dist/engine/types.d.ts.map +1 -0
  39. package/dist/engine/types.js +7 -0
  40. package/dist/engine/types.js.map +1 -0
  41. package/dist/handlers/create-handler.d.ts +110 -0
  42. package/dist/handlers/create-handler.d.ts.map +1 -0
  43. package/dist/handlers/create-handler.js +142 -0
  44. package/dist/handlers/create-handler.js.map +1 -0
  45. package/dist/handlers/registry.d.ts +15 -0
  46. package/dist/handlers/registry.d.ts.map +1 -0
  47. package/dist/handlers/registry.js +29 -0
  48. package/dist/handlers/registry.js.map +1 -0
  49. package/dist/handlers/types.d.ts +83 -0
  50. package/dist/handlers/types.d.ts.map +1 -0
  51. package/dist/handlers/types.js +5 -0
  52. package/dist/handlers/types.js.map +1 -0
  53. package/dist/index.d.ts +24 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.js +24 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/migrate.d.ts +19 -0
  58. package/dist/migrate.d.ts.map +1 -0
  59. package/dist/migrate.js +106 -0
  60. package/dist/migrate.js.map +1 -0
  61. package/dist/mutations.d.ts +138 -0
  62. package/dist/mutations.d.ts.map +1 -0
  63. package/dist/mutations.js +601 -0
  64. package/dist/mutations.js.map +1 -0
  65. package/dist/outbox.d.ts +112 -0
  66. package/dist/outbox.d.ts.map +1 -0
  67. package/dist/outbox.js +294 -0
  68. package/dist/outbox.js.map +1 -0
  69. package/dist/plugins/incrementing-version.d.ts +34 -0
  70. package/dist/plugins/incrementing-version.d.ts.map +1 -0
  71. package/dist/plugins/incrementing-version.js +83 -0
  72. package/dist/plugins/incrementing-version.js.map +1 -0
  73. package/dist/plugins/index.d.ts +3 -0
  74. package/dist/plugins/index.d.ts.map +1 -0
  75. package/dist/plugins/index.js +3 -0
  76. package/dist/plugins/index.js.map +1 -0
  77. package/dist/plugins/types.d.ts +49 -0
  78. package/dist/plugins/types.d.ts.map +1 -0
  79. package/dist/plugins/types.js +15 -0
  80. package/dist/plugins/types.js.map +1 -0
  81. package/dist/proxy/connection.d.ts +33 -0
  82. package/dist/proxy/connection.d.ts.map +1 -0
  83. package/dist/proxy/connection.js +153 -0
  84. package/dist/proxy/connection.js.map +1 -0
  85. package/dist/proxy/dialect.d.ts +46 -0
  86. package/dist/proxy/dialect.d.ts.map +1 -0
  87. package/dist/proxy/dialect.js +58 -0
  88. package/dist/proxy/dialect.js.map +1 -0
  89. package/dist/proxy/driver.d.ts +42 -0
  90. package/dist/proxy/driver.d.ts.map +1 -0
  91. package/dist/proxy/driver.js +78 -0
  92. package/dist/proxy/driver.js.map +1 -0
  93. package/dist/proxy/index.d.ts +10 -0
  94. package/dist/proxy/index.d.ts.map +1 -0
  95. package/dist/proxy/index.js +10 -0
  96. package/dist/proxy/index.js.map +1 -0
  97. package/dist/proxy/mutations.d.ts +9 -0
  98. package/dist/proxy/mutations.d.ts.map +1 -0
  99. package/dist/proxy/mutations.js +11 -0
  100. package/dist/proxy/mutations.js.map +1 -0
  101. package/dist/pull-engine.d.ts +45 -0
  102. package/dist/pull-engine.d.ts.map +1 -0
  103. package/dist/pull-engine.js +381 -0
  104. package/dist/pull-engine.js.map +1 -0
  105. package/dist/push-engine.d.ts +18 -0
  106. package/dist/push-engine.d.ts.map +1 -0
  107. package/dist/push-engine.js +155 -0
  108. package/dist/push-engine.js.map +1 -0
  109. package/dist/query/FingerprintCollector.d.ts +18 -0
  110. package/dist/query/FingerprintCollector.d.ts.map +1 -0
  111. package/dist/query/FingerprintCollector.js +28 -0
  112. package/dist/query/FingerprintCollector.js.map +1 -0
  113. package/dist/query/QueryContext.d.ts +33 -0
  114. package/dist/query/QueryContext.d.ts.map +1 -0
  115. package/dist/query/QueryContext.js +16 -0
  116. package/dist/query/QueryContext.js.map +1 -0
  117. package/dist/query/fingerprint.d.ts +61 -0
  118. package/dist/query/fingerprint.d.ts.map +1 -0
  119. package/dist/query/fingerprint.js +91 -0
  120. package/dist/query/fingerprint.js.map +1 -0
  121. package/dist/query/index.d.ts +7 -0
  122. package/dist/query/index.d.ts.map +1 -0
  123. package/dist/query/index.js +7 -0
  124. package/dist/query/index.js.map +1 -0
  125. package/dist/query/tracked-select.d.ts +18 -0
  126. package/dist/query/tracked-select.d.ts.map +1 -0
  127. package/dist/query/tracked-select.js +90 -0
  128. package/dist/query/tracked-select.js.map +1 -0
  129. package/dist/schema.d.ts +83 -0
  130. package/dist/schema.d.ts.map +1 -0
  131. package/dist/schema.js +7 -0
  132. package/dist/schema.js.map +1 -0
  133. package/dist/sync-loop.d.ts +32 -0
  134. package/dist/sync-loop.d.ts.map +1 -0
  135. package/dist/sync-loop.js +249 -0
  136. package/dist/sync-loop.js.map +1 -0
  137. package/dist/utils/id.d.ts +8 -0
  138. package/dist/utils/id.d.ts.map +1 -0
  139. package/dist/utils/id.js +19 -0
  140. package/dist/utils/id.js.map +1 -0
  141. package/package.json +59 -0
  142. package/src/blobs/index.ts +7 -0
  143. package/src/blobs/manager.ts +1027 -0
  144. package/src/blobs/migrate.ts +67 -0
  145. package/src/blobs/types.ts +84 -0
  146. package/src/client.test.ts +369 -0
  147. package/src/client.ts +1288 -0
  148. package/src/conflicts.ts +171 -0
  149. package/src/create-client.ts +297 -0
  150. package/src/engine/SyncEngine.test.ts +157 -0
  151. package/src/engine/SyncEngine.ts +1464 -0
  152. package/src/engine/index.ts +6 -0
  153. package/src/engine/types.ts +268 -0
  154. package/src/handlers/create-handler.ts +298 -0
  155. package/src/handlers/registry.ts +36 -0
  156. package/src/handlers/types.ts +102 -0
  157. package/src/index.ts +25 -0
  158. package/src/migrate.ts +122 -0
  159. package/src/mutations.ts +912 -0
  160. package/src/outbox.ts +383 -0
  161. package/src/plugins/incrementing-version.ts +133 -0
  162. package/src/plugins/index.ts +2 -0
  163. package/src/plugins/types.ts +63 -0
  164. package/src/proxy/connection.ts +191 -0
  165. package/src/proxy/dialect.ts +76 -0
  166. package/src/proxy/driver.ts +126 -0
  167. package/src/proxy/index.ts +10 -0
  168. package/src/proxy/mutations.ts +18 -0
  169. package/src/pull-engine.ts +508 -0
  170. package/src/push-engine.ts +201 -0
  171. package/src/query/FingerprintCollector.ts +29 -0
  172. package/src/query/QueryContext.ts +54 -0
  173. package/src/query/fingerprint.ts +109 -0
  174. package/src/query/index.ts +10 -0
  175. package/src/query/tracked-select.ts +139 -0
  176. package/src/schema.ts +94 -0
  177. package/src/sync-loop.ts +368 -0
  178. package/src/utils/id.ts +20 -0
@@ -0,0 +1,1464 @@
1
+ /**
2
+ * @syncular/client - Core sync engine
3
+ *
4
+ * Event-driven sync engine that manages push/pull cycles, connection state,
5
+ * and provides a clean API for framework bindings to consume.
6
+ */
7
+
8
+ import {
9
+ captureSyncException,
10
+ countSyncMetric,
11
+ distributionSyncMetric,
12
+ isRecord,
13
+ type SyncChange,
14
+ type SyncPullResponse,
15
+ type SyncSubscriptionRequest,
16
+ startSyncSpan,
17
+ } from '@syncular/core';
18
+ import { type Kysely, sql } from 'kysely';
19
+ import { syncPushOnce } from '../push-engine';
20
+ import type {
21
+ ConflictResultStatus,
22
+ OutboxCommitStatus,
23
+ SyncClientDb,
24
+ } from '../schema';
25
+ import { syncOnce } from '../sync-loop';
26
+ import type {
27
+ ConflictInfo,
28
+ OutboxStats,
29
+ PresenceEntry,
30
+ RealtimeTransportLike,
31
+ SyncConnectionState,
32
+ SyncEngineConfig,
33
+ SyncEngineState,
34
+ SyncError,
35
+ SyncEventListener,
36
+ SyncEventPayloads,
37
+ SyncEventType,
38
+ SyncResult,
39
+ SyncTransportMode,
40
+ } from './types';
41
+
42
+ const DEFAULT_POLL_INTERVAL_MS = 10_000;
43
+ const DEFAULT_MAX_RETRIES = 5;
44
+ const INITIAL_RETRY_DELAY_MS = 1000;
45
+ const MAX_RETRY_DELAY_MS = 60000;
46
+ const EXPONENTIAL_FACTOR = 2;
47
+ const REALTIME_RECONNECT_CATCHUP_DELAY_MS = 500;
48
+
49
+ function calculateRetryDelay(attemptIndex: number): number {
50
+ return Math.min(
51
+ INITIAL_RETRY_DELAY_MS * EXPONENTIAL_FACTOR ** attemptIndex,
52
+ MAX_RETRY_DELAY_MS
53
+ );
54
+ }
55
+
56
+ function isRealtimeTransport(
57
+ transport: unknown
58
+ ): transport is RealtimeTransportLike {
59
+ return (
60
+ typeof transport === 'object' &&
61
+ transport !== null &&
62
+ typeof (transport as RealtimeTransportLike).connect === 'function'
63
+ );
64
+ }
65
+
66
+ function createSyncError(
67
+ code: SyncError['code'],
68
+ message: string,
69
+ cause?: Error
70
+ ): SyncError {
71
+ return {
72
+ code,
73
+ message,
74
+ cause,
75
+ timestamp: Date.now(),
76
+ };
77
+ }
78
+
79
+ function resolveSyncTriggerLabel(
80
+ trigger?: 'ws' | 'local' | 'poll'
81
+ ): 'ws' | 'local' | 'poll' | 'auto' {
82
+ return trigger ?? 'auto';
83
+ }
84
+
85
+ /**
86
+ * Sync engine that orchestrates push/pull cycles with proper lifecycle management.
87
+ *
88
+ * Key features:
89
+ * - Event-driven architecture (no global mutable state)
90
+ * - Proper error handling (no silent catches)
91
+ * - Lifecycle managed by framework bindings
92
+ * - Type-safe event subscriptions
93
+ * - Presence tracking support
94
+ */
95
+ type AnyEventListener = (payload: SyncEventPayloads[SyncEventType]) => void;
96
+
97
+ export class SyncEngine<DB extends SyncClientDb = SyncClientDb> {
98
+ private config: SyncEngineConfig<DB>;
99
+ private state: SyncEngineState;
100
+ private listeners: Map<SyncEventType, Set<AnyEventListener>>;
101
+ private pollerId: ReturnType<typeof setInterval> | null = null;
102
+ private fallbackPollerId: ReturnType<typeof setInterval> | null = null;
103
+ private realtimeDisconnect: (() => void) | null = null;
104
+ private realtimePresenceUnsub: (() => void) | null = null;
105
+ private isDestroyed = false;
106
+ private migrated = false;
107
+ private syncPromise: Promise<SyncResult> | null = null;
108
+ private syncRequestedWhileRunning = false;
109
+ private retryTimeoutId: ReturnType<typeof setTimeout> | null = null;
110
+ private realtimeCatchupTimeoutId: ReturnType<typeof setTimeout> | null = null;
111
+ private hasRealtimeConnectedOnce = false;
112
+
113
+ /**
114
+ * In-memory map tracking local mutation timestamps by rowId.
115
+ * Used for efficient fingerprint-based rerender optimization.
116
+ * Key format: `${table}:${rowId}`, Value: timestamp (Date.now())
117
+ */
118
+ private mutationTimestamps = new Map<string, number>();
119
+
120
+ /**
121
+ * In-memory map tracking table-level mutation timestamps.
122
+ * Used for coarse invalidation during large bootstrap snapshots to avoid
123
+ * storing timestamps for every row.
124
+ */
125
+ private tableMutationTimestamps = new Map<string, number>();
126
+
127
+ /**
128
+ * In-memory presence state by scope key.
129
+ * Updated via realtime presence events.
130
+ */
131
+ private presenceByScopeKey = new Map<string, PresenceEntry[]>();
132
+
133
+ constructor(config: SyncEngineConfig<DB>) {
134
+ this.config = config;
135
+ this.listeners = new Map();
136
+ this.state = this.createInitialState();
137
+ }
138
+
139
+ /**
140
+ * Get mutation timestamp for a row (used by query hooks for fingerprinting).
141
+ * Returns 0 if row has no recorded mutation timestamp.
142
+ */
143
+ getMutationTimestamp(table: string, rowId: string): number {
144
+ const rowTs = this.mutationTimestamps.get(`${table}:${rowId}`) ?? 0;
145
+ const tableTs = this.tableMutationTimestamps.get(table) ?? 0;
146
+ return Math.max(rowTs, tableTs);
147
+ }
148
+
149
+ /**
150
+ * Get presence entries for a scope key.
151
+ * Returns empty array if no presence data for the scope.
152
+ */
153
+ getPresence<TMetadata = Record<string, unknown>>(
154
+ scopeKey: string
155
+ ): PresenceEntry<TMetadata>[] {
156
+ return (this.presenceByScopeKey.get(scopeKey) ??
157
+ []) as PresenceEntry<TMetadata>[];
158
+ }
159
+
160
+ /**
161
+ * Update presence for a scope key (called by realtime transport).
162
+ * Emits presence:change event for listeners.
163
+ */
164
+ updatePresence(scopeKey: string, presence: PresenceEntry[]): void {
165
+ this.presenceByScopeKey.set(scopeKey, presence);
166
+ this.emit('presence:change', { scopeKey, presence });
167
+ }
168
+
169
+ /**
170
+ * Join presence for a scope key.
171
+ * Sends via transport (if available) and updates local state optimistically.
172
+ */
173
+ joinPresence(scopeKey: string, metadata?: Record<string, unknown>): void {
174
+ if (isRealtimeTransport(this.config.transport)) {
175
+ const transport = this.config.transport as RealtimeTransportLike;
176
+ transport.sendPresenceJoin?.(scopeKey, metadata);
177
+ }
178
+ // Optimistic local update
179
+ this.handlePresenceEvent({
180
+ action: 'join',
181
+ scopeKey,
182
+ clientId: this.config.clientId!,
183
+ actorId: this.config.actorId!,
184
+ metadata,
185
+ });
186
+ }
187
+
188
+ /**
189
+ * Leave presence for a scope key.
190
+ */
191
+ leavePresence(scopeKey: string): void {
192
+ if (isRealtimeTransport(this.config.transport)) {
193
+ const transport = this.config.transport as RealtimeTransportLike;
194
+ transport.sendPresenceLeave?.(scopeKey);
195
+ }
196
+ this.handlePresenceEvent({
197
+ action: 'leave',
198
+ scopeKey,
199
+ clientId: this.config.clientId!,
200
+ actorId: this.config.actorId!,
201
+ });
202
+ }
203
+
204
+ /**
205
+ * Update presence metadata for a scope key.
206
+ */
207
+ updatePresenceMetadata(
208
+ scopeKey: string,
209
+ metadata: Record<string, unknown>
210
+ ): void {
211
+ if (isRealtimeTransport(this.config.transport)) {
212
+ const transport = this.config.transport as RealtimeTransportLike;
213
+ transport.sendPresenceUpdate?.(scopeKey, metadata);
214
+ }
215
+ this.handlePresenceEvent({
216
+ action: 'update',
217
+ scopeKey,
218
+ clientId: this.config.clientId!,
219
+ actorId: this.config.actorId!,
220
+ metadata,
221
+ });
222
+ }
223
+
224
+ /**
225
+ * Handle a single presence event (join/leave/update).
226
+ * Updates the in-memory presence state and emits change event.
227
+ */
228
+ handlePresenceEvent(event: {
229
+ action: 'join' | 'leave' | 'update';
230
+ scopeKey: string;
231
+ clientId: string;
232
+ actorId: string;
233
+ metadata?: Record<string, unknown>;
234
+ }): void {
235
+ const current = this.presenceByScopeKey.get(event.scopeKey) ?? [];
236
+
237
+ let updated: PresenceEntry[];
238
+ switch (event.action) {
239
+ case 'join':
240
+ // Add new entry (remove existing if present to update)
241
+ updated = [
242
+ ...current.filter((e) => e.clientId !== event.clientId),
243
+ {
244
+ clientId: event.clientId,
245
+ actorId: event.actorId,
246
+ joinedAt: Date.now(),
247
+ metadata: event.metadata,
248
+ },
249
+ ];
250
+ break;
251
+ case 'leave':
252
+ updated = current.filter((e) => e.clientId !== event.clientId);
253
+ break;
254
+ case 'update':
255
+ updated = current.map((e) =>
256
+ e.clientId === event.clientId ? { ...e, metadata: event.metadata } : e
257
+ );
258
+ break;
259
+ }
260
+
261
+ this.presenceByScopeKey.set(event.scopeKey, updated);
262
+ this.emit('presence:change', {
263
+ scopeKey: event.scopeKey,
264
+ presence: updated,
265
+ });
266
+ }
267
+
268
+ private createInitialState(): SyncEngineState {
269
+ const enabled = this.isEnabled();
270
+ return {
271
+ enabled,
272
+ isSyncing: false,
273
+ connectionState: enabled ? 'disconnected' : 'disconnected',
274
+ transportMode: this.detectTransportMode(),
275
+ lastSyncAt: null,
276
+ error: null,
277
+ pendingCount: 0,
278
+ retryCount: 0,
279
+ isRetrying: false,
280
+ };
281
+ }
282
+
283
+ private isEnabled(): boolean {
284
+ const { actorId, clientId } = this.config;
285
+ return (
286
+ typeof actorId === 'string' &&
287
+ actorId.length > 0 &&
288
+ typeof clientId === 'string' &&
289
+ clientId.length > 0
290
+ );
291
+ }
292
+
293
+ private detectTransportMode(): SyncTransportMode {
294
+ if (
295
+ this.config.realtimeEnabled !== false &&
296
+ isRealtimeTransport(this.config.transport)
297
+ ) {
298
+ return 'realtime';
299
+ }
300
+ return 'polling';
301
+ }
302
+
303
+ /**
304
+ * Get current engine state.
305
+ * Returns the same object reference to avoid useSyncExternalStore infinite loops.
306
+ */
307
+ getState(): Readonly<SyncEngineState> {
308
+ return this.state;
309
+ }
310
+
311
+ /**
312
+ * Get database instance
313
+ */
314
+ getDb(): Kysely<DB> {
315
+ return this.config.db;
316
+ }
317
+
318
+ /**
319
+ * Get current actor id (sync scoping).
320
+ */
321
+ getActorId(): string | null | undefined {
322
+ return this.config.actorId;
323
+ }
324
+
325
+ /**
326
+ * Get current client id (device/app install id).
327
+ */
328
+ getClientId(): string | null | undefined {
329
+ return this.config.clientId;
330
+ }
331
+
332
+ /**
333
+ * Subscribe to sync events
334
+ */
335
+ on<T extends SyncEventType>(
336
+ event: T,
337
+ listener: SyncEventListener<T>
338
+ ): () => void {
339
+ if (!this.listeners.has(event)) {
340
+ this.listeners.set(event, new Set());
341
+ }
342
+ const wrapped: AnyEventListener = (payload) => {
343
+ listener(payload as SyncEventPayloads[T]);
344
+ };
345
+ this.listeners.get(event)!.add(wrapped);
346
+
347
+ return () => {
348
+ this.listeners.get(event)?.delete(wrapped);
349
+ };
350
+ }
351
+
352
+ /**
353
+ * Subscribe to any state change (for useSyncExternalStore)
354
+ */
355
+ subscribe(callback: () => void): () => void {
356
+ // Subscribe to state:change which is emitted by updateState()
357
+ return this.on('state:change', callback);
358
+ }
359
+
360
+ private emit<T extends SyncEventType>(
361
+ event: T,
362
+ payload: SyncEventPayloads[T]
363
+ ): void {
364
+ const eventListeners = this.listeners.get(event);
365
+ if (eventListeners) {
366
+ for (const listener of eventListeners) {
367
+ try {
368
+ listener(payload);
369
+ } catch (err) {
370
+ console.error(`[SyncEngine] Error in ${event} listener:`, err);
371
+ }
372
+ }
373
+ }
374
+ }
375
+
376
+ private updateState(partial: Partial<SyncEngineState>): void {
377
+ this.state = { ...this.state, ...partial };
378
+ // Emit state:change to notify useSyncExternalStore subscribers
379
+ this.emit('state:change', {});
380
+ }
381
+
382
+ private setConnectionState(state: SyncConnectionState): void {
383
+ const previous = this.state.connectionState;
384
+ if (previous !== state) {
385
+ this.updateState({ connectionState: state });
386
+ this.emit('connection:change', { previous, current: state });
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Start the sync engine
392
+ */
393
+ async start(): Promise<void> {
394
+ if (this.isDestroyed) {
395
+ throw new Error('SyncEngine has been destroyed');
396
+ }
397
+
398
+ if (!this.isEnabled()) {
399
+ this.updateState({ enabled: false });
400
+ return;
401
+ }
402
+
403
+ this.updateState({ enabled: true });
404
+
405
+ // Run migration if provided
406
+ if (this.config.migrate && !this.migrated) {
407
+ // Best-effort: push any pending outbox commits before migration
408
+ // (migration may reset the DB, so we try to save unsynced changes)
409
+ try {
410
+ const hasOutbox = await sql`
411
+ select 1 from ${sql.table('sync_outbox_commits')} limit 1
412
+ `
413
+ .execute(this.config.db)
414
+ .then((r) => r.rows.length > 0)
415
+ .catch(() => false);
416
+
417
+ if (hasOutbox) {
418
+ // Push all pending commits (best effort)
419
+ let pushed = true;
420
+ while (pushed) {
421
+ const result = await syncPushOnce(
422
+ this.config.db,
423
+ this.config.transport,
424
+ {
425
+ clientId: this.config.clientId!,
426
+ actorId: this.config.actorId ?? undefined,
427
+ plugins: this.config.plugins,
428
+ }
429
+ );
430
+ pushed = result.pushed;
431
+ }
432
+ }
433
+ } catch {
434
+ // Best-effort: if push fails (network down, table missing), continue
435
+ }
436
+
437
+ try {
438
+ await this.config.migrate(this.config.db);
439
+ this.migrated = true;
440
+ } catch (err) {
441
+ const migrationError =
442
+ err instanceof Error ? err : new Error(String(err));
443
+ this.config.onMigrationError?.(migrationError);
444
+ const error = createSyncError(
445
+ 'SYNC_ERROR',
446
+ 'Migration failed',
447
+ migrationError
448
+ );
449
+ this.handleError(error);
450
+ return;
451
+ }
452
+ }
453
+
454
+ // Setup transport-specific handling
455
+ if (this.state.transportMode === 'realtime') {
456
+ this.setupRealtime();
457
+ } else {
458
+ this.setupPolling();
459
+ }
460
+
461
+ // Initial sync
462
+ await this.sync();
463
+ }
464
+
465
+ /**
466
+ * Stop the sync engine (cleanup without destroy)
467
+ */
468
+ stop(): void {
469
+ this.stopPolling();
470
+ this.stopRealtime();
471
+ this.setConnectionState('disconnected');
472
+ }
473
+
474
+ /**
475
+ * Destroy the engine (cannot be restarted)
476
+ */
477
+ destroy(): void {
478
+ this.stop();
479
+ this.listeners.clear();
480
+ this.isDestroyed = true;
481
+
482
+ if (this.retryTimeoutId) {
483
+ clearTimeout(this.retryTimeoutId);
484
+ this.retryTimeoutId = null;
485
+ }
486
+ if (this.realtimeCatchupTimeoutId) {
487
+ clearTimeout(this.realtimeCatchupTimeoutId);
488
+ this.realtimeCatchupTimeoutId = null;
489
+ }
490
+ }
491
+
492
+ /**
493
+ * Trigger a manual sync
494
+ */
495
+ async sync(opts?: {
496
+ trigger?: 'ws' | 'local' | 'poll';
497
+ }): Promise<SyncResult> {
498
+ // Dedupe concurrent sync calls
499
+ if (this.syncPromise) {
500
+ // A sync is already in-flight; queue one more run so we don't miss
501
+ // mutations enqueued during the current cycle (important in realtime mode).
502
+ this.syncRequestedWhileRunning = true;
503
+ return this.syncPromise;
504
+ }
505
+
506
+ if (
507
+ !this.isEnabled() ||
508
+ this.isDestroyed ||
509
+ this.state.connectionState === 'disconnected'
510
+ ) {
511
+ return {
512
+ success: false,
513
+ pushedCommits: 0,
514
+ pullRounds: 0,
515
+ pullResponse: { ok: true, subscriptions: [] },
516
+ error: createSyncError('SYNC_ERROR', 'Sync not enabled'),
517
+ };
518
+ }
519
+
520
+ this.syncPromise = this.performSyncLoop(opts?.trigger);
521
+ try {
522
+ return await this.syncPromise;
523
+ } finally {
524
+ this.syncPromise = null;
525
+ }
526
+ }
527
+
528
+ private async performSyncLoop(
529
+ trigger?: 'ws' | 'local' | 'poll'
530
+ ): Promise<SyncResult> {
531
+ let lastResult: SyncResult = {
532
+ success: false,
533
+ pushedCommits: 0,
534
+ pullRounds: 0,
535
+ pullResponse: { ok: true, subscriptions: [] },
536
+ error: createSyncError('SYNC_ERROR', 'Sync not started'),
537
+ };
538
+
539
+ do {
540
+ this.syncRequestedWhileRunning = false;
541
+ lastResult = await this.performSyncOnce(trigger);
542
+ // After the first iteration, clear trigger context
543
+ trigger = undefined;
544
+ // If the sync failed, let retry logic handle backoff instead of tight looping.
545
+ if (!lastResult.success) break;
546
+ } while (
547
+ this.syncRequestedWhileRunning &&
548
+ !this.isDestroyed &&
549
+ this.isEnabled()
550
+ );
551
+
552
+ return lastResult;
553
+ }
554
+
555
+ private async performSyncOnce(
556
+ trigger?: 'ws' | 'local' | 'poll'
557
+ ): Promise<SyncResult> {
558
+ const timestamp = Date.now();
559
+ const startedAtMs = timestamp;
560
+ const triggerLabel = resolveSyncTriggerLabel(trigger);
561
+ this.updateState({ isSyncing: true });
562
+ this.emit('sync:start', { timestamp });
563
+ countSyncMetric('sync.client.sync.attempts', 1, {
564
+ attributes: { trigger: triggerLabel },
565
+ });
566
+
567
+ try {
568
+ const pullApplyTimestamp = Date.now();
569
+ const result = await startSyncSpan(
570
+ {
571
+ name: 'sync.client.sync',
572
+ op: 'sync.client.sync',
573
+ attributes: { trigger: triggerLabel },
574
+ },
575
+ () =>
576
+ syncOnce(this.config.db, this.config.transport, this.config.shapes, {
577
+ clientId: this.config.clientId!,
578
+ actorId: this.config.actorId ?? undefined,
579
+ plugins: this.config.plugins,
580
+ subscriptions: this.config
581
+ .subscriptions as SyncSubscriptionRequest[],
582
+ limitCommits: this.config.limitCommits,
583
+ limitSnapshotRows: this.config.limitSnapshotRows,
584
+ maxSnapshotPages: this.config.maxSnapshotPages,
585
+ stateId: this.config.stateId,
586
+ trigger,
587
+ })
588
+ );
589
+
590
+ const syncResult: SyncResult = {
591
+ success: true,
592
+ pushedCommits: result.pushedCommits,
593
+ pullRounds: result.pullRounds,
594
+ pullResponse: result.pullResponse,
595
+ };
596
+
597
+ // Update fingerprint mutation timestamps for server-applied changes so wa-sqlite
598
+ // query hooks rerender on remote changes (not just local mutations).
599
+ this.recordMutationTimestampsFromPullResponse(
600
+ result.pullResponse,
601
+ pullApplyTimestamp
602
+ );
603
+
604
+ this.updateState({
605
+ isSyncing: false,
606
+ lastSyncAt: Date.now(),
607
+ error: null,
608
+ retryCount: 0,
609
+ isRetrying: false,
610
+ });
611
+
612
+ this.emit('sync:complete', {
613
+ timestamp: Date.now(),
614
+ pushedCommits: result.pushedCommits,
615
+ pullRounds: result.pullRounds,
616
+ pullResponse: result.pullResponse,
617
+ });
618
+
619
+ // Emit data change for any tables that had changes
620
+ const changedTables = this.extractChangedTables(result.pullResponse);
621
+ if (changedTables.length > 0) {
622
+ this.emit('data:change', {
623
+ scopes: changedTables,
624
+ timestamp: Date.now(),
625
+ });
626
+ this.config.onDataChange?.(changedTables);
627
+ }
628
+
629
+ // Refresh outbox stats (fire-and-forget — don't block sync:complete)
630
+ this.refreshOutboxStats().catch((error) => {
631
+ console.warn(
632
+ '[SyncEngine] Failed to refresh outbox stats after sync:',
633
+ error
634
+ );
635
+ });
636
+
637
+ const durationMs = Math.max(0, Date.now() - startedAtMs);
638
+ countSyncMetric('sync.client.sync.results', 1, {
639
+ attributes: {
640
+ trigger: triggerLabel,
641
+ status: 'success',
642
+ },
643
+ });
644
+ distributionSyncMetric('sync.client.sync.duration_ms', durationMs, {
645
+ unit: 'millisecond',
646
+ attributes: {
647
+ trigger: triggerLabel,
648
+ status: 'success',
649
+ },
650
+ });
651
+ distributionSyncMetric(
652
+ 'sync.client.sync.pushed_commits',
653
+ result.pushedCommits,
654
+ {
655
+ attributes: { trigger: triggerLabel },
656
+ }
657
+ );
658
+ distributionSyncMetric(
659
+ 'sync.client.sync.pull_rounds',
660
+ result.pullRounds,
661
+ {
662
+ attributes: { trigger: triggerLabel },
663
+ }
664
+ );
665
+
666
+ return syncResult;
667
+ } catch (err) {
668
+ const error = createSyncError(
669
+ 'SYNC_ERROR',
670
+ err instanceof Error ? err.message : 'Sync failed',
671
+ err instanceof Error ? err : undefined
672
+ );
673
+
674
+ this.updateState({
675
+ isSyncing: false,
676
+ error,
677
+ retryCount: this.state.retryCount + 1,
678
+ isRetrying: false,
679
+ });
680
+
681
+ this.handleError(error);
682
+
683
+ const durationMs = Math.max(0, Date.now() - startedAtMs);
684
+ countSyncMetric('sync.client.sync.results', 1, {
685
+ attributes: {
686
+ trigger: triggerLabel,
687
+ status: 'error',
688
+ },
689
+ });
690
+ distributionSyncMetric('sync.client.sync.duration_ms', durationMs, {
691
+ unit: 'millisecond',
692
+ attributes: {
693
+ trigger: triggerLabel,
694
+ status: 'error',
695
+ },
696
+ });
697
+ captureSyncException(err, {
698
+ event: 'sync.client.sync',
699
+ trigger: triggerLabel,
700
+ });
701
+
702
+ // Schedule retry if under max retries
703
+ const maxRetries = this.config.maxRetries ?? DEFAULT_MAX_RETRIES;
704
+ if (this.state.retryCount < maxRetries) {
705
+ this.scheduleRetry();
706
+ }
707
+
708
+ return {
709
+ success: false,
710
+ pushedCommits: 0,
711
+ pullRounds: 0,
712
+ pullResponse: { ok: true, subscriptions: [] },
713
+ error,
714
+ };
715
+ }
716
+ }
717
+
718
+ private extractChangedTables(response: SyncPullResponse): string[] {
719
+ const tables = new Set<string>();
720
+ for (const sub of response.subscriptions ?? []) {
721
+ // Extract tables from snapshots
722
+ for (const snapshot of sub.snapshots ?? []) {
723
+ if (snapshot.table) {
724
+ tables.add(snapshot.table);
725
+ }
726
+ }
727
+ // Extract tables from commits
728
+ for (const commit of sub.commits ?? []) {
729
+ for (const change of commit.changes ?? []) {
730
+ if (change.table) {
731
+ tables.add(change.table);
732
+ }
733
+ }
734
+ }
735
+ }
736
+
737
+ return Array.from(tables);
738
+ }
739
+
740
+ /**
741
+ * Apply changes delivered inline over WebSocket for instant UI updates.
742
+ * Returns true if changes were applied and cursor updated successfully,
743
+ * false if anything failed (caller should fall back to HTTP sync).
744
+ */
745
+ private async applyWsDeliveredChanges(
746
+ changes: SyncChange[],
747
+ cursor: number
748
+ ): Promise<boolean> {
749
+ try {
750
+ await this.config.db.transaction().execute(async (trx) => {
751
+ for (const change of changes) {
752
+ const handler = this.config.shapes.get(change.table);
753
+ if (!handler) {
754
+ throw new Error(
755
+ `Missing client table handler for WS change table "${change.table}"`
756
+ );
757
+ }
758
+ await handler.applyChange({ trx }, change);
759
+ }
760
+
761
+ // Update subscription cursors
762
+ const stateId = this.config.stateId ?? 'default';
763
+ await sql`
764
+ update ${sql.table('sync_subscription_state')}
765
+ set ${sql.ref('cursor')} = ${sql.val(cursor)}
766
+ where ${sql.ref('state_id')} = ${sql.val(stateId)}
767
+ and ${sql.ref('cursor')} < ${sql.val(cursor)}
768
+ `.execute(trx);
769
+ });
770
+
771
+ // Update mutation timestamps BEFORE emitting data:change so that
772
+ // React hooks re-querying the DB see fresh fingerprints immediately.
773
+ const now = Date.now();
774
+ for (const change of changes) {
775
+ if (!change.table || !change.row_id) continue;
776
+ if (change.op === 'delete') {
777
+ this.mutationTimestamps.delete(`${change.table}:${change.row_id}`);
778
+ } else {
779
+ this.bumpMutationTimestamp(change.table, change.row_id, now);
780
+ }
781
+ }
782
+
783
+ // Emit data change for immediate UI update
784
+ const changedTables = [...new Set(changes.map((c) => c.table))];
785
+ if (changedTables.length > 0) {
786
+ this.emit('data:change', {
787
+ scopes: changedTables,
788
+ timestamp: Date.now(),
789
+ });
790
+ this.config.onDataChange?.(changedTables);
791
+ }
792
+
793
+ return true;
794
+ } catch {
795
+ return false;
796
+ }
797
+ }
798
+
799
+ /**
800
+ * Handle WS-delivered changes: apply them and decide whether to skip HTTP pull.
801
+ * Falls back to full HTTP sync when conditions require it.
802
+ */
803
+ private async handleWsDelivery(
804
+ changes: SyncChange[],
805
+ cursor: number
806
+ ): Promise<void> {
807
+ // If a sync is already in-flight, let it handle everything
808
+ if (this.syncPromise) {
809
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
810
+ attributes: { path: 'inflight_sync' },
811
+ });
812
+ this.triggerSyncInBackground(
813
+ { trigger: 'ws' },
814
+ 'ws delivery with in-flight sync'
815
+ );
816
+ return;
817
+ }
818
+
819
+ // If there are pending outbox commits, need to push via HTTP
820
+ if (this.state.pendingCount > 0) {
821
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
822
+ attributes: { path: 'pending_outbox' },
823
+ });
824
+ this.triggerSyncInBackground(
825
+ { trigger: 'ws' },
826
+ 'ws delivery with pending outbox'
827
+ );
828
+ return;
829
+ }
830
+
831
+ // If afterPull plugins exist, inline WS changes may require transforms
832
+ // (e.g. decryption). Fall back to HTTP sync and do not apply inline payload.
833
+ const hasAfterPullPlugins = this.config.plugins?.some(
834
+ (p) => typeof p.afterPull === 'function'
835
+ );
836
+ if (hasAfterPullPlugins) {
837
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
838
+ attributes: { path: 'after_pull_plugins' },
839
+ });
840
+ this.triggerSyncInBackground(
841
+ { trigger: 'ws' },
842
+ 'ws delivery with afterPull plugins'
843
+ );
844
+ return;
845
+ }
846
+
847
+ // Apply changes + update cursor
848
+ const inlineApplyStartedAtMs = Date.now();
849
+ const applied = await this.applyWsDeliveredChanges(changes, cursor);
850
+ const inlineApplyDurationMs = Math.max(
851
+ 0,
852
+ Date.now() - inlineApplyStartedAtMs
853
+ );
854
+ distributionSyncMetric(
855
+ 'sync.client.ws.inline_apply.duration_ms',
856
+ inlineApplyDurationMs,
857
+ {
858
+ unit: 'millisecond',
859
+ }
860
+ );
861
+
862
+ if (!applied) {
863
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
864
+ attributes: { path: 'inline_fallback' },
865
+ });
866
+ this.triggerSyncInBackground(
867
+ { trigger: 'ws' },
868
+ 'ws inline apply fallback'
869
+ );
870
+ return;
871
+ }
872
+
873
+ // All clear — skip HTTP pull entirely
874
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
875
+ attributes: { path: 'inline_applied' },
876
+ });
877
+ this.updateState({
878
+ lastSyncAt: Date.now(),
879
+ error: null,
880
+ retryCount: 0,
881
+ isRetrying: false,
882
+ });
883
+
884
+ this.emit('sync:complete', {
885
+ timestamp: Date.now(),
886
+ pushedCommits: 0,
887
+ pullRounds: 0,
888
+ pullResponse: { ok: true, subscriptions: [] },
889
+ });
890
+
891
+ this.refreshOutboxStats().catch((error) => {
892
+ console.warn(
893
+ '[SyncEngine] Failed to refresh outbox stats after WS apply:',
894
+ error
895
+ );
896
+ });
897
+ }
898
+
899
+ private timestampCounter = 0;
900
+
901
+ private nextPreciseTimestamp(now: number): number {
902
+ // Use sub-millisecond precision by combining timestamp with atomic counter
903
+ // This prevents race conditions in concurrent mutations while maintaining
904
+ // millisecond-level compatibility with existing code.
905
+ return now + (this.timestampCounter++ % 1000) / 1000;
906
+ }
907
+
908
+ private bumpMutationTimestamp(
909
+ table: string,
910
+ rowId: string,
911
+ now: number
912
+ ): void {
913
+ const key = `${table}:${rowId}`;
914
+ const preciseNow = this.nextPreciseTimestamp(now);
915
+ const prev = this.mutationTimestamps.get(key) ?? 0;
916
+ this.mutationTimestamps.set(key, Math.max(preciseNow, prev + 0.001));
917
+ }
918
+
919
+ private bumpTableMutationTimestamp(table: string, now: number): void {
920
+ const preciseNow = this.nextPreciseTimestamp(now);
921
+ const prev = this.tableMutationTimestamps.get(table) ?? 0;
922
+ this.tableMutationTimestamps.set(table, Math.max(preciseNow, prev + 0.001));
923
+ }
924
+
925
+ /**
926
+ * Record local mutations that were already applied to the DB.
927
+ *
928
+ * This updates in-memory mutation timestamps (for fingerprint-based rerenders),
929
+ * and emits a single `data:change` event for the affected tables.
930
+ *
931
+ * This is intentionally separate from applyLocalMutation() so callers that
932
+ * perform their own DB transactions (e.g. `useMutations`) can still keep UI
933
+ * updates correct without double-writing.
934
+ */
935
+ recordLocalMutations(
936
+ inputs: Array<{
937
+ table: string;
938
+ rowId: string;
939
+ op: 'upsert' | 'delete';
940
+ }>,
941
+ now = Date.now()
942
+ ): void {
943
+ const affectedTables = new Set<string>();
944
+
945
+ for (const input of inputs) {
946
+ if (!input.table || !input.rowId) continue;
947
+ affectedTables.add(input.table);
948
+
949
+ if (input.op === 'delete') {
950
+ this.mutationTimestamps.delete(`${input.table}:${input.rowId}`);
951
+ continue;
952
+ }
953
+
954
+ this.bumpMutationTimestamp(input.table, input.rowId, now);
955
+ }
956
+
957
+ if (affectedTables.size > 0) {
958
+ this.emit('data:change', {
959
+ scopes: Array.from(affectedTables),
960
+ timestamp: Date.now(),
961
+ });
962
+ this.config.onDataChange?.(Array.from(affectedTables));
963
+ }
964
+ }
965
+
966
+ private recordMutationTimestampsFromPullResponse(
967
+ response: SyncPullResponse,
968
+ now: number
969
+ ): void {
970
+ for (const sub of response.subscriptions ?? []) {
971
+ // Mark snapshot tables as changed so bootstrap/resnapshot updates
972
+ // propagate without storing per-row timestamps for massive snapshots.
973
+ for (const snapshot of sub.snapshots ?? []) {
974
+ if (!snapshot.table) continue;
975
+ this.bumpTableMutationTimestamp(snapshot.table, now);
976
+ }
977
+
978
+ for (const commit of sub.commits ?? []) {
979
+ for (const change of commit.changes ?? []) {
980
+ const table = change.table;
981
+ const rowId = change.row_id;
982
+ if (!table || !rowId) continue;
983
+
984
+ if (change.op === 'delete') {
985
+ this.mutationTimestamps.delete(`${table}:${rowId}`);
986
+ } else {
987
+ this.bumpMutationTimestamp(table, rowId, now);
988
+ }
989
+ }
990
+ }
991
+ }
992
+ }
993
+
994
+ private scheduleRetry(): void {
995
+ if (this.retryTimeoutId) {
996
+ clearTimeout(this.retryTimeoutId);
997
+ }
998
+
999
+ const delay = calculateRetryDelay(this.state.retryCount);
1000
+ this.updateState({ isRetrying: true });
1001
+
1002
+ this.retryTimeoutId = setTimeout(() => {
1003
+ this.retryTimeoutId = null;
1004
+ if (!this.isDestroyed) {
1005
+ this.triggerSyncInBackground(undefined, 'retry timer');
1006
+ }
1007
+ }, delay);
1008
+ }
1009
+
1010
+ private handleError(error: SyncError): void {
1011
+ this.emit('sync:error', error);
1012
+ this.config.onError?.(error);
1013
+ }
1014
+
1015
+ private triggerSyncInBackground(
1016
+ opts?: { trigger?: 'ws' | 'local' | 'poll' },
1017
+ reason = 'background'
1018
+ ): void {
1019
+ void this.sync(opts).catch((error) => {
1020
+ console.error(
1021
+ `[SyncEngine] Unexpected sync failure during ${reason}:`,
1022
+ error
1023
+ );
1024
+ });
1025
+ }
1026
+
1027
+ private setupPolling(): void {
1028
+ this.stopPolling();
1029
+
1030
+ const interval = this.config.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
1031
+ this.pollerId = setInterval(() => {
1032
+ if (!this.state.isSyncing && !this.isDestroyed) {
1033
+ this.triggerSyncInBackground(undefined, 'polling interval');
1034
+ }
1035
+ }, interval);
1036
+
1037
+ this.setConnectionState('connected');
1038
+ }
1039
+
1040
+ private stopPolling(): void {
1041
+ if (this.pollerId) {
1042
+ clearInterval(this.pollerId);
1043
+ this.pollerId = null;
1044
+ }
1045
+ }
1046
+
1047
+ private setupRealtime(): void {
1048
+ if (!isRealtimeTransport(this.config.transport)) {
1049
+ console.warn(
1050
+ '[SyncEngine] realtimeEnabled=true but transport does not support realtime. Falling back to polling.'
1051
+ );
1052
+ this.updateState({ transportMode: 'polling' });
1053
+ this.setupPolling();
1054
+ return;
1055
+ }
1056
+
1057
+ this.setConnectionState('connecting');
1058
+
1059
+ const transport = this.config.transport as RealtimeTransportLike;
1060
+
1061
+ // Wire up presence events if transport supports them
1062
+ if (transport.onPresenceEvent) {
1063
+ this.realtimePresenceUnsub = transport.onPresenceEvent((event) => {
1064
+ if (event.action === 'snapshot' && event.entries) {
1065
+ this.updatePresence(event.scopeKey, event.entries);
1066
+ } else if (
1067
+ event.action === 'join' ||
1068
+ event.action === 'leave' ||
1069
+ event.action === 'update'
1070
+ ) {
1071
+ this.handlePresenceEvent({
1072
+ action: event.action,
1073
+ scopeKey: event.scopeKey,
1074
+ clientId: event.clientId ?? '',
1075
+ actorId: event.actorId ?? '',
1076
+ metadata: event.metadata,
1077
+ });
1078
+ }
1079
+ });
1080
+ }
1081
+
1082
+ this.realtimeDisconnect = transport.connect(
1083
+ { clientId: this.config.clientId! },
1084
+ (event) => {
1085
+ if (event.event === 'sync') {
1086
+ countSyncMetric('sync.client.ws.events', 1, {
1087
+ attributes: { type: 'sync' },
1088
+ });
1089
+ const hasInlineChanges =
1090
+ Array.isArray(event.data.changes) && event.data.changes.length > 0;
1091
+ const cursor = event.data.cursor;
1092
+
1093
+ if (hasInlineChanges && typeof cursor === 'number') {
1094
+ // WS delivered changes + cursor — may skip HTTP pull
1095
+ this.handleWsDelivery(event.data.changes as SyncChange[], cursor);
1096
+ } else {
1097
+ // Cursor-only wake-up or no cursor — must HTTP sync
1098
+ countSyncMetric('sync.client.ws.delivery.events', 1, {
1099
+ attributes: { path: 'cursor_wakeup' },
1100
+ });
1101
+ this.triggerSyncInBackground({ trigger: 'ws' }, 'ws cursor wakeup');
1102
+ }
1103
+ }
1104
+ },
1105
+ (state) => {
1106
+ switch (state) {
1107
+ case 'connected': {
1108
+ const wasConnectedBefore = this.hasRealtimeConnectedOnce;
1109
+ this.hasRealtimeConnectedOnce = true;
1110
+ this.setConnectionState('connected');
1111
+ this.stopFallbackPolling();
1112
+ this.triggerSyncInBackground(undefined, 'realtime connected state');
1113
+ if (wasConnectedBefore) {
1114
+ this.scheduleRealtimeReconnectCatchupSync();
1115
+ }
1116
+ break;
1117
+ }
1118
+ case 'connecting':
1119
+ this.setConnectionState('connecting');
1120
+ break;
1121
+ case 'disconnected':
1122
+ this.setConnectionState('reconnecting');
1123
+ this.startFallbackPolling();
1124
+ break;
1125
+ }
1126
+ }
1127
+ );
1128
+ }
1129
+
1130
+ private stopRealtime(): void {
1131
+ if (this.realtimeCatchupTimeoutId) {
1132
+ clearTimeout(this.realtimeCatchupTimeoutId);
1133
+ this.realtimeCatchupTimeoutId = null;
1134
+ }
1135
+ if (this.realtimePresenceUnsub) {
1136
+ this.realtimePresenceUnsub();
1137
+ this.realtimePresenceUnsub = null;
1138
+ }
1139
+ if (this.realtimeDisconnect) {
1140
+ this.realtimeDisconnect();
1141
+ this.realtimeDisconnect = null;
1142
+ }
1143
+ this.stopFallbackPolling();
1144
+ }
1145
+
1146
+ private scheduleRealtimeReconnectCatchupSync(): void {
1147
+ if (this.realtimeCatchupTimeoutId) {
1148
+ clearTimeout(this.realtimeCatchupTimeoutId);
1149
+ }
1150
+
1151
+ this.realtimeCatchupTimeoutId = setTimeout(() => {
1152
+ this.realtimeCatchupTimeoutId = null;
1153
+
1154
+ if (this.isDestroyed || !this.isEnabled()) return;
1155
+ if (this.state.connectionState !== 'connected') return;
1156
+
1157
+ this.triggerSyncInBackground(undefined, 'realtime reconnect catchup');
1158
+ }, REALTIME_RECONNECT_CATCHUP_DELAY_MS);
1159
+ }
1160
+
1161
+ private startFallbackPolling(): void {
1162
+ if (this.fallbackPollerId) return;
1163
+
1164
+ const interval = this.config.realtimeFallbackPollMs ?? 30_000;
1165
+ this.fallbackPollerId = setInterval(() => {
1166
+ if (!this.state.isSyncing && !this.isDestroyed) {
1167
+ this.triggerSyncInBackground(undefined, 'realtime fallback poll');
1168
+ }
1169
+ }, interval);
1170
+ }
1171
+
1172
+ private stopFallbackPolling(): void {
1173
+ if (this.fallbackPollerId) {
1174
+ clearInterval(this.fallbackPollerId);
1175
+ this.fallbackPollerId = null;
1176
+ }
1177
+ }
1178
+
1179
+ /**
1180
+ * Clear all in-memory mutation state and emit data:change so UI re-renders.
1181
+ * Call this after deleting local data (e.g. reset flow) so that React hooks
1182
+ * recompute fingerprints from scratch instead of seeing stale timestamps.
1183
+ */
1184
+ resetLocalState(): void {
1185
+ const tables = [...this.tableMutationTimestamps.keys()];
1186
+ this.mutationTimestamps.clear();
1187
+ this.tableMutationTimestamps.clear();
1188
+
1189
+ if (tables.length > 0) {
1190
+ this.emit('data:change', {
1191
+ scopes: tables,
1192
+ timestamp: Date.now(),
1193
+ });
1194
+ this.config.onDataChange?.(tables);
1195
+ }
1196
+ }
1197
+
1198
+ /**
1199
+ * Reconnect
1200
+ */
1201
+ reconnect(): void {
1202
+ if (this.isDestroyed || !this.isEnabled()) return;
1203
+
1204
+ if (
1205
+ this.state.transportMode === 'realtime' &&
1206
+ isRealtimeTransport(this.config.transport)
1207
+ ) {
1208
+ // If we previously disconnected, we need to re-register callbacks via connect().
1209
+ if (!this.realtimeDisconnect) {
1210
+ this.setupRealtime();
1211
+ } else {
1212
+ this.config.transport.reconnect();
1213
+ }
1214
+ return;
1215
+ }
1216
+
1217
+ // Polling mode: restart the poller and trigger a sync immediately.
1218
+ if (this.state.transportMode === 'polling') {
1219
+ this.setupPolling();
1220
+ this.triggerSyncInBackground(undefined, 'reconnect');
1221
+ }
1222
+ }
1223
+
1224
+ /**
1225
+ * Disconnect (pause syncing)
1226
+ */
1227
+ disconnect(): void {
1228
+ this.stop();
1229
+ }
1230
+
1231
+ /**
1232
+ * Refresh outbox statistics
1233
+ */
1234
+ async refreshOutboxStats(options?: { emit?: boolean }): Promise<OutboxStats> {
1235
+ const db = this.config.db;
1236
+
1237
+ const res = await sql<{ status: OutboxCommitStatus; count: number }>`
1238
+ select
1239
+ ${sql.ref('status')},
1240
+ count(${sql.ref('id')}) as ${sql.ref('count')}
1241
+ from ${sql.table('sync_outbox_commits')}
1242
+ group by ${sql.ref('status')}
1243
+ `.execute(db);
1244
+ const rows = res.rows;
1245
+
1246
+ const stats: OutboxStats = {
1247
+ pending: 0,
1248
+ sending: 0,
1249
+ failed: 0,
1250
+ acked: 0,
1251
+ total: 0,
1252
+ };
1253
+
1254
+ for (const row of rows) {
1255
+ const count = Number(row.count);
1256
+ switch (row.status) {
1257
+ case 'pending':
1258
+ stats.pending = count;
1259
+ break;
1260
+ case 'sending':
1261
+ stats.sending = count;
1262
+ break;
1263
+ case 'failed':
1264
+ stats.failed = count;
1265
+ break;
1266
+ case 'acked':
1267
+ stats.acked = count;
1268
+ break;
1269
+ }
1270
+ stats.total += count;
1271
+ }
1272
+
1273
+ this.updateState({ pendingCount: stats.pending + stats.failed });
1274
+ if (options?.emit !== false) {
1275
+ this.emit('outbox:change', {
1276
+ pendingCount: stats.pending,
1277
+ sendingCount: stats.sending,
1278
+ failedCount: stats.failed,
1279
+ ackedCount: stats.acked,
1280
+ });
1281
+ }
1282
+
1283
+ return stats;
1284
+ }
1285
+
1286
+ /**
1287
+ * Get pending conflicts with operation details from outbox
1288
+ */
1289
+ async getConflicts(): Promise<ConflictInfo[]> {
1290
+ // Join with outbox to get operation details
1291
+ const res = await sql<{
1292
+ id: string;
1293
+ outbox_commit_id: string;
1294
+ client_commit_id: string;
1295
+ op_index: number;
1296
+ result_status: ConflictResultStatus;
1297
+ message: string;
1298
+ code: string | null;
1299
+ server_version: number | null;
1300
+ server_row_json: string | null;
1301
+ created_at: number;
1302
+ operations_json: string;
1303
+ }>`
1304
+ select
1305
+ ${sql.ref('c.id')},
1306
+ ${sql.ref('c.outbox_commit_id')},
1307
+ ${sql.ref('c.client_commit_id')},
1308
+ ${sql.ref('c.op_index')},
1309
+ ${sql.ref('c.result_status')},
1310
+ ${sql.ref('c.message')},
1311
+ ${sql.ref('c.code')},
1312
+ ${sql.ref('c.server_version')},
1313
+ ${sql.ref('c.server_row_json')},
1314
+ ${sql.ref('c.created_at')},
1315
+ ${sql.ref('oc.operations_json')}
1316
+ from ${sql.table('sync_conflicts')} as ${sql.ref('c')}
1317
+ inner join ${sql.table('sync_outbox_commits')} as ${sql.ref('oc')}
1318
+ on ${sql.ref('oc.id')} = ${sql.ref('c.outbox_commit_id')}
1319
+ where ${sql.ref('c.resolved_at')} is null
1320
+ order by ${sql.ref('c.created_at')} desc
1321
+ `.execute(this.config.db);
1322
+ const rows = res.rows;
1323
+
1324
+ return rows.map((row) => {
1325
+ // Extract operation details from outbox
1326
+ let table = '';
1327
+ let rowId = '';
1328
+ let localPayload: Record<string, unknown> | null = null;
1329
+
1330
+ if (row.operations_json) {
1331
+ try {
1332
+ const operations: unknown = JSON.parse(row.operations_json);
1333
+ if (Array.isArray(operations)) {
1334
+ const op = operations[row.op_index];
1335
+ if (isRecord(op)) {
1336
+ if (typeof op.table === 'string') table = op.table;
1337
+ if (typeof op.row_id === 'string') rowId = op.row_id;
1338
+ localPayload =
1339
+ op.payload === null
1340
+ ? null
1341
+ : isRecord(op.payload)
1342
+ ? op.payload
1343
+ : null;
1344
+ }
1345
+ }
1346
+ } catch {
1347
+ // Ignore parse errors
1348
+ }
1349
+ }
1350
+
1351
+ return {
1352
+ id: row.id,
1353
+ outboxCommitId: row.outbox_commit_id,
1354
+ clientCommitId: row.client_commit_id,
1355
+ opIndex: row.op_index,
1356
+ resultStatus: row.result_status,
1357
+ message: row.message,
1358
+ code: row.code,
1359
+ serverVersion: row.server_version,
1360
+ serverRowJson: row.server_row_json,
1361
+ createdAt: row.created_at,
1362
+ table,
1363
+ rowId,
1364
+ localPayload,
1365
+ };
1366
+ });
1367
+ }
1368
+
1369
+ /**
1370
+ * Update subscriptions dynamically
1371
+ */
1372
+ updateSubscriptions(
1373
+ subscriptions: Array<Omit<SyncSubscriptionRequest, 'cursor'>>
1374
+ ): void {
1375
+ this.config.subscriptions = subscriptions;
1376
+ // Trigger a sync to apply new subscriptions
1377
+ this.triggerSyncInBackground(undefined, 'subscription update');
1378
+ }
1379
+
1380
+ /**
1381
+ * Apply local mutations immediately to the database and emit change events.
1382
+ * Used for instant UI updates before the sync cycle completes.
1383
+ */
1384
+ async applyLocalMutation(
1385
+ inputs: Array<{
1386
+ table: string;
1387
+ rowId: string;
1388
+ op: 'upsert' | 'delete';
1389
+ payload?: Record<string, unknown> | null;
1390
+ }>
1391
+ ): Promise<void> {
1392
+ const db = this.config.db;
1393
+ const shapes = this.config.shapes;
1394
+ const affectedTables = new Set<string>();
1395
+ const now = Date.now();
1396
+
1397
+ await db.transaction().execute(async (trx) => {
1398
+ for (const input of inputs) {
1399
+ const handler = shapes.get(input.table);
1400
+ if (!handler) continue;
1401
+
1402
+ affectedTables.add(input.table);
1403
+
1404
+ const change: SyncChange = {
1405
+ table: input.table,
1406
+ row_id: input.rowId,
1407
+ op: input.op,
1408
+ scopes: {},
1409
+ // For delete ops, row_json should be null; for upserts, default to empty object
1410
+ row_json: input.op === 'delete' ? null : (input.payload ?? {}),
1411
+ // null indicates local optimistic change (no server version yet)
1412
+ row_version: null,
1413
+ };
1414
+
1415
+ await handler.applyChange({ trx }, change);
1416
+ }
1417
+ });
1418
+
1419
+ // Track mutation timestamps for fingerprint-based rerender optimization (in-memory only)
1420
+ this.recordLocalMutations(
1421
+ inputs
1422
+ .filter((i) => affectedTables.has(i.table))
1423
+ .map((i) => ({ table: i.table, rowId: i.rowId, op: i.op })),
1424
+ now
1425
+ );
1426
+ }
1427
+
1428
+ /**
1429
+ * Clear failed commits from the outbox.
1430
+ * Use this to discard commits that keep failing (e.g., version conflicts).
1431
+ */
1432
+ async clearFailedCommits(): Promise<number> {
1433
+ const db = this.config.db;
1434
+
1435
+ const res = await sql`
1436
+ delete from ${sql.table('sync_outbox_commits')}
1437
+ where ${sql.ref('status')} = ${sql.val('failed')}
1438
+ `.execute(db);
1439
+ const count = Number(res.numAffectedRows ?? 0);
1440
+
1441
+ await this.refreshOutboxStats();
1442
+ return count;
1443
+ }
1444
+
1445
+ /**
1446
+ * Clear all pending and failed commits from the outbox.
1447
+ * Use this to reset the outbox completely (e.g., for testing).
1448
+ */
1449
+ async clearAllCommits(): Promise<number> {
1450
+ const db = this.config.db;
1451
+
1452
+ const res = await sql`
1453
+ delete from ${sql.table('sync_outbox_commits')}
1454
+ where ${sql.ref('status')} in (${sql.join([
1455
+ sql.val('pending'),
1456
+ sql.val('failed'),
1457
+ ])})
1458
+ `.execute(db);
1459
+ const count = Number(res.numAffectedRows ?? 0);
1460
+
1461
+ await this.refreshOutboxStats();
1462
+ return count;
1463
+ }
1464
+ }