@syncular/client 0.0.6-95 → 0.1.0

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 (356) hide show
  1. package/README.md +713 -11
  2. package/dist/auth-leases.d.ts +11 -0
  3. package/dist/auth-leases.d.ts.map +1 -0
  4. package/dist/auth-leases.js +140 -0
  5. package/dist/auth-leases.js.map +1 -0
  6. package/dist/blob-limits.d.ts +15 -0
  7. package/dist/blob-limits.d.ts.map +1 -0
  8. package/dist/blob-limits.js +66 -0
  9. package/dist/blob-limits.js.map +1 -0
  10. package/dist/bridge-client.d.ts +69 -0
  11. package/dist/bridge-client.d.ts.map +1 -0
  12. package/dist/bridge-client.js +282 -0
  13. package/dist/bridge-client.js.map +1 -0
  14. package/dist/client-config.d.ts +6 -0
  15. package/dist/client-config.d.ts.map +1 -0
  16. package/dist/client-config.js +18 -0
  17. package/dist/client-config.js.map +1 -0
  18. package/dist/client.d.ts +65 -397
  19. package/dist/client.d.ts.map +1 -1
  20. package/dist/client.js +150 -960
  21. package/dist/client.js.map +1 -1
  22. package/dist/command-history.d.ts +35 -0
  23. package/dist/command-history.d.ts.map +1 -0
  24. package/dist/command-history.js +378 -0
  25. package/dist/command-history.js.map +1 -0
  26. package/dist/console-diagnostics.d.ts +33 -0
  27. package/dist/console-diagnostics.d.ts.map +1 -0
  28. package/dist/console-diagnostics.js +496 -0
  29. package/dist/console-diagnostics.js.map +1 -0
  30. package/dist/crdt-yjs/index.d.ts +4 -0
  31. package/dist/crdt-yjs/index.d.ts.map +1 -0
  32. package/dist/crdt-yjs/index.js +4 -0
  33. package/dist/crdt-yjs/index.js.map +1 -0
  34. package/dist/crdt-yjs/webview-host-facade.d.ts +126 -0
  35. package/dist/crdt-yjs/webview-host-facade.d.ts.map +1 -0
  36. package/dist/crdt-yjs/webview-host-facade.js +284 -0
  37. package/dist/crdt-yjs/webview-host-facade.js.map +1 -0
  38. package/dist/crdt-yjs/yjs-document-field-adapter.d.ts +153 -0
  39. package/dist/crdt-yjs/yjs-document-field-adapter.d.ts.map +1 -0
  40. package/dist/crdt-yjs/yjs-document-field-adapter.js +406 -0
  41. package/dist/crdt-yjs/yjs-document-field-adapter.js.map +1 -0
  42. package/dist/crdt-yjs/yjs-prosemirror-bridge.d.ts +73 -0
  43. package/dist/crdt-yjs/yjs-prosemirror-bridge.d.ts.map +1 -0
  44. package/dist/crdt-yjs/yjs-prosemirror-bridge.js +169 -0
  45. package/dist/crdt-yjs/yjs-prosemirror-bridge.js.map +1 -0
  46. package/dist/database.d.ts +83 -0
  47. package/dist/database.d.ts.map +1 -0
  48. package/dist/database.js +916 -0
  49. package/dist/database.js.map +1 -0
  50. package/dist/diagnostics.d.ts +10 -0
  51. package/dist/diagnostics.d.ts.map +1 -0
  52. package/dist/diagnostics.js +83 -0
  53. package/dist/diagnostics.js.map +1 -0
  54. package/dist/errors.d.ts +24 -0
  55. package/dist/errors.d.ts.map +1 -0
  56. package/dist/errors.js +211 -0
  57. package/dist/errors.js.map +1 -0
  58. package/dist/generated-bridge.d.ts +365 -0
  59. package/dist/generated-bridge.d.ts.map +1 -0
  60. package/dist/generated-bridge.js +250 -0
  61. package/dist/generated-bridge.js.map +1 -0
  62. package/dist/index.d.ts +13 -23
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +14 -25
  65. package/dist/index.js.map +1 -1
  66. package/dist/mutations.d.ts +4 -72
  67. package/dist/mutations.d.ts.map +1 -1
  68. package/dist/mutations.js +2 -568
  69. package/dist/mutations.js.map +1 -1
  70. package/dist/network.d.ts +3 -0
  71. package/dist/network.d.ts.map +1 -0
  72. package/dist/network.js +17 -0
  73. package/dist/network.js.map +1 -0
  74. package/dist/react/index.d.ts +169 -0
  75. package/dist/react/index.d.ts.map +1 -0
  76. package/dist/react/index.js +628 -0
  77. package/dist/react/index.js.map +1 -0
  78. package/dist/react-native/index.d.ts +35 -0
  79. package/dist/react-native/index.d.ts.map +1 -0
  80. package/dist/react-native/index.js +49 -0
  81. package/dist/react-native/index.js.map +1 -0
  82. package/dist/runtime-contract.d.ts +13 -0
  83. package/dist/runtime-contract.d.ts.map +1 -0
  84. package/dist/runtime-contract.js +24 -0
  85. package/dist/runtime-contract.js.map +1 -0
  86. package/dist/rust-client.d.ts +106 -0
  87. package/dist/rust-client.d.ts.map +1 -0
  88. package/dist/rust-client.js +936 -0
  89. package/dist/rust-client.js.map +1 -0
  90. package/dist/sentry.d.ts +35 -0
  91. package/dist/sentry.d.ts.map +1 -0
  92. package/dist/sentry.js +155 -0
  93. package/dist/sentry.js.map +1 -0
  94. package/dist/sql-safety.d.ts +3 -0
  95. package/dist/sql-safety.d.ts.map +1 -0
  96. package/dist/sql-safety.js +62 -0
  97. package/dist/sql-safety.js.map +1 -0
  98. package/dist/syncular-runtime-artifacts.json +61 -0
  99. package/dist/tauri/index.d.ts +35 -0
  100. package/dist/tauri/index.d.ts.map +1 -0
  101. package/dist/tauri/index.js +114 -0
  102. package/dist/tauri/index.js.map +1 -0
  103. package/dist/types.d.ts +1059 -0
  104. package/dist/types.d.ts.map +1 -0
  105. package/dist/types.js +2 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/wasm/.syncular-wasm-profile +1 -0
  108. package/dist/wasm/syncular-runtime-artifact.json +21 -0
  109. package/dist/wasm/syncular.d.ts +207 -0
  110. package/dist/wasm/syncular.js +2341 -0
  111. package/dist/wasm/syncular_bg.wasm +0 -0
  112. package/dist/wasm/syncular_bg.wasm.d.ts +97 -0
  113. package/dist/wasm-bindings/runtime-contract.d.ts +22 -0
  114. package/dist/wasm-bindings/runtime-contract.d.ts.map +1 -0
  115. package/dist/wasm-bindings/runtime-contract.js +112 -0
  116. package/dist/wasm-bindings/runtime-contract.js.map +1 -0
  117. package/dist/wasm-core/.syncular-wasm-profile +1 -0
  118. package/dist/wasm-core/syncular-runtime-artifact.json +17 -0
  119. package/dist/wasm-core/syncular.d.ts +162 -0
  120. package/dist/wasm-core/syncular.js +1847 -0
  121. package/dist/wasm-core/syncular_bg.wasm +0 -0
  122. package/dist/wasm-core/syncular_bg.wasm.d.ts +77 -0
  123. package/dist/wasm-perf/.syncular-wasm-profile +1 -0
  124. package/dist/wasm-perf/syncular-runtime-artifact.json +21 -0
  125. package/dist/wasm-perf/syncular.d.ts +207 -0
  126. package/dist/wasm-perf/syncular.js +2341 -0
  127. package/dist/wasm-perf/syncular_bg.wasm +0 -0
  128. package/dist/wasm-perf/syncular_bg.wasm.d.ts +97 -0
  129. package/dist/wasm-runtime.d.ts +18 -0
  130. package/dist/wasm-runtime.d.ts.map +1 -0
  131. package/dist/wasm-runtime.js +28 -0
  132. package/dist/wasm-runtime.js.map +1 -0
  133. package/dist/worker-client.d.ts +123 -0
  134. package/dist/worker-client.d.ts.map +1 -0
  135. package/dist/worker-client.js +1686 -0
  136. package/dist/worker-client.js.map +1 -0
  137. package/dist/worker-entry.d.ts +2 -0
  138. package/dist/worker-entry.d.ts.map +1 -0
  139. package/dist/worker-entry.js +479 -0
  140. package/dist/worker-entry.js.map +1 -0
  141. package/dist/worker-protocol.d.ts +64 -0
  142. package/dist/worker-protocol.d.ts.map +1 -0
  143. package/dist/worker-protocol.js +16 -0
  144. package/dist/worker-protocol.js.map +1 -0
  145. package/dist/worker-realtime.d.ts +39 -0
  146. package/dist/worker-realtime.d.ts.map +1 -0
  147. package/dist/worker-realtime.js +677 -0
  148. package/dist/worker-realtime.js.map +1 -0
  149. package/package.json +124 -17
  150. package/src/auth-leases.ts +251 -0
  151. package/src/blob-limits.ts +98 -0
  152. package/src/bridge-client.ts +512 -0
  153. package/src/client-config.ts +29 -0
  154. package/src/client.ts +256 -1414
  155. package/src/command-history.ts +623 -0
  156. package/src/console-diagnostics.ts +617 -0
  157. package/src/crdt-yjs/index.ts +3 -0
  158. package/src/crdt-yjs/webview-host-facade.ts +477 -0
  159. package/src/crdt-yjs/yjs-document-field-adapter.ts +733 -0
  160. package/src/crdt-yjs/yjs-prosemirror-bridge.ts +272 -0
  161. package/src/database.ts +1342 -0
  162. package/src/diagnostics.ts +116 -0
  163. package/src/errors.ts +269 -0
  164. package/src/generated-bridge.ts +741 -0
  165. package/src/index.ts +21 -25
  166. package/src/mutations.ts +12 -821
  167. package/src/network.ts +32 -0
  168. package/src/react/index.ts +1036 -0
  169. package/src/react-native/index.ts +152 -0
  170. package/src/runtime-contract.ts +48 -0
  171. package/src/rust-client.ts +1488 -0
  172. package/src/sentry.ts +215 -0
  173. package/src/sql-safety.ts +61 -0
  174. package/src/tauri/index.ts +211 -0
  175. package/src/types.ts +1356 -0
  176. package/src/wasm-bindings/generated-wasm-bindings.d.ts +70 -0
  177. package/src/wasm-bindings/runtime-contract.ts +158 -0
  178. package/src/wasm-runtime.ts +76 -0
  179. package/src/worker-client.ts +2215 -0
  180. package/src/worker-entry.ts +584 -0
  181. package/src/worker-protocol.ts +109 -0
  182. package/src/worker-realtime.ts +843 -0
  183. package/dist/blobs/index.d.ts +0 -7
  184. package/dist/blobs/index.d.ts.map +0 -1
  185. package/dist/blobs/index.js +0 -7
  186. package/dist/blobs/index.js.map +0 -1
  187. package/dist/blobs/manager.d.ts +0 -345
  188. package/dist/blobs/manager.d.ts.map +0 -1
  189. package/dist/blobs/manager.js +0 -749
  190. package/dist/blobs/manager.js.map +0 -1
  191. package/dist/blobs/migrate.d.ts +0 -14
  192. package/dist/blobs/migrate.d.ts.map +0 -1
  193. package/dist/blobs/migrate.js +0 -59
  194. package/dist/blobs/migrate.js.map +0 -1
  195. package/dist/blobs/types.d.ts +0 -62
  196. package/dist/blobs/types.d.ts.map +0 -1
  197. package/dist/blobs/types.js +0 -5
  198. package/dist/blobs/types.js.map +0 -1
  199. package/dist/conflicts.d.ts +0 -31
  200. package/dist/conflicts.d.ts.map +0 -1
  201. package/dist/conflicts.js +0 -112
  202. package/dist/conflicts.js.map +0 -1
  203. package/dist/create-client.d.ts +0 -118
  204. package/dist/create-client.d.ts.map +0 -1
  205. package/dist/create-client.js +0 -178
  206. package/dist/create-client.js.map +0 -1
  207. package/dist/engine/SyncEngine.d.ts +0 -272
  208. package/dist/engine/SyncEngine.d.ts.map +0 -1
  209. package/dist/engine/SyncEngine.js +0 -1860
  210. package/dist/engine/SyncEngine.js.map +0 -1
  211. package/dist/engine/index.d.ts +0 -6
  212. package/dist/engine/index.d.ts.map +0 -1
  213. package/dist/engine/index.js +0 -6
  214. package/dist/engine/index.js.map +0 -1
  215. package/dist/engine/types.d.ts +0 -345
  216. package/dist/engine/types.d.ts.map +0 -1
  217. package/dist/engine/types.js +0 -7
  218. package/dist/engine/types.js.map +0 -1
  219. package/dist/handlers/collection.d.ts +0 -6
  220. package/dist/handlers/collection.d.ts.map +0 -1
  221. package/dist/handlers/collection.js +0 -21
  222. package/dist/handlers/collection.js.map +0 -1
  223. package/dist/handlers/create-handler.d.ts +0 -120
  224. package/dist/handlers/create-handler.d.ts.map +0 -1
  225. package/dist/handlers/create-handler.js +0 -145
  226. package/dist/handlers/create-handler.js.map +0 -1
  227. package/dist/handlers/types.d.ts +0 -83
  228. package/dist/handlers/types.d.ts.map +0 -1
  229. package/dist/handlers/types.js +0 -5
  230. package/dist/handlers/types.js.map +0 -1
  231. package/dist/migrate.d.ts +0 -19
  232. package/dist/migrate.d.ts.map +0 -1
  233. package/dist/migrate.js +0 -238
  234. package/dist/migrate.js.map +0 -1
  235. package/dist/outbox.d.ts +0 -112
  236. package/dist/outbox.d.ts.map +0 -1
  237. package/dist/outbox.js +0 -294
  238. package/dist/outbox.js.map +0 -1
  239. package/dist/plugins/incrementing-version.d.ts +0 -34
  240. package/dist/plugins/incrementing-version.d.ts.map +0 -1
  241. package/dist/plugins/incrementing-version.js +0 -83
  242. package/dist/plugins/incrementing-version.js.map +0 -1
  243. package/dist/plugins/index.d.ts +0 -3
  244. package/dist/plugins/index.d.ts.map +0 -1
  245. package/dist/plugins/index.js +0 -3
  246. package/dist/plugins/index.js.map +0 -1
  247. package/dist/plugins/types.d.ts +0 -49
  248. package/dist/plugins/types.d.ts.map +0 -1
  249. package/dist/plugins/types.js +0 -15
  250. package/dist/plugins/types.js.map +0 -1
  251. package/dist/proxy/connection.d.ts +0 -33
  252. package/dist/proxy/connection.d.ts.map +0 -1
  253. package/dist/proxy/connection.js +0 -153
  254. package/dist/proxy/connection.js.map +0 -1
  255. package/dist/proxy/dialect.d.ts +0 -46
  256. package/dist/proxy/dialect.d.ts.map +0 -1
  257. package/dist/proxy/dialect.js +0 -58
  258. package/dist/proxy/dialect.js.map +0 -1
  259. package/dist/proxy/driver.d.ts +0 -42
  260. package/dist/proxy/driver.d.ts.map +0 -1
  261. package/dist/proxy/driver.js +0 -78
  262. package/dist/proxy/driver.js.map +0 -1
  263. package/dist/proxy/index.d.ts +0 -10
  264. package/dist/proxy/index.d.ts.map +0 -1
  265. package/dist/proxy/index.js +0 -10
  266. package/dist/proxy/index.js.map +0 -1
  267. package/dist/proxy/mutations.d.ts +0 -9
  268. package/dist/proxy/mutations.d.ts.map +0 -1
  269. package/dist/proxy/mutations.js +0 -11
  270. package/dist/proxy/mutations.js.map +0 -1
  271. package/dist/pull-engine.d.ts +0 -48
  272. package/dist/pull-engine.d.ts.map +0 -1
  273. package/dist/pull-engine.js +0 -648
  274. package/dist/pull-engine.js.map +0 -1
  275. package/dist/push-engine.d.ts +0 -18
  276. package/dist/push-engine.d.ts.map +0 -1
  277. package/dist/push-engine.js +0 -167
  278. package/dist/push-engine.js.map +0 -1
  279. package/dist/query/FingerprintCollector.d.ts +0 -18
  280. package/dist/query/FingerprintCollector.d.ts.map +0 -1
  281. package/dist/query/FingerprintCollector.js +0 -28
  282. package/dist/query/FingerprintCollector.js.map +0 -1
  283. package/dist/query/QueryContext.d.ts +0 -33
  284. package/dist/query/QueryContext.d.ts.map +0 -1
  285. package/dist/query/QueryContext.js +0 -16
  286. package/dist/query/QueryContext.js.map +0 -1
  287. package/dist/query/fingerprint.d.ts +0 -61
  288. package/dist/query/fingerprint.d.ts.map +0 -1
  289. package/dist/query/fingerprint.js +0 -91
  290. package/dist/query/fingerprint.js.map +0 -1
  291. package/dist/query/index.d.ts +0 -7
  292. package/dist/query/index.d.ts.map +0 -1
  293. package/dist/query/index.js +0 -7
  294. package/dist/query/index.js.map +0 -1
  295. package/dist/query/tracked-select.d.ts +0 -18
  296. package/dist/query/tracked-select.d.ts.map +0 -1
  297. package/dist/query/tracked-select.js +0 -90
  298. package/dist/query/tracked-select.js.map +0 -1
  299. package/dist/schema.d.ts +0 -83
  300. package/dist/schema.d.ts.map +0 -1
  301. package/dist/schema.js +0 -7
  302. package/dist/schema.js.map +0 -1
  303. package/dist/subscription-state.d.ts +0 -46
  304. package/dist/subscription-state.d.ts.map +0 -1
  305. package/dist/subscription-state.js +0 -185
  306. package/dist/subscription-state.js.map +0 -1
  307. package/dist/sync-loop.d.ts +0 -34
  308. package/dist/sync-loop.d.ts.map +0 -1
  309. package/dist/sync-loop.js +0 -253
  310. package/dist/sync-loop.js.map +0 -1
  311. package/dist/sync.d.ts +0 -32
  312. package/dist/sync.d.ts.map +0 -1
  313. package/dist/sync.js +0 -55
  314. package/dist/sync.js.map +0 -1
  315. package/dist/utils/id.d.ts +0 -17
  316. package/dist/utils/id.d.ts.map +0 -1
  317. package/dist/utils/id.js +0 -46
  318. package/dist/utils/id.js.map +0 -1
  319. package/src/blobs/index.ts +0 -7
  320. package/src/blobs/manager.ts +0 -1027
  321. package/src/blobs/migrate.ts +0 -67
  322. package/src/blobs/types.ts +0 -84
  323. package/src/client.test.ts +0 -417
  324. package/src/conflicts.ts +0 -171
  325. package/src/create-client.test.ts +0 -83
  326. package/src/create-client.ts +0 -330
  327. package/src/engine/SyncEngine.test.ts +0 -254
  328. package/src/engine/SyncEngine.ts +0 -2375
  329. package/src/engine/index.ts +0 -6
  330. package/src/engine/types.ts +0 -419
  331. package/src/handlers/collection.ts +0 -36
  332. package/src/handlers/create-handler.ts +0 -330
  333. package/src/handlers/types.ts +0 -108
  334. package/src/migrate.ts +0 -318
  335. package/src/outbox.ts +0 -383
  336. package/src/plugins/incrementing-version.ts +0 -133
  337. package/src/plugins/index.ts +0 -2
  338. package/src/plugins/types.ts +0 -63
  339. package/src/proxy/connection.ts +0 -191
  340. package/src/proxy/dialect.ts +0 -76
  341. package/src/proxy/driver.ts +0 -126
  342. package/src/proxy/index.ts +0 -10
  343. package/src/proxy/mutations.ts +0 -18
  344. package/src/pull-engine.test.ts +0 -151
  345. package/src/pull-engine.ts +0 -855
  346. package/src/push-engine.ts +0 -216
  347. package/src/query/FingerprintCollector.ts +0 -29
  348. package/src/query/QueryContext.ts +0 -54
  349. package/src/query/fingerprint.ts +0 -109
  350. package/src/query/index.ts +0 -10
  351. package/src/query/tracked-select.ts +0 -139
  352. package/src/schema.ts +0 -94
  353. package/src/subscription-state.ts +0 -259
  354. package/src/sync-loop.ts +0 -379
  355. package/src/sync.ts +0 -170
  356. package/src/utils/id.ts +0 -55
@@ -1,2375 +0,0 @@
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 SyncPullSubscriptionResponse,
16
- type SyncSubscriptionRequest,
17
- SyncTransportError,
18
- startSyncSpan,
19
- } from '@syncular/core';
20
- import { type Kysely, sql, type Transaction } from 'kysely';
21
- import { getClientHandler } from '../handlers/collection';
22
- import { ensureClientSyncSchema } from '../migrate';
23
- import { syncPushOnce } from '../push-engine';
24
- import type {
25
- ConflictResultStatus,
26
- OutboxCommitStatus,
27
- SyncClientDb,
28
- } from '../schema';
29
- import {
30
- DEFAULT_SYNC_STATE_ID,
31
- getSubscriptionState as readSubscriptionState,
32
- listSubscriptionStates as readSubscriptionStates,
33
- type SubscriptionState,
34
- } from '../subscription-state';
35
- import { syncOnce } from '../sync-loop';
36
- import type {
37
- ConflictInfo,
38
- OutboxStats,
39
- PresenceEntry,
40
- RealtimeTransportLike,
41
- SubscriptionProgress,
42
- SyncAwaitBootstrapOptions,
43
- SyncAwaitPhaseOptions,
44
- SyncConnectionState,
45
- SyncDiagnostics,
46
- SyncEngineConfig,
47
- SyncEngineState,
48
- SyncError,
49
- SyncEventListener,
50
- SyncEventPayloads,
51
- SyncEventType,
52
- SyncInspectorEvent,
53
- SyncInspectorOptions,
54
- SyncInspectorSnapshot,
55
- SyncProgress,
56
- SyncRepairOptions,
57
- SyncResetOptions,
58
- SyncResetResult,
59
- SyncResult,
60
- SyncTransportMode,
61
- TransportHealth,
62
- } from './types';
63
-
64
- const DEFAULT_POLL_INTERVAL_MS = 10_000;
65
- const DEFAULT_MAX_RETRIES = 5;
66
- const INITIAL_RETRY_DELAY_MS = 1000;
67
- const MAX_RETRY_DELAY_MS = 60000;
68
- const EXPONENTIAL_FACTOR = 2;
69
- const REALTIME_RECONNECT_CATCHUP_DELAY_MS = 500;
70
- const DEFAULT_AWAIT_TIMEOUT_MS = 60_000;
71
- const DEFAULT_INSPECTOR_EVENT_LIMIT = 100;
72
- const MAX_INSPECTOR_EVENT_LIMIT = 500;
73
-
74
- function calculateRetryDelay(attemptIndex: number): number {
75
- return Math.min(
76
- INITIAL_RETRY_DELAY_MS * EXPONENTIAL_FACTOR ** attemptIndex,
77
- MAX_RETRY_DELAY_MS
78
- );
79
- }
80
-
81
- function isRealtimeTransport(
82
- transport: unknown
83
- ): transport is RealtimeTransportLike {
84
- return (
85
- typeof transport === 'object' &&
86
- transport !== null &&
87
- typeof (transport as RealtimeTransportLike).connect === 'function'
88
- );
89
- }
90
-
91
- function createSyncError(args: {
92
- code: SyncError['code'];
93
- message: string;
94
- cause?: Error;
95
- retryable?: boolean;
96
- httpStatus?: number;
97
- subscriptionId?: string;
98
- stateId?: string;
99
- }): SyncError {
100
- return {
101
- code: args.code,
102
- message: args.message,
103
- cause: args.cause,
104
- timestamp: Date.now(),
105
- retryable: args.retryable ?? false,
106
- httpStatus: args.httpStatus,
107
- subscriptionId: args.subscriptionId,
108
- stateId: args.stateId,
109
- };
110
- }
111
-
112
- function classifySyncFailure(error: unknown): {
113
- code: SyncError['code'];
114
- message: string;
115
- cause: Error;
116
- retryable: boolean;
117
- httpStatus?: number;
118
- } {
119
- const cause = error instanceof Error ? error : new Error(String(error));
120
- const message = cause.message || 'Sync failed';
121
- const normalized = message.toLowerCase();
122
-
123
- if (cause instanceof SyncTransportError) {
124
- if (cause.status === 401 || cause.status === 403) {
125
- return {
126
- code: 'AUTH_FAILED',
127
- message,
128
- cause,
129
- retryable: false,
130
- httpStatus: cause.status,
131
- };
132
- }
133
-
134
- if (
135
- cause.status === 404 &&
136
- normalized.includes('snapshot') &&
137
- normalized.includes('chunk')
138
- ) {
139
- return {
140
- code: 'SNAPSHOT_CHUNK_NOT_FOUND',
141
- message,
142
- cause,
143
- retryable: false,
144
- httpStatus: cause.status,
145
- };
146
- }
147
-
148
- if (
149
- cause.status !== undefined &&
150
- (cause.status >= 500 || cause.status === 408 || cause.status === 429)
151
- ) {
152
- return {
153
- code: 'NETWORK_ERROR',
154
- message,
155
- cause,
156
- retryable: true,
157
- httpStatus: cause.status,
158
- };
159
- }
160
-
161
- return {
162
- code: 'SYNC_ERROR',
163
- message,
164
- cause,
165
- retryable: false,
166
- httpStatus: cause.status,
167
- };
168
- }
169
-
170
- if (
171
- normalized.includes('network') ||
172
- normalized.includes('fetch') ||
173
- normalized.includes('timeout') ||
174
- normalized.includes('offline')
175
- ) {
176
- return {
177
- code: 'NETWORK_ERROR',
178
- message,
179
- cause,
180
- retryable: true,
181
- };
182
- }
183
-
184
- if (normalized.includes('conflict')) {
185
- return {
186
- code: 'CONFLICT',
187
- message,
188
- cause,
189
- retryable: false,
190
- };
191
- }
192
-
193
- return {
194
- code: 'SYNC_ERROR',
195
- message,
196
- cause,
197
- retryable: false,
198
- };
199
- }
200
-
201
- function resolveSyncTriggerLabel(
202
- trigger?: 'ws' | 'local' | 'poll'
203
- ): 'ws' | 'local' | 'poll' | 'auto' {
204
- return trigger ?? 'auto';
205
- }
206
-
207
- function serializeInspectorValue(value: unknown): unknown {
208
- const encoded = JSON.stringify(value, (_key, nextValue) => {
209
- if (nextValue instanceof Error) {
210
- return {
211
- name: nextValue.name,
212
- message: nextValue.message,
213
- stack: nextValue.stack,
214
- };
215
- }
216
- if (typeof nextValue === 'bigint') {
217
- return nextValue.toString();
218
- }
219
- return nextValue;
220
- });
221
-
222
- if (!encoded) return null;
223
- return JSON.parse(encoded) as unknown;
224
- }
225
-
226
- function serializeInspectorRecord(value: unknown): Record<string, unknown> {
227
- const serialized = serializeInspectorValue(value);
228
- if (isRecord(serialized)) {
229
- return serialized;
230
- }
231
- return { value: serialized };
232
- }
233
-
234
- /**
235
- * Sync engine that orchestrates push/pull cycles with proper lifecycle management.
236
- *
237
- * Key features:
238
- * - Event-driven architecture (no global mutable state)
239
- * - Proper error handling (no silent catches)
240
- * - Lifecycle managed by framework bindings
241
- * - Type-safe event subscriptions
242
- * - Presence tracking support
243
- */
244
- type AnyEventListener = (payload: SyncEventPayloads[SyncEventType]) => void;
245
-
246
- export class SyncEngine<DB extends SyncClientDb = SyncClientDb> {
247
- private config: SyncEngineConfig<DB>;
248
- private state: SyncEngineState;
249
- private listeners: Map<SyncEventType, Set<AnyEventListener>>;
250
- private pollerId: ReturnType<typeof setInterval> | null = null;
251
- private fallbackPollerId: ReturnType<typeof setInterval> | null = null;
252
- private realtimeDisconnect: (() => void) | null = null;
253
- private realtimePresenceUnsub: (() => void) | null = null;
254
- private isDestroyed = false;
255
- private migrated = false;
256
- private syncPromise: Promise<SyncResult> | null = null;
257
- private syncRequestedWhileRunning = false;
258
- private retryTimeoutId: ReturnType<typeof setTimeout> | null = null;
259
- private realtimeCatchupTimeoutId: ReturnType<typeof setTimeout> | null = null;
260
- private hasRealtimeConnectedOnce = false;
261
- private transportHealth: TransportHealth = {
262
- mode: 'disconnected',
263
- connected: false,
264
- lastSuccessfulPollAt: null,
265
- lastRealtimeMessageAt: null,
266
- fallbackReason: null,
267
- };
268
- private activeBootstrapSubscriptions = new Set<string>();
269
- private bootstrapStartedAt = new Map<string, number>();
270
- private inspectorEvents: SyncInspectorEvent[] = [];
271
- private nextInspectorEventId = 1;
272
-
273
- /**
274
- * In-memory map tracking local mutation timestamps by rowId.
275
- * Used for efficient fingerprint-based rerender optimization.
276
- * Key format: `${table}:${rowId}`, Value: timestamp (Date.now())
277
- */
278
- private mutationTimestamps = new Map<string, number>();
279
-
280
- /**
281
- * In-memory map tracking table-level mutation timestamps.
282
- * Used for coarse invalidation during large bootstrap snapshots to avoid
283
- * storing timestamps for every row.
284
- */
285
- private tableMutationTimestamps = new Map<string, number>();
286
-
287
- /**
288
- * In-memory presence state by scope key.
289
- * Updated via realtime presence events.
290
- */
291
- private presenceByScopeKey = new Map<string, PresenceEntry[]>();
292
-
293
- constructor(config: SyncEngineConfig<DB>) {
294
- this.config = config;
295
- this.listeners = new Map();
296
- this.state = this.createInitialState();
297
- this.transportHealth = {
298
- mode: this.state.transportMode === 'polling' ? 'polling' : 'disconnected',
299
- connected: false,
300
- lastSuccessfulPollAt: null,
301
- lastRealtimeMessageAt: null,
302
- fallbackReason: null,
303
- };
304
- }
305
-
306
- /**
307
- * Get mutation timestamp for a row (used by query hooks for fingerprinting).
308
- * Returns 0 if row has no recorded mutation timestamp.
309
- */
310
- getMutationTimestamp(table: string, rowId: string): number {
311
- const rowTs = this.mutationTimestamps.get(`${table}:${rowId}`) ?? 0;
312
- const tableTs = this.tableMutationTimestamps.get(table) ?? 0;
313
- return Math.max(rowTs, tableTs);
314
- }
315
-
316
- /**
317
- * Get presence entries for a scope key.
318
- * Returns empty array if no presence data for the scope.
319
- */
320
- getPresence<TMetadata = Record<string, unknown>>(
321
- scopeKey: string
322
- ): PresenceEntry<TMetadata>[] {
323
- return (this.presenceByScopeKey.get(scopeKey) ??
324
- []) as PresenceEntry<TMetadata>[];
325
- }
326
-
327
- /**
328
- * Update presence for a scope key (called by realtime transport).
329
- * Emits presence:change event for listeners.
330
- */
331
- updatePresence(scopeKey: string, presence: PresenceEntry[]): void {
332
- this.presenceByScopeKey.set(scopeKey, presence);
333
- this.emit('presence:change', { scopeKey, presence });
334
- }
335
-
336
- /**
337
- * Join presence for a scope key.
338
- * Sends via transport (if available) and updates local state optimistically.
339
- */
340
- joinPresence(scopeKey: string, metadata?: Record<string, unknown>): void {
341
- if (isRealtimeTransport(this.config.transport)) {
342
- const transport = this.config.transport as RealtimeTransportLike;
343
- transport.sendPresenceJoin?.(scopeKey, metadata);
344
- }
345
- // Optimistic local update
346
- this.handlePresenceEvent({
347
- action: 'join',
348
- scopeKey,
349
- clientId: this.config.clientId!,
350
- actorId: this.config.actorId!,
351
- metadata,
352
- });
353
- }
354
-
355
- /**
356
- * Leave presence for a scope key.
357
- */
358
- leavePresence(scopeKey: string): void {
359
- if (isRealtimeTransport(this.config.transport)) {
360
- const transport = this.config.transport as RealtimeTransportLike;
361
- transport.sendPresenceLeave?.(scopeKey);
362
- }
363
- this.handlePresenceEvent({
364
- action: 'leave',
365
- scopeKey,
366
- clientId: this.config.clientId!,
367
- actorId: this.config.actorId!,
368
- });
369
- }
370
-
371
- /**
372
- * Update presence metadata for a scope key.
373
- */
374
- updatePresenceMetadata(
375
- scopeKey: string,
376
- metadata: Record<string, unknown>
377
- ): void {
378
- if (isRealtimeTransport(this.config.transport)) {
379
- const transport = this.config.transport as RealtimeTransportLike;
380
- transport.sendPresenceUpdate?.(scopeKey, metadata);
381
- }
382
- this.handlePresenceEvent({
383
- action: 'update',
384
- scopeKey,
385
- clientId: this.config.clientId!,
386
- actorId: this.config.actorId!,
387
- metadata,
388
- });
389
- }
390
-
391
- /**
392
- * Handle a single presence event (join/leave/update).
393
- * Updates the in-memory presence state and emits change event.
394
- */
395
- handlePresenceEvent(event: {
396
- action: 'join' | 'leave' | 'update';
397
- scopeKey: string;
398
- clientId: string;
399
- actorId: string;
400
- metadata?: Record<string, unknown>;
401
- }): void {
402
- const current = this.presenceByScopeKey.get(event.scopeKey) ?? [];
403
-
404
- let updated: PresenceEntry[];
405
- switch (event.action) {
406
- case 'join':
407
- // Add new entry (remove existing if present to update)
408
- updated = [
409
- ...current.filter((e) => e.clientId !== event.clientId),
410
- {
411
- clientId: event.clientId,
412
- actorId: event.actorId,
413
- joinedAt: Date.now(),
414
- metadata: event.metadata,
415
- },
416
- ];
417
- break;
418
- case 'leave':
419
- updated = current.filter((e) => e.clientId !== event.clientId);
420
- break;
421
- case 'update':
422
- updated = current.map((e) =>
423
- e.clientId === event.clientId ? { ...e, metadata: event.metadata } : e
424
- );
425
- break;
426
- }
427
-
428
- this.presenceByScopeKey.set(event.scopeKey, updated);
429
- this.emit('presence:change', {
430
- scopeKey: event.scopeKey,
431
- presence: updated,
432
- });
433
- }
434
-
435
- private createInitialState(): SyncEngineState {
436
- const enabled = this.isEnabled();
437
- return {
438
- enabled,
439
- isSyncing: false,
440
- connectionState: enabled ? 'disconnected' : 'disconnected',
441
- transportMode: this.detectTransportMode(),
442
- lastSyncAt: null,
443
- error: null,
444
- pendingCount: 0,
445
- retryCount: 0,
446
- isRetrying: false,
447
- };
448
- }
449
-
450
- private isEnabled(): boolean {
451
- const { actorId, clientId } = this.config;
452
- return (
453
- typeof actorId === 'string' &&
454
- actorId.length > 0 &&
455
- typeof clientId === 'string' &&
456
- clientId.length > 0
457
- );
458
- }
459
-
460
- private detectTransportMode(): SyncTransportMode {
461
- if (
462
- this.config.realtimeEnabled !== false &&
463
- isRealtimeTransport(this.config.transport)
464
- ) {
465
- return 'realtime';
466
- }
467
- return 'polling';
468
- }
469
-
470
- /**
471
- * Get current engine state.
472
- * Returns the same object reference to avoid useSyncExternalStore infinite loops.
473
- */
474
- getState(): Readonly<SyncEngineState> {
475
- return this.state;
476
- }
477
-
478
- /**
479
- * Get transport health details (realtime/polling/fallback).
480
- */
481
- getTransportHealth(): Readonly<TransportHealth> {
482
- return this.transportHealth;
483
- }
484
-
485
- /**
486
- * Get subscription state metadata for the current profile.
487
- */
488
- async listSubscriptionStates(args?: {
489
- stateId?: string;
490
- table?: string;
491
- status?: 'active' | 'revoked';
492
- }): Promise<SubscriptionState[]> {
493
- return readSubscriptionStates(this.config.db, {
494
- stateId: args?.stateId ?? this.getStateId(),
495
- table: args?.table,
496
- status: args?.status,
497
- });
498
- }
499
-
500
- /**
501
- * Get a single subscription state by id.
502
- */
503
- async getSubscriptionState(
504
- subscriptionId: string,
505
- options?: { stateId?: string }
506
- ): Promise<SubscriptionState | null> {
507
- return readSubscriptionState(this.config.db, {
508
- stateId: options?.stateId ?? this.getStateId(),
509
- subscriptionId,
510
- });
511
- }
512
-
513
- /**
514
- * Get normalized progress for all active subscriptions in this state profile.
515
- */
516
- async getProgress(): Promise<SyncProgress> {
517
- const subscriptions = await this.listSubscriptionStates();
518
- const progress = subscriptions.map((sub) =>
519
- this.mapSubscriptionToProgress(sub)
520
- );
521
-
522
- const channelPhase = this.resolveChannelPhase(progress);
523
- const hasSubscriptions = progress.length > 0;
524
- const basePercent = hasSubscriptions
525
- ? Math.round(
526
- progress.reduce((sum, item) => sum + item.progressPercent, 0) /
527
- progress.length
528
- )
529
- : this.state.lastSyncAt !== null
530
- ? 100
531
- : 0;
532
-
533
- const progressPercent =
534
- channelPhase === 'live'
535
- ? 100
536
- : Math.max(0, Math.min(100, Math.trunc(basePercent)));
537
-
538
- return {
539
- channelPhase,
540
- progressPercent,
541
- subscriptions: progress,
542
- };
543
- }
544
-
545
- /**
546
- * Wait until the channel reaches a target phase.
547
- */
548
- async awaitPhase(
549
- phase: SyncProgress['channelPhase'],
550
- options: SyncAwaitPhaseOptions = {}
551
- ): Promise<SyncProgress> {
552
- const timeoutMs = Math.max(
553
- 0,
554
- options.timeoutMs ?? DEFAULT_AWAIT_TIMEOUT_MS
555
- );
556
- const deadline = Date.now() + timeoutMs;
557
-
558
- while (true) {
559
- const progress = await this.getProgress();
560
-
561
- if (progress.channelPhase === phase) {
562
- return progress;
563
- }
564
-
565
- if (progress.channelPhase === 'error') {
566
- const message = this.state.error?.message ?? 'Sync entered error state';
567
- throw new Error(
568
- `[SyncEngine.awaitPhase] Failed while waiting for "${phase}": ${message}`
569
- );
570
- }
571
-
572
- const remainingMs = deadline - Date.now();
573
- if (remainingMs <= 0) {
574
- throw new Error(
575
- `[SyncEngine.awaitPhase] Timed out after ${timeoutMs}ms waiting for phase "${phase}"`
576
- );
577
- }
578
-
579
- await this.waitForProgressSignal(remainingMs);
580
- }
581
- }
582
-
583
- /**
584
- * Wait until bootstrap finishes for a state or a specific subscription.
585
- */
586
- async awaitBootstrapComplete(
587
- options: SyncAwaitBootstrapOptions = {}
588
- ): Promise<SyncProgress> {
589
- const timeoutMs = Math.max(
590
- 0,
591
- options.timeoutMs ?? DEFAULT_AWAIT_TIMEOUT_MS
592
- );
593
- const stateId = options.stateId ?? this.getStateId();
594
- const deadline = Date.now() + timeoutMs;
595
-
596
- while (true) {
597
- const states = await this.listSubscriptionStates({ stateId });
598
- const relevantStates =
599
- options.subscriptionId === undefined
600
- ? states
601
- : states.filter(
602
- (state) => state.subscriptionId === options.subscriptionId
603
- );
604
-
605
- const hasPendingBootstrap = relevantStates.some(
606
- (state) => state.status === 'active' && state.bootstrapState !== null
607
- );
608
-
609
- if (!hasPendingBootstrap) {
610
- return this.getProgress();
611
- }
612
-
613
- if (this.state.error) {
614
- throw new Error(
615
- `[SyncEngine.awaitBootstrapComplete] Failed while waiting for bootstrap completion: ${this.state.error.message}`
616
- );
617
- }
618
-
619
- const remainingMs = deadline - Date.now();
620
- if (remainingMs <= 0) {
621
- const target =
622
- options.subscriptionId === undefined
623
- ? `state "${stateId}"`
624
- : `subscription "${options.subscriptionId}" in state "${stateId}"`;
625
-
626
- throw new Error(
627
- `[SyncEngine.awaitBootstrapComplete] Timed out after ${timeoutMs}ms waiting for ${target}`
628
- );
629
- }
630
-
631
- await this.waitForProgressSignal(remainingMs);
632
- }
633
- }
634
-
635
- /**
636
- * Get a diagnostics snapshot suitable for debug UIs and bug reports.
637
- */
638
- async getDiagnostics(): Promise<SyncDiagnostics> {
639
- const [subscriptions, progress, outbox, conflicts] = await Promise.all([
640
- this.listSubscriptionStates(),
641
- this.getProgress(),
642
- this.refreshOutboxStats({ emit: false }),
643
- this.getConflicts(),
644
- ]);
645
-
646
- return {
647
- timestamp: Date.now(),
648
- state: this.state,
649
- transport: this.transportHealth,
650
- progress,
651
- outbox,
652
- conflictCount: conflicts.length,
653
- subscriptions,
654
- };
655
- }
656
-
657
- /**
658
- * Get a serializable inspector snapshot for app debug UIs and support tooling.
659
- */
660
- async getInspectorSnapshot(
661
- options: SyncInspectorOptions = {}
662
- ): Promise<SyncInspectorSnapshot> {
663
- const diagnostics = await this.getDiagnostics();
664
- const requestedLimit = options.eventLimit ?? DEFAULT_INSPECTOR_EVENT_LIMIT;
665
- const eventLimit = Math.max(
666
- 0,
667
- Math.min(MAX_INSPECTOR_EVENT_LIMIT, requestedLimit)
668
- );
669
- const recentEvents =
670
- eventLimit === 0 ? [] : this.inspectorEvents.slice(-eventLimit);
671
-
672
- return {
673
- version: 1,
674
- generatedAt: Date.now(),
675
- diagnostics: serializeInspectorRecord(diagnostics),
676
- recentEvents,
677
- };
678
- }
679
-
680
- /**
681
- * Get database instance
682
- */
683
- getDb(): Kysely<DB> {
684
- return this.config.db;
685
- }
686
-
687
- /**
688
- * Get current actor id (sync scoping).
689
- */
690
- getActorId(): string | null | undefined {
691
- return this.config.actorId;
692
- }
693
-
694
- /**
695
- * Get current client id (device/app install id).
696
- */
697
- getClientId(): string | null | undefined {
698
- return this.config.clientId;
699
- }
700
-
701
- private getStateId(): string {
702
- return this.config.stateId ?? DEFAULT_SYNC_STATE_ID;
703
- }
704
-
705
- private makeBootstrapKey(stateId: string, subscriptionId: string): string {
706
- return `${stateId}:${subscriptionId}`;
707
- }
708
-
709
- private updateTransportHealth(partial: Partial<TransportHealth>): void {
710
- this.transportHealth = {
711
- ...this.transportHealth,
712
- ...partial,
713
- };
714
- this.emit('state:change', {});
715
- }
716
-
717
- private waitForProgressSignal(timeoutMs: number): Promise<void> {
718
- return new Promise((resolve) => {
719
- const cleanups: Array<() => void> = [];
720
- let settled = false;
721
-
722
- const finish = () => {
723
- if (settled) return;
724
- settled = true;
725
- clearTimeout(timeoutId);
726
- for (const cleanup of cleanups) cleanup();
727
- resolve();
728
- };
729
-
730
- const listen = (event: SyncEventType) => {
731
- cleanups.push(this.on(event, finish));
732
- };
733
-
734
- listen('sync:start');
735
- listen('sync:complete');
736
- listen('sync:error');
737
- listen('sync:live');
738
- listen('bootstrap:start');
739
- listen('bootstrap:progress');
740
- listen('bootstrap:complete');
741
-
742
- const timeoutId = setTimeout(finish, Math.max(1, timeoutMs));
743
- });
744
- }
745
-
746
- private mapSubscriptionToProgress(
747
- subscription: SubscriptionState
748
- ): SubscriptionProgress {
749
- if (subscription.status === 'revoked') {
750
- return {
751
- stateId: subscription.stateId,
752
- id: subscription.subscriptionId,
753
- table: subscription.table,
754
- phase: 'error',
755
- progressPercent: 0,
756
- startedAt: subscription.createdAt,
757
- completedAt: subscription.updatedAt,
758
- lastErrorCode: 'SUBSCRIPTION_REVOKED',
759
- lastErrorMessage: 'Subscription is revoked',
760
- };
761
- }
762
-
763
- if (subscription.bootstrapState) {
764
- const tableCount = Math.max(0, subscription.bootstrapState.tables.length);
765
- const tableIndex = Math.max(0, subscription.bootstrapState.tableIndex);
766
- const tablesProcessed = Math.min(tableCount, tableIndex);
767
- const progressPercent =
768
- tableCount === 0
769
- ? 0
770
- : Math.max(
771
- 0,
772
- Math.min(100, Math.round((tablesProcessed / tableCount) * 100))
773
- );
774
-
775
- return {
776
- stateId: subscription.stateId,
777
- id: subscription.subscriptionId,
778
- table: subscription.table,
779
- phase: 'bootstrapping',
780
- progressPercent,
781
- tablesProcessed,
782
- tablesTotal: tableCount,
783
- startedAt: this.bootstrapStartedAt.get(
784
- this.makeBootstrapKey(
785
- subscription.stateId,
786
- subscription.subscriptionId
787
- )
788
- ),
789
- };
790
- }
791
-
792
- if (this.state.error) {
793
- return {
794
- stateId: subscription.stateId,
795
- id: subscription.subscriptionId,
796
- table: subscription.table,
797
- phase: 'error',
798
- progressPercent: subscription.cursor >= 0 ? 100 : 0,
799
- startedAt: subscription.createdAt,
800
- lastErrorCode: this.state.error.code,
801
- lastErrorMessage: this.state.error.message,
802
- };
803
- }
804
-
805
- if (this.state.isSyncing) {
806
- return {
807
- stateId: subscription.stateId,
808
- id: subscription.subscriptionId,
809
- table: subscription.table,
810
- phase: 'catching_up',
811
- progressPercent: subscription.cursor >= 0 ? 90 : 0,
812
- startedAt: subscription.createdAt,
813
- };
814
- }
815
-
816
- if (subscription.cursor >= 0 || this.state.lastSyncAt !== null) {
817
- return {
818
- stateId: subscription.stateId,
819
- id: subscription.subscriptionId,
820
- table: subscription.table,
821
- phase: 'live',
822
- progressPercent: 100,
823
- startedAt: subscription.createdAt,
824
- completedAt: subscription.updatedAt,
825
- };
826
- }
827
-
828
- return {
829
- stateId: subscription.stateId,
830
- id: subscription.subscriptionId,
831
- table: subscription.table,
832
- phase: 'idle',
833
- progressPercent: 0,
834
- startedAt: subscription.createdAt,
835
- };
836
- }
837
-
838
- private resolveChannelPhase(
839
- subscriptions: SubscriptionProgress[]
840
- ): SyncProgress['channelPhase'] {
841
- if (this.state.error) return 'error';
842
- if (subscriptions.some((sub) => sub.phase === 'error')) return 'error';
843
- if (subscriptions.some((sub) => sub.phase === 'bootstrapping')) {
844
- return 'bootstrapping';
845
- }
846
- if (this.state.isSyncing) {
847
- return this.state.lastSyncAt === null ? 'starting' : 'catching_up';
848
- }
849
- if (this.state.lastSyncAt !== null) return 'live';
850
- return 'idle';
851
- }
852
-
853
- private deriveProgressFromPullSubscription(
854
- sub: SyncPullSubscriptionResponse
855
- ): SubscriptionProgress {
856
- const stateId = this.getStateId();
857
- const key = this.makeBootstrapKey(stateId, sub.id);
858
- const startedAt = this.bootstrapStartedAt.get(key);
859
-
860
- if (sub.status === 'revoked') {
861
- return {
862
- stateId,
863
- id: sub.id,
864
- phase: 'error',
865
- progressPercent: 0,
866
- startedAt,
867
- completedAt: Date.now(),
868
- lastErrorCode: 'SUBSCRIPTION_REVOKED',
869
- lastErrorMessage: 'Subscription is revoked',
870
- };
871
- }
872
-
873
- if (sub.bootstrap && sub.bootstrapState) {
874
- const tableCount = Math.max(0, sub.bootstrapState.tables.length);
875
- const tableIndex = Math.max(0, sub.bootstrapState.tableIndex);
876
- const tablesProcessed = Math.min(tableCount, tableIndex);
877
- const progressPercent =
878
- tableCount === 0
879
- ? 0
880
- : Math.max(
881
- 0,
882
- Math.min(100, Math.round((tablesProcessed / tableCount) * 100))
883
- );
884
-
885
- return {
886
- stateId,
887
- id: sub.id,
888
- phase: 'bootstrapping',
889
- progressPercent,
890
- tablesProcessed,
891
- tablesTotal: tableCount,
892
- startedAt,
893
- };
894
- }
895
-
896
- return {
897
- stateId,
898
- id: sub.id,
899
- phase: this.state.isSyncing ? 'catching_up' : 'live',
900
- progressPercent: this.state.isSyncing ? 90 : 100,
901
- startedAt,
902
- completedAt: this.state.isSyncing ? undefined : Date.now(),
903
- };
904
- }
905
-
906
- private handleBootstrapLifecycle(response: SyncPullResponse): void {
907
- const stateId = this.getStateId();
908
- const now = Date.now();
909
- const seenKeys = new Set<string>();
910
-
911
- for (const sub of response.subscriptions ?? []) {
912
- const key = this.makeBootstrapKey(stateId, sub.id);
913
- seenKeys.add(key);
914
- const isBootstrapping = sub.bootstrap === true;
915
- const wasBootstrapping = this.activeBootstrapSubscriptions.has(key);
916
-
917
- if (isBootstrapping && !wasBootstrapping) {
918
- this.activeBootstrapSubscriptions.add(key);
919
- this.bootstrapStartedAt.set(key, now);
920
- this.emit('bootstrap:start', {
921
- timestamp: now,
922
- stateId,
923
- subscriptionId: sub.id,
924
- });
925
- }
926
-
927
- if (isBootstrapping) {
928
- this.emit('bootstrap:progress', {
929
- timestamp: now,
930
- stateId,
931
- subscriptionId: sub.id,
932
- progress: this.deriveProgressFromPullSubscription(sub),
933
- });
934
- }
935
-
936
- if (!isBootstrapping && wasBootstrapping) {
937
- const startedAt = this.bootstrapStartedAt.get(key) ?? now;
938
- this.activeBootstrapSubscriptions.delete(key);
939
- this.bootstrapStartedAt.delete(key);
940
- this.emit('bootstrap:complete', {
941
- timestamp: now,
942
- stateId,
943
- subscriptionId: sub.id,
944
- durationMs: Math.max(0, now - startedAt),
945
- });
946
- }
947
- }
948
-
949
- for (const key of Array.from(this.activeBootstrapSubscriptions)) {
950
- if (seenKeys.has(key)) continue;
951
- if (!key.startsWith(`${stateId}:`)) continue;
952
- const subscriptionId = key.slice(stateId.length + 1);
953
- if (!subscriptionId) continue;
954
-
955
- const startedAt = this.bootstrapStartedAt.get(key) ?? now;
956
- this.activeBootstrapSubscriptions.delete(key);
957
- this.bootstrapStartedAt.delete(key);
958
- this.emit('bootstrap:complete', {
959
- timestamp: now,
960
- stateId,
961
- subscriptionId,
962
- durationMs: Math.max(0, now - startedAt),
963
- });
964
- }
965
-
966
- if (this.activeBootstrapSubscriptions.size === 0 && !this.state.error) {
967
- this.emit('sync:live', { timestamp: now });
968
- }
969
- }
970
-
971
- private async resolveResetTargets(
972
- options: SyncResetOptions
973
- ): Promise<SubscriptionState[]> {
974
- const stateId = options.stateId ?? this.getStateId();
975
-
976
- if (options.scope === 'all') {
977
- return readSubscriptionStates(this.config.db);
978
- }
979
-
980
- if (options.scope === 'state') {
981
- return readSubscriptionStates(this.config.db, { stateId });
982
- }
983
-
984
- const subscriptionIds = options.subscriptionIds ?? [];
985
- if (subscriptionIds.length === 0) {
986
- throw new Error(
987
- '[SyncEngine.reset] subscriptionIds is required when scope="subscription"'
988
- );
989
- }
990
-
991
- const allInState = await readSubscriptionStates(this.config.db, {
992
- stateId,
993
- });
994
- const wanted = new Set(subscriptionIds);
995
- return allInState.filter((state) => wanted.has(state.subscriptionId));
996
- }
997
-
998
- private async clearSyncedTablesForReset(
999
- trx: Transaction<DB>,
1000
- options: SyncResetOptions,
1001
- targets: SubscriptionState[]
1002
- ): Promise<string[]> {
1003
- const clearedTables: string[] = [];
1004
-
1005
- if (!options.clearSyncedTables) {
1006
- return clearedTables;
1007
- }
1008
-
1009
- if (options.scope === 'all') {
1010
- for (const handler of this.config.handlers) {
1011
- await handler.clearAll({ trx, scopes: {} });
1012
- clearedTables.push(handler.table);
1013
- }
1014
- return clearedTables;
1015
- }
1016
-
1017
- const seen = new Set<string>();
1018
- for (const target of targets) {
1019
- const handler = getClientHandler(this.config.handlers, target.table);
1020
- if (!handler) continue;
1021
-
1022
- const key = `${target.table}:${JSON.stringify(target.scopes)}`;
1023
- if (seen.has(key)) continue;
1024
- seen.add(key);
1025
-
1026
- await handler.clearAll({ trx, scopes: target.scopes });
1027
- clearedTables.push(target.table);
1028
- }
1029
-
1030
- return clearedTables;
1031
- }
1032
-
1033
- async reset(options: SyncResetOptions): Promise<SyncResetResult> {
1034
- const resetOptions: SyncResetOptions = {
1035
- clearOutbox: false,
1036
- clearConflicts: false,
1037
- clearSyncedTables: false,
1038
- ...options,
1039
- };
1040
- const targets = await this.resolveResetTargets(resetOptions);
1041
- const stateId = resetOptions.stateId ?? this.getStateId();
1042
-
1043
- this.stop();
1044
-
1045
- const result = await this.config.db.transaction().execute(async (trx) => {
1046
- const clearedTables = await this.clearSyncedTablesForReset(
1047
- trx,
1048
- resetOptions,
1049
- targets
1050
- );
1051
-
1052
- let deletedSubscriptionStates = 0;
1053
- if (resetOptions.scope === 'all') {
1054
- const res = await sql`
1055
- delete from ${sql.table('sync_subscription_state')}
1056
- `.execute(trx);
1057
- deletedSubscriptionStates = Number(res.numAffectedRows ?? 0);
1058
- } else if (resetOptions.scope === 'state') {
1059
- const res = await sql`
1060
- delete from ${sql.table('sync_subscription_state')}
1061
- where ${sql.ref('state_id')} = ${sql.val(stateId)}
1062
- `.execute(trx);
1063
- deletedSubscriptionStates = Number(res.numAffectedRows ?? 0);
1064
- } else {
1065
- const subscriptionIds = resetOptions.subscriptionIds ?? [];
1066
- const res = await sql`
1067
- delete from ${sql.table('sync_subscription_state')}
1068
- where
1069
- ${sql.ref('state_id')} = ${sql.val(stateId)}
1070
- and ${sql.ref('subscription_id')} in (${sql.join(
1071
- subscriptionIds.map((id) => sql.val(id))
1072
- )})
1073
- `.execute(trx);
1074
- deletedSubscriptionStates = Number(res.numAffectedRows ?? 0);
1075
- }
1076
-
1077
- let deletedOutboxCommits = 0;
1078
- if (resetOptions.clearOutbox) {
1079
- const res = await sql`
1080
- delete from ${sql.table('sync_outbox_commits')}
1081
- `.execute(trx);
1082
- deletedOutboxCommits = Number(res.numAffectedRows ?? 0);
1083
- }
1084
-
1085
- let deletedConflicts = 0;
1086
- if (resetOptions.clearConflicts) {
1087
- const res = await sql`
1088
- delete from ${sql.table('sync_conflicts')}
1089
- `.execute(trx);
1090
- deletedConflicts = Number(res.numAffectedRows ?? 0);
1091
- }
1092
-
1093
- return {
1094
- deletedSubscriptionStates,
1095
- deletedOutboxCommits,
1096
- deletedConflicts,
1097
- clearedTables,
1098
- };
1099
- });
1100
-
1101
- if (resetOptions.scope === 'all') {
1102
- this.activeBootstrapSubscriptions.clear();
1103
- this.bootstrapStartedAt.clear();
1104
- } else {
1105
- for (const target of targets) {
1106
- const key = this.makeBootstrapKey(
1107
- target.stateId,
1108
- target.subscriptionId
1109
- );
1110
- this.activeBootstrapSubscriptions.delete(key);
1111
- this.bootstrapStartedAt.delete(key);
1112
- }
1113
- }
1114
-
1115
- this.resetLocalState();
1116
- await this.refreshOutboxStats();
1117
- this.updateState({ error: null });
1118
-
1119
- return result;
1120
- }
1121
-
1122
- async repair(options: SyncRepairOptions): Promise<SyncResetResult> {
1123
- if (options.mode !== 'rebootstrap-missing-chunks') {
1124
- throw new Error(
1125
- `[SyncEngine.repair] Unsupported repair mode: ${options.mode}`
1126
- );
1127
- }
1128
-
1129
- return this.reset({
1130
- scope: options.subscriptionIds ? 'subscription' : 'state',
1131
- stateId: options.stateId,
1132
- subscriptionIds: options.subscriptionIds,
1133
- clearOutbox: options.clearOutbox ?? false,
1134
- clearConflicts: options.clearConflicts ?? false,
1135
- clearSyncedTables: true,
1136
- });
1137
- }
1138
-
1139
- /**
1140
- * Subscribe to sync events
1141
- */
1142
- on<T extends SyncEventType>(
1143
- event: T,
1144
- listener: SyncEventListener<T>
1145
- ): () => void {
1146
- if (!this.listeners.has(event)) {
1147
- this.listeners.set(event, new Set());
1148
- }
1149
- const wrapped: AnyEventListener = (payload) => {
1150
- listener(payload as SyncEventPayloads[T]);
1151
- };
1152
- this.listeners.get(event)!.add(wrapped);
1153
-
1154
- return () => {
1155
- this.listeners.get(event)?.delete(wrapped);
1156
- };
1157
- }
1158
-
1159
- /**
1160
- * Subscribe to any state change (for useSyncExternalStore)
1161
- */
1162
- subscribe(callback: () => void): () => void {
1163
- // Subscribe to state:change which is emitted by updateState()
1164
- return this.on('state:change', callback);
1165
- }
1166
-
1167
- private emit<T extends SyncEventType>(
1168
- event: T,
1169
- payload: SyncEventPayloads[T]
1170
- ): void {
1171
- this.inspectorEvents.push({
1172
- id: this.nextInspectorEventId++,
1173
- event,
1174
- timestamp: Date.now(),
1175
- payload: serializeInspectorRecord(payload),
1176
- });
1177
- if (this.inspectorEvents.length > MAX_INSPECTOR_EVENT_LIMIT) {
1178
- this.inspectorEvents.splice(
1179
- 0,
1180
- this.inspectorEvents.length - MAX_INSPECTOR_EVENT_LIMIT
1181
- );
1182
- }
1183
-
1184
- const eventListeners = this.listeners.get(event);
1185
- if (eventListeners) {
1186
- for (const listener of eventListeners) {
1187
- try {
1188
- listener(payload);
1189
- } catch (err) {
1190
- console.error(`[SyncEngine] Error in ${event} listener:`, err);
1191
- }
1192
- }
1193
- }
1194
- }
1195
-
1196
- private updateState(partial: Partial<SyncEngineState>): void {
1197
- this.state = { ...this.state, ...partial };
1198
- // Emit state:change to notify useSyncExternalStore subscribers
1199
- this.emit('state:change', {});
1200
- }
1201
-
1202
- private setConnectionState(state: SyncConnectionState): void {
1203
- const previous = this.state.connectionState;
1204
- if (previous !== state) {
1205
- this.updateState({ connectionState: state });
1206
- this.emit('connection:change', { previous, current: state });
1207
- }
1208
- }
1209
-
1210
- /**
1211
- * Start the sync engine
1212
- */
1213
- async start(): Promise<void> {
1214
- if (this.isDestroyed) {
1215
- throw new Error('SyncEngine has been destroyed');
1216
- }
1217
-
1218
- if (!this.isEnabled()) {
1219
- this.updateState({ enabled: false });
1220
- return;
1221
- }
1222
-
1223
- this.updateState({ enabled: true });
1224
-
1225
- // Run migrations before first sync.
1226
- if (!this.migrated) {
1227
- // Best-effort: push pending commits before user migration, because
1228
- // app migrations may reset tables and discard unsynced local writes.
1229
- if (this.config.migrate) {
1230
- try {
1231
- const hasOutbox = await sql`
1232
- select 1 from ${sql.table('sync_outbox_commits')} limit 1
1233
- `
1234
- .execute(this.config.db)
1235
- .then((r) => r.rows.length > 0)
1236
- .catch(() => false);
1237
-
1238
- if (hasOutbox) {
1239
- let pushed = true;
1240
- while (pushed) {
1241
- const result = await syncPushOnce(
1242
- this.config.db,
1243
- this.config.transport,
1244
- {
1245
- clientId: this.config.clientId!,
1246
- actorId: this.config.actorId ?? undefined,
1247
- plugins: this.config.plugins,
1248
- }
1249
- );
1250
- pushed = result.pushed;
1251
- }
1252
- }
1253
- } catch {
1254
- // Best-effort: continue even if pre-migration push fails.
1255
- }
1256
- }
1257
-
1258
- try {
1259
- if (this.config.migrate) {
1260
- await this.config.migrate(this.config.db);
1261
- }
1262
- await ensureClientSyncSchema(this.config.db);
1263
- this.migrated = true;
1264
- } catch (err) {
1265
- const migrationError =
1266
- err instanceof Error ? err : new Error(String(err));
1267
- this.config.onMigrationError?.(migrationError);
1268
- const error = createSyncError({
1269
- code: 'MIGRATION_FAILED',
1270
- message: 'Migration failed',
1271
- cause: migrationError,
1272
- retryable: false,
1273
- stateId: this.getStateId(),
1274
- });
1275
- this.updateState({
1276
- isSyncing: false,
1277
- error,
1278
- });
1279
- this.handleError(error);
1280
- return;
1281
- }
1282
- }
1283
-
1284
- // Setup transport-specific handling
1285
- if (this.state.transportMode === 'realtime') {
1286
- this.setupRealtime();
1287
- } else {
1288
- this.setupPolling();
1289
- }
1290
-
1291
- // Initial sync
1292
- await this.sync();
1293
- }
1294
-
1295
- /**
1296
- * Stop the sync engine (cleanup without destroy)
1297
- */
1298
- stop(): void {
1299
- this.stopPolling();
1300
- this.stopRealtime();
1301
- this.setConnectionState('disconnected');
1302
- }
1303
-
1304
- /**
1305
- * Destroy the engine (cannot be restarted)
1306
- */
1307
- destroy(): void {
1308
- this.stop();
1309
- this.listeners.clear();
1310
- this.isDestroyed = true;
1311
-
1312
- if (this.retryTimeoutId) {
1313
- clearTimeout(this.retryTimeoutId);
1314
- this.retryTimeoutId = null;
1315
- }
1316
- if (this.realtimeCatchupTimeoutId) {
1317
- clearTimeout(this.realtimeCatchupTimeoutId);
1318
- this.realtimeCatchupTimeoutId = null;
1319
- }
1320
- }
1321
-
1322
- /**
1323
- * Trigger a manual sync
1324
- */
1325
- async sync(opts?: {
1326
- trigger?: 'ws' | 'local' | 'poll';
1327
- }): Promise<SyncResult> {
1328
- // Dedupe concurrent sync calls
1329
- if (this.syncPromise) {
1330
- // A sync is already in-flight; queue one more run so we don't miss
1331
- // mutations enqueued during the current cycle (important in realtime mode).
1332
- this.syncRequestedWhileRunning = true;
1333
- return this.syncPromise;
1334
- }
1335
-
1336
- if (
1337
- !this.isEnabled() ||
1338
- this.isDestroyed ||
1339
- this.state.connectionState === 'disconnected'
1340
- ) {
1341
- return {
1342
- success: false,
1343
- pushedCommits: 0,
1344
- pullRounds: 0,
1345
- pullResponse: { ok: true, subscriptions: [] },
1346
- error: createSyncError({
1347
- code: 'SYNC_ERROR',
1348
- message: 'Sync not enabled',
1349
- retryable: false,
1350
- stateId: this.getStateId(),
1351
- }),
1352
- };
1353
- }
1354
-
1355
- this.syncPromise = this.performSyncLoop(opts?.trigger);
1356
- try {
1357
- return await this.syncPromise;
1358
- } finally {
1359
- this.syncPromise = null;
1360
- }
1361
- }
1362
-
1363
- private async performSyncLoop(
1364
- trigger?: 'ws' | 'local' | 'poll'
1365
- ): Promise<SyncResult> {
1366
- let lastResult: SyncResult = {
1367
- success: false,
1368
- pushedCommits: 0,
1369
- pullRounds: 0,
1370
- pullResponse: { ok: true, subscriptions: [] },
1371
- error: createSyncError({
1372
- code: 'SYNC_ERROR',
1373
- message: 'Sync not started',
1374
- retryable: false,
1375
- stateId: this.getStateId(),
1376
- }),
1377
- };
1378
-
1379
- do {
1380
- this.syncRequestedWhileRunning = false;
1381
- lastResult = await this.performSyncOnce(trigger);
1382
- // After the first iteration, clear trigger context
1383
- trigger = undefined;
1384
- // If the sync failed, let retry logic handle backoff instead of tight looping.
1385
- if (!lastResult.success) break;
1386
- } while (
1387
- this.syncRequestedWhileRunning &&
1388
- !this.isDestroyed &&
1389
- this.isEnabled()
1390
- );
1391
-
1392
- return lastResult;
1393
- }
1394
-
1395
- private async performSyncOnce(
1396
- trigger?: 'ws' | 'local' | 'poll'
1397
- ): Promise<SyncResult> {
1398
- const timestamp = Date.now();
1399
- const startedAtMs = timestamp;
1400
- const triggerLabel = resolveSyncTriggerLabel(trigger);
1401
- this.updateState({ isSyncing: true });
1402
- this.emit('sync:start', { timestamp });
1403
- countSyncMetric('sync.client.sync.attempts', 1, {
1404
- attributes: { trigger: triggerLabel },
1405
- });
1406
-
1407
- try {
1408
- const pullApplyTimestamp = Date.now();
1409
- const result = await startSyncSpan(
1410
- {
1411
- name: 'sync.client.sync',
1412
- op: 'sync.client.sync',
1413
- attributes: { trigger: triggerLabel },
1414
- },
1415
- () =>
1416
- syncOnce(
1417
- this.config.db,
1418
- this.config.transport,
1419
- this.config.handlers,
1420
- {
1421
- clientId: this.config.clientId!,
1422
- actorId: this.config.actorId ?? undefined,
1423
- plugins: this.config.plugins,
1424
- subscriptions: this.config
1425
- .subscriptions as SyncSubscriptionRequest[],
1426
- limitCommits: this.config.limitCommits,
1427
- limitSnapshotRows: this.config.limitSnapshotRows,
1428
- maxSnapshotPages: this.config.maxSnapshotPages,
1429
- stateId: this.config.stateId,
1430
- sha256: this.config.sha256,
1431
- trigger,
1432
- }
1433
- )
1434
- );
1435
-
1436
- const syncResult: SyncResult = {
1437
- success: true,
1438
- pushedCommits: result.pushedCommits,
1439
- pullRounds: result.pullRounds,
1440
- pullResponse: result.pullResponse,
1441
- };
1442
-
1443
- // Update fingerprint mutation timestamps for server-applied changes so wa-sqlite
1444
- // query hooks rerender on remote changes (not just local mutations).
1445
- this.recordMutationTimestampsFromPullResponse(
1446
- result.pullResponse,
1447
- pullApplyTimestamp
1448
- );
1449
-
1450
- this.updateState({
1451
- isSyncing: false,
1452
- lastSyncAt: Date.now(),
1453
- error: null,
1454
- retryCount: 0,
1455
- isRetrying: false,
1456
- });
1457
- this.updateTransportHealth({
1458
- lastSuccessfulPollAt: Date.now(),
1459
- });
1460
-
1461
- this.emit('sync:complete', {
1462
- timestamp: Date.now(),
1463
- pushedCommits: result.pushedCommits,
1464
- pullRounds: result.pullRounds,
1465
- pullResponse: result.pullResponse,
1466
- });
1467
-
1468
- // Emit data change for any tables that had changes
1469
- const changedTables = this.extractChangedTables(result.pullResponse);
1470
- if (changedTables.length > 0) {
1471
- this.emit('data:change', {
1472
- scopes: changedTables,
1473
- timestamp: Date.now(),
1474
- });
1475
- this.config.onDataChange?.(changedTables);
1476
- }
1477
- this.handleBootstrapLifecycle(result.pullResponse);
1478
-
1479
- // Refresh outbox stats (fire-and-forget — don't block sync:complete)
1480
- this.refreshOutboxStats().catch((error) => {
1481
- console.warn(
1482
- '[SyncEngine] Failed to refresh outbox stats after sync:',
1483
- error
1484
- );
1485
- });
1486
-
1487
- const durationMs = Math.max(0, Date.now() - startedAtMs);
1488
- countSyncMetric('sync.client.sync.results', 1, {
1489
- attributes: {
1490
- trigger: triggerLabel,
1491
- status: 'success',
1492
- },
1493
- });
1494
- distributionSyncMetric('sync.client.sync.duration_ms', durationMs, {
1495
- unit: 'millisecond',
1496
- attributes: {
1497
- trigger: triggerLabel,
1498
- status: 'success',
1499
- },
1500
- });
1501
- distributionSyncMetric(
1502
- 'sync.client.sync.pushed_commits',
1503
- result.pushedCommits,
1504
- {
1505
- attributes: { trigger: triggerLabel },
1506
- }
1507
- );
1508
- distributionSyncMetric(
1509
- 'sync.client.sync.pull_rounds',
1510
- result.pullRounds,
1511
- {
1512
- attributes: { trigger: triggerLabel },
1513
- }
1514
- );
1515
-
1516
- return syncResult;
1517
- } catch (err) {
1518
- const classified = classifySyncFailure(err);
1519
- const error = createSyncError({
1520
- code: classified.code,
1521
- message: classified.message,
1522
- cause: classified.cause,
1523
- retryable: classified.retryable,
1524
- httpStatus: classified.httpStatus,
1525
- stateId: this.getStateId(),
1526
- });
1527
-
1528
- this.updateState({
1529
- isSyncing: false,
1530
- error,
1531
- retryCount: this.state.retryCount + 1,
1532
- isRetrying: false,
1533
- });
1534
-
1535
- this.handleError(error);
1536
-
1537
- const durationMs = Math.max(0, Date.now() - startedAtMs);
1538
- countSyncMetric('sync.client.sync.results', 1, {
1539
- attributes: {
1540
- trigger: triggerLabel,
1541
- status: 'error',
1542
- },
1543
- });
1544
- distributionSyncMetric('sync.client.sync.duration_ms', durationMs, {
1545
- unit: 'millisecond',
1546
- attributes: {
1547
- trigger: triggerLabel,
1548
- status: 'error',
1549
- },
1550
- });
1551
- captureSyncException(err, {
1552
- event: 'sync.client.sync',
1553
- trigger: triggerLabel,
1554
- });
1555
-
1556
- // Schedule retry if under max retries
1557
- const maxRetries = this.config.maxRetries ?? DEFAULT_MAX_RETRIES;
1558
- if (error.retryable && this.state.retryCount < maxRetries) {
1559
- this.scheduleRetry();
1560
- }
1561
-
1562
- return {
1563
- success: false,
1564
- pushedCommits: 0,
1565
- pullRounds: 0,
1566
- pullResponse: { ok: true, subscriptions: [] },
1567
- error,
1568
- };
1569
- }
1570
- }
1571
-
1572
- private extractChangedTables(response: SyncPullResponse): string[] {
1573
- const tables = new Set<string>();
1574
- for (const sub of response.subscriptions ?? []) {
1575
- // Extract tables from snapshots
1576
- for (const snapshot of sub.snapshots ?? []) {
1577
- if (snapshot.table) {
1578
- tables.add(snapshot.table);
1579
- }
1580
- }
1581
- // Extract tables from commits
1582
- for (const commit of sub.commits ?? []) {
1583
- for (const change of commit.changes ?? []) {
1584
- if (change.table) {
1585
- tables.add(change.table);
1586
- }
1587
- }
1588
- }
1589
- }
1590
-
1591
- return Array.from(tables);
1592
- }
1593
-
1594
- /**
1595
- * Apply changes delivered inline over WebSocket for instant UI updates.
1596
- * Returns true if changes were applied and cursor updated successfully,
1597
- * false if anything failed (caller should fall back to HTTP sync).
1598
- */
1599
- private async applyWsDeliveredChanges(
1600
- changes: SyncChange[],
1601
- cursor: number
1602
- ): Promise<boolean> {
1603
- try {
1604
- await this.config.db.transaction().execute(async (trx) => {
1605
- for (const change of changes) {
1606
- const handler = getClientHandler(this.config.handlers, change.table);
1607
- if (!handler) {
1608
- throw new Error(
1609
- `Missing client table handler for WS change table "${change.table}"`
1610
- );
1611
- }
1612
- await handler.applyChange({ trx }, change);
1613
- }
1614
-
1615
- // Update subscription cursors
1616
- const stateId = this.config.stateId ?? 'default';
1617
- await sql`
1618
- update ${sql.table('sync_subscription_state')}
1619
- set ${sql.ref('cursor')} = ${sql.val(cursor)}
1620
- where ${sql.ref('state_id')} = ${sql.val(stateId)}
1621
- and ${sql.ref('cursor')} < ${sql.val(cursor)}
1622
- `.execute(trx);
1623
- });
1624
-
1625
- // Update mutation timestamps BEFORE emitting data:change so that
1626
- // React hooks re-querying the DB see fresh fingerprints immediately.
1627
- const now = Date.now();
1628
- for (const change of changes) {
1629
- if (!change.table || !change.row_id) continue;
1630
- if (change.op === 'delete') {
1631
- this.mutationTimestamps.delete(`${change.table}:${change.row_id}`);
1632
- } else {
1633
- this.bumpMutationTimestamp(change.table, change.row_id, now);
1634
- }
1635
- }
1636
-
1637
- // Emit data change for immediate UI update
1638
- const changedTables = [...new Set(changes.map((c) => c.table))];
1639
- if (changedTables.length > 0) {
1640
- this.emit('data:change', {
1641
- scopes: changedTables,
1642
- timestamp: Date.now(),
1643
- });
1644
- this.config.onDataChange?.(changedTables);
1645
- }
1646
-
1647
- return true;
1648
- } catch {
1649
- return false;
1650
- }
1651
- }
1652
-
1653
- /**
1654
- * Handle WS-delivered changes: apply them and decide whether to skip HTTP pull.
1655
- * Falls back to full HTTP sync when conditions require it.
1656
- */
1657
- private async handleWsDelivery(
1658
- changes: SyncChange[],
1659
- cursor: number
1660
- ): Promise<void> {
1661
- // If a sync is already in-flight, let it handle everything
1662
- if (this.syncPromise) {
1663
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1664
- attributes: { path: 'inflight_sync' },
1665
- });
1666
- this.triggerSyncInBackground(
1667
- { trigger: 'ws' },
1668
- 'ws delivery with in-flight sync'
1669
- );
1670
- return;
1671
- }
1672
-
1673
- // If there are pending outbox commits, need to push via HTTP
1674
- if (this.state.pendingCount > 0) {
1675
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1676
- attributes: { path: 'pending_outbox' },
1677
- });
1678
- this.triggerSyncInBackground(
1679
- { trigger: 'ws' },
1680
- 'ws delivery with pending outbox'
1681
- );
1682
- return;
1683
- }
1684
-
1685
- // If afterPull plugins exist, inline WS changes may require transforms
1686
- // (e.g. decryption). Fall back to HTTP sync and do not apply inline payload.
1687
- const hasAfterPullPlugins = this.config.plugins?.some(
1688
- (p) => typeof p.afterPull === 'function'
1689
- );
1690
- if (hasAfterPullPlugins) {
1691
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1692
- attributes: { path: 'after_pull_plugins' },
1693
- });
1694
- this.triggerSyncInBackground(
1695
- { trigger: 'ws' },
1696
- 'ws delivery with afterPull plugins'
1697
- );
1698
- return;
1699
- }
1700
-
1701
- // Apply changes + update cursor
1702
- const inlineApplyStartedAtMs = Date.now();
1703
- const applied = await this.applyWsDeliveredChanges(changes, cursor);
1704
- const inlineApplyDurationMs = Math.max(
1705
- 0,
1706
- Date.now() - inlineApplyStartedAtMs
1707
- );
1708
- distributionSyncMetric(
1709
- 'sync.client.ws.inline_apply.duration_ms',
1710
- inlineApplyDurationMs,
1711
- {
1712
- unit: 'millisecond',
1713
- }
1714
- );
1715
-
1716
- if (!applied) {
1717
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1718
- attributes: { path: 'inline_fallback' },
1719
- });
1720
- this.triggerSyncInBackground(
1721
- { trigger: 'ws' },
1722
- 'ws inline apply fallback'
1723
- );
1724
- return;
1725
- }
1726
-
1727
- // All clear — skip HTTP pull entirely
1728
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1729
- attributes: { path: 'inline_applied' },
1730
- });
1731
- this.updateState({
1732
- lastSyncAt: Date.now(),
1733
- error: null,
1734
- retryCount: 0,
1735
- isRetrying: false,
1736
- });
1737
- this.updateTransportHealth({
1738
- mode: 'realtime',
1739
- connected: true,
1740
- fallbackReason: null,
1741
- lastSuccessfulPollAt: Date.now(),
1742
- });
1743
-
1744
- this.emit('sync:complete', {
1745
- timestamp: Date.now(),
1746
- pushedCommits: 0,
1747
- pullRounds: 0,
1748
- pullResponse: { ok: true, subscriptions: [] },
1749
- });
1750
- this.emit('sync:live', { timestamp: Date.now() });
1751
-
1752
- this.refreshOutboxStats().catch((error) => {
1753
- console.warn(
1754
- '[SyncEngine] Failed to refresh outbox stats after WS apply:',
1755
- error
1756
- );
1757
- });
1758
- }
1759
-
1760
- private timestampCounter = 0;
1761
-
1762
- private nextPreciseTimestamp(now: number): number {
1763
- // Use sub-millisecond precision by combining timestamp with atomic counter
1764
- // This prevents race conditions in concurrent mutations while maintaining
1765
- // millisecond-level compatibility with existing code.
1766
- return now + (this.timestampCounter++ % 1000) / 1000;
1767
- }
1768
-
1769
- private bumpMutationTimestamp(
1770
- table: string,
1771
- rowId: string,
1772
- now: number
1773
- ): void {
1774
- const key = `${table}:${rowId}`;
1775
- const preciseNow = this.nextPreciseTimestamp(now);
1776
- const prev = this.mutationTimestamps.get(key) ?? 0;
1777
- this.mutationTimestamps.set(key, Math.max(preciseNow, prev + 0.001));
1778
- }
1779
-
1780
- private bumpTableMutationTimestamp(table: string, now: number): void {
1781
- const preciseNow = this.nextPreciseTimestamp(now);
1782
- const prev = this.tableMutationTimestamps.get(table) ?? 0;
1783
- this.tableMutationTimestamps.set(table, Math.max(preciseNow, prev + 0.001));
1784
- }
1785
-
1786
- /**
1787
- * Record local mutations that were already applied to the DB.
1788
- *
1789
- * This updates in-memory mutation timestamps (for fingerprint-based rerenders),
1790
- * and emits a single `data:change` event for the affected tables.
1791
- *
1792
- * This is intentionally separate from applyLocalMutation() so callers that
1793
- * perform their own DB transactions (e.g. `useMutations`) can still keep UI
1794
- * updates correct without double-writing.
1795
- */
1796
- recordLocalMutations(
1797
- inputs: Array<{
1798
- table: string;
1799
- rowId: string;
1800
- op: 'upsert' | 'delete';
1801
- }>,
1802
- now = Date.now()
1803
- ): void {
1804
- const affectedTables = new Set<string>();
1805
-
1806
- for (const input of inputs) {
1807
- if (!input.table || !input.rowId) continue;
1808
- affectedTables.add(input.table);
1809
-
1810
- if (input.op === 'delete') {
1811
- this.mutationTimestamps.delete(`${input.table}:${input.rowId}`);
1812
- continue;
1813
- }
1814
-
1815
- this.bumpMutationTimestamp(input.table, input.rowId, now);
1816
- }
1817
-
1818
- if (affectedTables.size > 0) {
1819
- this.emit('data:change', {
1820
- scopes: Array.from(affectedTables),
1821
- timestamp: Date.now(),
1822
- });
1823
- this.config.onDataChange?.(Array.from(affectedTables));
1824
- }
1825
- }
1826
-
1827
- private recordMutationTimestampsFromPullResponse(
1828
- response: SyncPullResponse,
1829
- now: number
1830
- ): void {
1831
- for (const sub of response.subscriptions ?? []) {
1832
- // Mark snapshot tables as changed so bootstrap/resnapshot updates
1833
- // propagate without storing per-row timestamps for massive snapshots.
1834
- for (const snapshot of sub.snapshots ?? []) {
1835
- if (!snapshot.table) continue;
1836
- this.bumpTableMutationTimestamp(snapshot.table, now);
1837
- }
1838
-
1839
- for (const commit of sub.commits ?? []) {
1840
- for (const change of commit.changes ?? []) {
1841
- const table = change.table;
1842
- const rowId = change.row_id;
1843
- if (!table || !rowId) continue;
1844
-
1845
- if (change.op === 'delete') {
1846
- this.mutationTimestamps.delete(`${table}:${rowId}`);
1847
- } else {
1848
- this.bumpMutationTimestamp(table, rowId, now);
1849
- }
1850
- }
1851
- }
1852
- }
1853
- }
1854
-
1855
- private scheduleRetry(): void {
1856
- if (this.retryTimeoutId) {
1857
- clearTimeout(this.retryTimeoutId);
1858
- }
1859
-
1860
- const delay = calculateRetryDelay(this.state.retryCount);
1861
- if (this.state.pendingCount > 0) {
1862
- countSyncMetric('sync.outbox.retry_count', 1, {
1863
- attributes: {
1864
- retryCount: this.state.retryCount,
1865
- },
1866
- });
1867
- }
1868
- this.updateState({ isRetrying: true });
1869
-
1870
- this.retryTimeoutId = setTimeout(() => {
1871
- this.retryTimeoutId = null;
1872
- if (!this.isDestroyed) {
1873
- this.triggerSyncInBackground(undefined, 'retry timer');
1874
- }
1875
- }, delay);
1876
- }
1877
-
1878
- private handleError(error: SyncError): void {
1879
- this.emit('sync:error', error);
1880
- this.config.onError?.(error);
1881
- }
1882
-
1883
- private triggerSyncInBackground(
1884
- opts?: { trigger?: 'ws' | 'local' | 'poll' },
1885
- reason = 'background'
1886
- ): void {
1887
- void this.sync(opts).catch((error) => {
1888
- console.error(
1889
- `[SyncEngine] Unexpected sync failure during ${reason}:`,
1890
- error
1891
- );
1892
- });
1893
- }
1894
-
1895
- private setupPolling(): void {
1896
- this.stopPolling();
1897
-
1898
- const interval = this.config.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
1899
- this.pollerId = setInterval(() => {
1900
- if (!this.state.isSyncing && !this.isDestroyed) {
1901
- this.triggerSyncInBackground(undefined, 'polling interval');
1902
- }
1903
- }, interval);
1904
-
1905
- this.setConnectionState('connected');
1906
- this.updateTransportHealth({
1907
- mode: 'polling',
1908
- connected: true,
1909
- fallbackReason: null,
1910
- });
1911
- }
1912
-
1913
- private stopPolling(): void {
1914
- if (this.pollerId) {
1915
- clearInterval(this.pollerId);
1916
- this.pollerId = null;
1917
- }
1918
- }
1919
-
1920
- private setupRealtime(): void {
1921
- if (!isRealtimeTransport(this.config.transport)) {
1922
- console.warn(
1923
- '[SyncEngine] realtimeEnabled=true but transport does not support realtime. Falling back to polling.'
1924
- );
1925
- this.updateState({ transportMode: 'polling' });
1926
- this.setupPolling();
1927
- return;
1928
- }
1929
-
1930
- this.setConnectionState('connecting');
1931
- this.updateTransportHealth({
1932
- mode: 'disconnected',
1933
- connected: false,
1934
- fallbackReason: null,
1935
- });
1936
-
1937
- const transport = this.config.transport as RealtimeTransportLike;
1938
-
1939
- // Wire up presence events if transport supports them
1940
- if (transport.onPresenceEvent) {
1941
- this.realtimePresenceUnsub = transport.onPresenceEvent((event) => {
1942
- if (event.action === 'snapshot' && event.entries) {
1943
- this.updatePresence(event.scopeKey, event.entries);
1944
- } else if (
1945
- event.action === 'join' ||
1946
- event.action === 'leave' ||
1947
- event.action === 'update'
1948
- ) {
1949
- this.handlePresenceEvent({
1950
- action: event.action,
1951
- scopeKey: event.scopeKey,
1952
- clientId: event.clientId ?? '',
1953
- actorId: event.actorId ?? '',
1954
- metadata: event.metadata,
1955
- });
1956
- }
1957
- });
1958
- }
1959
-
1960
- this.realtimeDisconnect = transport.connect(
1961
- { clientId: this.config.clientId! },
1962
- (event) => {
1963
- if (event.event === 'sync') {
1964
- this.updateTransportHealth({
1965
- lastRealtimeMessageAt: Date.now(),
1966
- });
1967
- countSyncMetric('sync.client.ws.events', 1, {
1968
- attributes: { type: 'sync' },
1969
- });
1970
- const hasInlineChanges =
1971
- Array.isArray(event.data.changes) && event.data.changes.length > 0;
1972
- const cursor = event.data.cursor;
1973
-
1974
- if (hasInlineChanges && typeof cursor === 'number') {
1975
- // WS delivered changes + cursor — may skip HTTP pull
1976
- this.handleWsDelivery(event.data.changes as SyncChange[], cursor);
1977
- } else {
1978
- // Cursor-only wake-up or no cursor — must HTTP sync
1979
- countSyncMetric('sync.client.ws.delivery.events', 1, {
1980
- attributes: { path: 'cursor_wakeup' },
1981
- });
1982
- this.triggerSyncInBackground({ trigger: 'ws' }, 'ws cursor wakeup');
1983
- }
1984
- }
1985
- },
1986
- (state) => {
1987
- switch (state) {
1988
- case 'connected': {
1989
- const wasConnectedBefore = this.hasRealtimeConnectedOnce;
1990
- this.hasRealtimeConnectedOnce = true;
1991
- if (wasConnectedBefore) {
1992
- countSyncMetric('sync.transport.reconnects', 1, {
1993
- attributes: {
1994
- source: 'client',
1995
- },
1996
- });
1997
- }
1998
- this.setConnectionState('connected');
1999
- this.updateTransportHealth({
2000
- mode: 'realtime',
2001
- connected: true,
2002
- fallbackReason: null,
2003
- });
2004
- this.stopFallbackPolling();
2005
- this.triggerSyncInBackground(undefined, 'realtime connected state');
2006
- if (wasConnectedBefore) {
2007
- this.scheduleRealtimeReconnectCatchupSync();
2008
- }
2009
- break;
2010
- }
2011
- case 'connecting':
2012
- this.setConnectionState('connecting');
2013
- this.updateTransportHealth({
2014
- mode: 'disconnected',
2015
- connected: false,
2016
- });
2017
- break;
2018
- case 'disconnected':
2019
- this.setConnectionState('reconnecting');
2020
- this.updateTransportHealth({
2021
- mode: 'disconnected',
2022
- connected: false,
2023
- });
2024
- this.startFallbackPolling();
2025
- break;
2026
- }
2027
- }
2028
- );
2029
- }
2030
-
2031
- private stopRealtime(): void {
2032
- if (this.realtimeCatchupTimeoutId) {
2033
- clearTimeout(this.realtimeCatchupTimeoutId);
2034
- this.realtimeCatchupTimeoutId = null;
2035
- }
2036
- if (this.realtimePresenceUnsub) {
2037
- this.realtimePresenceUnsub();
2038
- this.realtimePresenceUnsub = null;
2039
- }
2040
- if (this.realtimeDisconnect) {
2041
- this.realtimeDisconnect();
2042
- this.realtimeDisconnect = null;
2043
- }
2044
- this.stopFallbackPolling();
2045
- this.updateTransportHealth({
2046
- mode: 'disconnected',
2047
- connected: false,
2048
- });
2049
- }
2050
-
2051
- private scheduleRealtimeReconnectCatchupSync(): void {
2052
- if (this.realtimeCatchupTimeoutId) {
2053
- clearTimeout(this.realtimeCatchupTimeoutId);
2054
- }
2055
-
2056
- this.realtimeCatchupTimeoutId = setTimeout(() => {
2057
- this.realtimeCatchupTimeoutId = null;
2058
-
2059
- if (this.isDestroyed || !this.isEnabled()) return;
2060
- if (this.state.connectionState !== 'connected') return;
2061
-
2062
- this.triggerSyncInBackground(undefined, 'realtime reconnect catchup');
2063
- }, REALTIME_RECONNECT_CATCHUP_DELAY_MS);
2064
- }
2065
-
2066
- private startFallbackPolling(): void {
2067
- if (this.fallbackPollerId) return;
2068
-
2069
- const interval = this.config.realtimeFallbackPollMs ?? 30_000;
2070
- this.updateTransportHealth({
2071
- mode: 'polling',
2072
- connected: false,
2073
- fallbackReason: 'network',
2074
- });
2075
- this.fallbackPollerId = setInterval(() => {
2076
- if (!this.state.isSyncing && !this.isDestroyed) {
2077
- this.triggerSyncInBackground(undefined, 'realtime fallback poll');
2078
- }
2079
- }, interval);
2080
- }
2081
-
2082
- private stopFallbackPolling(): void {
2083
- if (this.fallbackPollerId) {
2084
- clearInterval(this.fallbackPollerId);
2085
- this.fallbackPollerId = null;
2086
- }
2087
- this.updateTransportHealth({ fallbackReason: null });
2088
- }
2089
-
2090
- /**
2091
- * Clear all in-memory mutation state and emit data:change so UI re-renders.
2092
- * Call this after deleting local data (e.g. reset flow) so that React hooks
2093
- * recompute fingerprints from scratch instead of seeing stale timestamps.
2094
- */
2095
- resetLocalState(): void {
2096
- const tables = [...this.tableMutationTimestamps.keys()];
2097
- this.mutationTimestamps.clear();
2098
- this.tableMutationTimestamps.clear();
2099
-
2100
- if (tables.length > 0) {
2101
- this.emit('data:change', {
2102
- scopes: tables,
2103
- timestamp: Date.now(),
2104
- });
2105
- this.config.onDataChange?.(tables);
2106
- }
2107
- }
2108
-
2109
- /**
2110
- * Reconnect
2111
- */
2112
- reconnect(): void {
2113
- if (this.isDestroyed || !this.isEnabled()) return;
2114
-
2115
- if (
2116
- this.state.transportMode === 'realtime' &&
2117
- isRealtimeTransport(this.config.transport)
2118
- ) {
2119
- // If we previously disconnected, we need to re-register callbacks via connect().
2120
- if (!this.realtimeDisconnect) {
2121
- this.setupRealtime();
2122
- } else {
2123
- this.config.transport.reconnect();
2124
- }
2125
- return;
2126
- }
2127
-
2128
- // Polling mode: restart the poller and trigger a sync immediately.
2129
- if (this.state.transportMode === 'polling') {
2130
- this.setupPolling();
2131
- this.triggerSyncInBackground(undefined, 'reconnect');
2132
- }
2133
- }
2134
-
2135
- /**
2136
- * Disconnect (pause syncing)
2137
- */
2138
- disconnect(): void {
2139
- this.stop();
2140
- }
2141
-
2142
- /**
2143
- * Refresh outbox statistics
2144
- */
2145
- async refreshOutboxStats(options?: { emit?: boolean }): Promise<OutboxStats> {
2146
- const db = this.config.db;
2147
-
2148
- const res = await sql<{ status: OutboxCommitStatus; count: number }>`
2149
- select
2150
- ${sql.ref('status')},
2151
- count(${sql.ref('id')}) as ${sql.ref('count')}
2152
- from ${sql.table('sync_outbox_commits')}
2153
- group by ${sql.ref('status')}
2154
- `.execute(db);
2155
- const rows = res.rows;
2156
-
2157
- const stats: OutboxStats = {
2158
- pending: 0,
2159
- sending: 0,
2160
- failed: 0,
2161
- acked: 0,
2162
- total: 0,
2163
- };
2164
-
2165
- for (const row of rows) {
2166
- const count = Number(row.count);
2167
- switch (row.status) {
2168
- case 'pending':
2169
- stats.pending = count;
2170
- break;
2171
- case 'sending':
2172
- stats.sending = count;
2173
- break;
2174
- case 'failed':
2175
- stats.failed = count;
2176
- break;
2177
- case 'acked':
2178
- stats.acked = count;
2179
- break;
2180
- }
2181
- stats.total += count;
2182
- }
2183
-
2184
- this.updateState({ pendingCount: stats.pending + stats.failed });
2185
- if (options?.emit !== false) {
2186
- this.emit('outbox:change', {
2187
- pendingCount: stats.pending,
2188
- sendingCount: stats.sending,
2189
- failedCount: stats.failed,
2190
- ackedCount: stats.acked,
2191
- });
2192
- }
2193
-
2194
- return stats;
2195
- }
2196
-
2197
- /**
2198
- * Get pending conflicts with operation details from outbox
2199
- */
2200
- async getConflicts(): Promise<ConflictInfo[]> {
2201
- // Join with outbox to get operation details
2202
- const res = await sql<{
2203
- id: string;
2204
- outbox_commit_id: string;
2205
- client_commit_id: string;
2206
- op_index: number;
2207
- result_status: ConflictResultStatus;
2208
- message: string;
2209
- code: string | null;
2210
- server_version: number | null;
2211
- server_row_json: string | null;
2212
- created_at: number;
2213
- operations_json: string;
2214
- }>`
2215
- select
2216
- ${sql.ref('c.id')},
2217
- ${sql.ref('c.outbox_commit_id')},
2218
- ${sql.ref('c.client_commit_id')},
2219
- ${sql.ref('c.op_index')},
2220
- ${sql.ref('c.result_status')},
2221
- ${sql.ref('c.message')},
2222
- ${sql.ref('c.code')},
2223
- ${sql.ref('c.server_version')},
2224
- ${sql.ref('c.server_row_json')},
2225
- ${sql.ref('c.created_at')},
2226
- ${sql.ref('oc.operations_json')}
2227
- from ${sql.table('sync_conflicts')} as ${sql.ref('c')}
2228
- inner join ${sql.table('sync_outbox_commits')} as ${sql.ref('oc')}
2229
- on ${sql.ref('oc.id')} = ${sql.ref('c.outbox_commit_id')}
2230
- where ${sql.ref('c.resolved_at')} is null
2231
- order by ${sql.ref('c.created_at')} desc
2232
- `.execute(this.config.db);
2233
- const rows = res.rows;
2234
-
2235
- return rows.map((row) => {
2236
- // Extract operation details from outbox
2237
- let table = '';
2238
- let rowId = '';
2239
- let localPayload: Record<string, unknown> | null = null;
2240
-
2241
- if (row.operations_json) {
2242
- try {
2243
- const operations: unknown = JSON.parse(row.operations_json);
2244
- if (Array.isArray(operations)) {
2245
- const op = operations[row.op_index];
2246
- if (isRecord(op)) {
2247
- if (typeof op.table === 'string') table = op.table;
2248
- if (typeof op.row_id === 'string') rowId = op.row_id;
2249
- localPayload =
2250
- op.payload === null
2251
- ? null
2252
- : isRecord(op.payload)
2253
- ? op.payload
2254
- : null;
2255
- }
2256
- }
2257
- } catch {
2258
- // Ignore parse errors
2259
- }
2260
- }
2261
-
2262
- return {
2263
- id: row.id,
2264
- outboxCommitId: row.outbox_commit_id,
2265
- clientCommitId: row.client_commit_id,
2266
- opIndex: row.op_index,
2267
- resultStatus: row.result_status,
2268
- message: row.message,
2269
- code: row.code,
2270
- serverVersion: row.server_version,
2271
- serverRowJson: row.server_row_json,
2272
- createdAt: row.created_at,
2273
- table,
2274
- rowId,
2275
- localPayload,
2276
- };
2277
- });
2278
- }
2279
-
2280
- /**
2281
- * Update subscriptions dynamically
2282
- */
2283
- updateSubscriptions(
2284
- subscriptions: Array<Omit<SyncSubscriptionRequest, 'cursor'>>
2285
- ): void {
2286
- this.config.subscriptions = subscriptions;
2287
- // Trigger a sync to apply new subscriptions
2288
- this.triggerSyncInBackground(undefined, 'subscription update');
2289
- }
2290
-
2291
- /**
2292
- * Apply local mutations immediately to the database and emit change events.
2293
- * Used for instant UI updates before the sync cycle completes.
2294
- */
2295
- async applyLocalMutation(
2296
- inputs: Array<{
2297
- table: string;
2298
- rowId: string;
2299
- op: 'upsert' | 'delete';
2300
- payload?: Record<string, unknown> | null;
2301
- }>
2302
- ): Promise<void> {
2303
- const db = this.config.db;
2304
- const handlers = this.config.handlers;
2305
- const affectedTables = new Set<string>();
2306
- const now = Date.now();
2307
-
2308
- await db.transaction().execute(async (trx) => {
2309
- for (const input of inputs) {
2310
- const handler = getClientHandler(handlers, input.table);
2311
- if (!handler) continue;
2312
-
2313
- affectedTables.add(input.table);
2314
-
2315
- const change: SyncChange = {
2316
- table: input.table,
2317
- row_id: input.rowId,
2318
- op: input.op,
2319
- scopes: {},
2320
- // For delete ops, row_json should be null; for upserts, default to empty object
2321
- row_json: input.op === 'delete' ? null : (input.payload ?? {}),
2322
- // null indicates local optimistic change (no server version yet)
2323
- row_version: null,
2324
- };
2325
-
2326
- await handler.applyChange({ trx }, change);
2327
- }
2328
- });
2329
-
2330
- // Track mutation timestamps for fingerprint-based rerender optimization (in-memory only)
2331
- this.recordLocalMutations(
2332
- inputs
2333
- .filter((i) => affectedTables.has(i.table))
2334
- .map((i) => ({ table: i.table, rowId: i.rowId, op: i.op })),
2335
- now
2336
- );
2337
- }
2338
-
2339
- /**
2340
- * Clear failed commits from the outbox.
2341
- * Use this to discard commits that keep failing (e.g., version conflicts).
2342
- */
2343
- async clearFailedCommits(): Promise<number> {
2344
- const db = this.config.db;
2345
-
2346
- const res = await sql`
2347
- delete from ${sql.table('sync_outbox_commits')}
2348
- where ${sql.ref('status')} = ${sql.val('failed')}
2349
- `.execute(db);
2350
- const count = Number(res.numAffectedRows ?? 0);
2351
-
2352
- await this.refreshOutboxStats();
2353
- return count;
2354
- }
2355
-
2356
- /**
2357
- * Clear all pending and failed commits from the outbox.
2358
- * Use this to reset the outbox completely (e.g., for testing).
2359
- */
2360
- async clearAllCommits(): Promise<number> {
2361
- const db = this.config.db;
2362
-
2363
- const res = await sql`
2364
- delete from ${sql.table('sync_outbox_commits')}
2365
- where ${sql.ref('status')} in (${sql.join([
2366
- sql.val('pending'),
2367
- sql.val('failed'),
2368
- ])})
2369
- `.execute(db);
2370
- const count = Number(res.numAffectedRows ?? 0);
2371
-
2372
- await this.refreshOutboxStats();
2373
- return count;
2374
- }
2375
- }