@syncular/client 0.0.1-60
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.
- package/dist/blobs/index.d.ts +7 -0
- package/dist/blobs/index.d.ts.map +1 -0
- package/dist/blobs/index.js +7 -0
- package/dist/blobs/index.js.map +1 -0
- package/dist/blobs/manager.d.ts +345 -0
- package/dist/blobs/manager.d.ts.map +1 -0
- package/dist/blobs/manager.js +749 -0
- package/dist/blobs/manager.js.map +1 -0
- package/dist/blobs/migrate.d.ts +14 -0
- package/dist/blobs/migrate.d.ts.map +1 -0
- package/dist/blobs/migrate.js +59 -0
- package/dist/blobs/migrate.js.map +1 -0
- package/dist/blobs/types.d.ts +62 -0
- package/dist/blobs/types.d.ts.map +1 -0
- package/dist/blobs/types.js +5 -0
- package/dist/blobs/types.js.map +1 -0
- package/dist/client.d.ts +338 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +834 -0
- package/dist/client.js.map +1 -0
- package/dist/conflicts.d.ts +31 -0
- package/dist/conflicts.d.ts.map +1 -0
- package/dist/conflicts.js +118 -0
- package/dist/conflicts.js.map +1 -0
- package/dist/create-client.d.ts +115 -0
- package/dist/create-client.d.ts.map +1 -0
- package/dist/create-client.js +162 -0
- package/dist/create-client.js.map +1 -0
- package/dist/engine/SyncEngine.d.ts +215 -0
- package/dist/engine/SyncEngine.d.ts.map +1 -0
- package/dist/engine/SyncEngine.js +1066 -0
- package/dist/engine/SyncEngine.js.map +1 -0
- package/dist/engine/index.d.ts +6 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +6 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/types.d.ts +230 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +7 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/handlers/create-handler.d.ts +110 -0
- package/dist/handlers/create-handler.d.ts.map +1 -0
- package/dist/handlers/create-handler.js +140 -0
- package/dist/handlers/create-handler.js.map +1 -0
- package/dist/handlers/registry.d.ts +15 -0
- package/dist/handlers/registry.d.ts.map +1 -0
- package/dist/handlers/registry.js +29 -0
- package/dist/handlers/registry.js.map +1 -0
- package/dist/handlers/types.d.ts +83 -0
- package/dist/handlers/types.d.ts.map +1 -0
- package/dist/handlers/types.js +5 -0
- package/dist/handlers/types.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate.d.ts +19 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +106 -0
- package/dist/migrate.js.map +1 -0
- package/dist/mutations.d.ts +138 -0
- package/dist/mutations.d.ts.map +1 -0
- package/dist/mutations.js +611 -0
- package/dist/mutations.js.map +1 -0
- package/dist/outbox.d.ts +112 -0
- package/dist/outbox.d.ts.map +1 -0
- package/dist/outbox.js +304 -0
- package/dist/outbox.js.map +1 -0
- package/dist/plugins/incrementing-version.d.ts +34 -0
- package/dist/plugins/incrementing-version.d.ts.map +1 -0
- package/dist/plugins/incrementing-version.js +83 -0
- package/dist/plugins/incrementing-version.js.map +1 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/types.d.ts +49 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/plugins/types.js +15 -0
- package/dist/plugins/types.js.map +1 -0
- package/dist/proxy/connection.d.ts +33 -0
- package/dist/proxy/connection.d.ts.map +1 -0
- package/dist/proxy/connection.js +153 -0
- package/dist/proxy/connection.js.map +1 -0
- package/dist/proxy/dialect.d.ts +46 -0
- package/dist/proxy/dialect.d.ts.map +1 -0
- package/dist/proxy/dialect.js +58 -0
- package/dist/proxy/dialect.js.map +1 -0
- package/dist/proxy/driver.d.ts +42 -0
- package/dist/proxy/driver.d.ts.map +1 -0
- package/dist/proxy/driver.js +78 -0
- package/dist/proxy/driver.js.map +1 -0
- package/dist/proxy/index.d.ts +10 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/index.js +10 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/proxy/mutations.d.ts +9 -0
- package/dist/proxy/mutations.d.ts.map +1 -0
- package/dist/proxy/mutations.js +11 -0
- package/dist/proxy/mutations.js.map +1 -0
- package/dist/pull-engine.d.ts +45 -0
- package/dist/pull-engine.d.ts.map +1 -0
- package/dist/pull-engine.js +391 -0
- package/dist/pull-engine.js.map +1 -0
- package/dist/push-engine.d.ts +18 -0
- package/dist/push-engine.d.ts.map +1 -0
- package/dist/push-engine.js +155 -0
- package/dist/push-engine.js.map +1 -0
- package/dist/query/FingerprintCollector.d.ts +18 -0
- package/dist/query/FingerprintCollector.d.ts.map +1 -0
- package/dist/query/FingerprintCollector.js +28 -0
- package/dist/query/FingerprintCollector.js.map +1 -0
- package/dist/query/QueryContext.d.ts +33 -0
- package/dist/query/QueryContext.d.ts.map +1 -0
- package/dist/query/QueryContext.js +16 -0
- package/dist/query/QueryContext.js.map +1 -0
- package/dist/query/fingerprint.d.ts +61 -0
- package/dist/query/fingerprint.d.ts.map +1 -0
- package/dist/query/fingerprint.js +91 -0
- package/dist/query/fingerprint.js.map +1 -0
- package/dist/query/index.d.ts +7 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +7 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/tracked-select.d.ts +18 -0
- package/dist/query/tracked-select.d.ts.map +1 -0
- package/dist/query/tracked-select.js +90 -0
- package/dist/query/tracked-select.js.map +1 -0
- package/dist/schema.d.ts +83 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +7 -0
- package/dist/schema.js.map +1 -0
- package/dist/sync-loop.d.ts +32 -0
- package/dist/sync-loop.d.ts.map +1 -0
- package/dist/sync-loop.js +249 -0
- package/dist/sync-loop.js.map +1 -0
- package/dist/utils/id.d.ts +8 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/id.js +19 -0
- package/dist/utils/id.js.map +1 -0
- package/package.json +58 -0
- package/src/blobs/index.ts +7 -0
- package/src/blobs/manager.ts +1027 -0
- package/src/blobs/migrate.ts +67 -0
- package/src/blobs/types.ts +84 -0
- package/src/client.ts +1222 -0
- package/src/conflicts.ts +180 -0
- package/src/create-client.ts +297 -0
- package/src/engine/SyncEngine.ts +1337 -0
- package/src/engine/index.ts +6 -0
- package/src/engine/types.ts +268 -0
- package/src/handlers/create-handler.ts +287 -0
- package/src/handlers/registry.ts +36 -0
- package/src/handlers/types.ts +102 -0
- package/src/index.ts +25 -0
- package/src/migrate.ts +122 -0
- package/src/mutations.ts +926 -0
- package/src/outbox.ts +397 -0
- package/src/plugins/incrementing-version.ts +133 -0
- package/src/plugins/index.ts +2 -0
- package/src/plugins/types.ts +63 -0
- package/src/proxy/connection.ts +191 -0
- package/src/proxy/dialect.ts +76 -0
- package/src/proxy/driver.ts +126 -0
- package/src/proxy/index.ts +10 -0
- package/src/proxy/mutations.ts +18 -0
- package/src/pull-engine.ts +518 -0
- package/src/push-engine.ts +201 -0
- package/src/query/FingerprintCollector.ts +29 -0
- package/src/query/QueryContext.ts +54 -0
- package/src/query/fingerprint.ts +109 -0
- package/src/query/index.ts +10 -0
- package/src/query/tracked-select.ts +139 -0
- package/src/schema.ts +94 -0
- package/src/sync-loop.ts +368 -0
- package/src/utils/id.ts +20 -0
|
@@ -0,0 +1,1066 @@
|
|
|
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
|
+
import { sql } from 'kysely';
|
|
8
|
+
import { syncPushOnce } from '../push-engine';
|
|
9
|
+
import { syncOnce } from '../sync-loop';
|
|
10
|
+
const DEFAULT_POLL_INTERVAL_MS = 10_000;
|
|
11
|
+
const DEFAULT_MAX_RETRIES = 5;
|
|
12
|
+
const INITIAL_RETRY_DELAY_MS = 1000;
|
|
13
|
+
const MAX_RETRY_DELAY_MS = 60000;
|
|
14
|
+
const EXPONENTIAL_FACTOR = 2;
|
|
15
|
+
const REALTIME_RECONNECT_CATCHUP_DELAY_MS = 500;
|
|
16
|
+
function calculateRetryDelay(attemptIndex) {
|
|
17
|
+
return Math.min(INITIAL_RETRY_DELAY_MS * EXPONENTIAL_FACTOR ** attemptIndex, MAX_RETRY_DELAY_MS);
|
|
18
|
+
}
|
|
19
|
+
function isRealtimeTransport(transport) {
|
|
20
|
+
return (typeof transport === 'object' &&
|
|
21
|
+
transport !== null &&
|
|
22
|
+
typeof transport.connect === 'function');
|
|
23
|
+
}
|
|
24
|
+
function createSyncError(code, message, cause) {
|
|
25
|
+
return {
|
|
26
|
+
code,
|
|
27
|
+
message,
|
|
28
|
+
cause,
|
|
29
|
+
timestamp: Date.now(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function isRecord(value) {
|
|
33
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
export class SyncEngine {
|
|
36
|
+
config;
|
|
37
|
+
state;
|
|
38
|
+
listeners;
|
|
39
|
+
pollerId = null;
|
|
40
|
+
fallbackPollerId = null;
|
|
41
|
+
realtimeDisconnect = null;
|
|
42
|
+
realtimePresenceUnsub = null;
|
|
43
|
+
isDestroyed = false;
|
|
44
|
+
migrated = false;
|
|
45
|
+
syncPromise = null;
|
|
46
|
+
syncRequestedWhileRunning = false;
|
|
47
|
+
retryTimeoutId = null;
|
|
48
|
+
realtimeCatchupTimeoutId = null;
|
|
49
|
+
hasRealtimeConnectedOnce = false;
|
|
50
|
+
/**
|
|
51
|
+
* In-memory map tracking local mutation timestamps by rowId.
|
|
52
|
+
* Used for efficient fingerprint-based rerender optimization.
|
|
53
|
+
* Key format: `${table}:${rowId}`, Value: timestamp (Date.now())
|
|
54
|
+
*/
|
|
55
|
+
mutationTimestamps = new Map();
|
|
56
|
+
/**
|
|
57
|
+
* In-memory map tracking table-level mutation timestamps.
|
|
58
|
+
* Used for coarse invalidation during large bootstrap snapshots to avoid
|
|
59
|
+
* storing timestamps for every row.
|
|
60
|
+
*/
|
|
61
|
+
tableMutationTimestamps = new Map();
|
|
62
|
+
/**
|
|
63
|
+
* In-memory presence state by scope key.
|
|
64
|
+
* Updated via realtime presence events.
|
|
65
|
+
*/
|
|
66
|
+
presenceByScopeKey = new Map();
|
|
67
|
+
constructor(config) {
|
|
68
|
+
this.config = config;
|
|
69
|
+
this.listeners = new Map();
|
|
70
|
+
this.state = this.createInitialState();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get mutation timestamp for a row (used by query hooks for fingerprinting).
|
|
74
|
+
* Returns 0 if row has no recorded mutation timestamp.
|
|
75
|
+
*/
|
|
76
|
+
getMutationTimestamp(table, rowId) {
|
|
77
|
+
const rowTs = this.mutationTimestamps.get(`${table}:${rowId}`) ?? 0;
|
|
78
|
+
const tableTs = this.tableMutationTimestamps.get(table) ?? 0;
|
|
79
|
+
return Math.max(rowTs, tableTs);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get presence entries for a scope key.
|
|
83
|
+
* Returns empty array if no presence data for the scope.
|
|
84
|
+
*/
|
|
85
|
+
getPresence(scopeKey) {
|
|
86
|
+
return (this.presenceByScopeKey.get(scopeKey) ??
|
|
87
|
+
[]);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Update presence for a scope key (called by realtime transport).
|
|
91
|
+
* Emits presence:change event for listeners.
|
|
92
|
+
*/
|
|
93
|
+
updatePresence(scopeKey, presence) {
|
|
94
|
+
this.presenceByScopeKey.set(scopeKey, presence);
|
|
95
|
+
this.emit('presence:change', { scopeKey, presence });
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Join presence for a scope key.
|
|
99
|
+
* Sends via transport (if available) and updates local state optimistically.
|
|
100
|
+
*/
|
|
101
|
+
joinPresence(scopeKey, metadata) {
|
|
102
|
+
if (isRealtimeTransport(this.config.transport)) {
|
|
103
|
+
const transport = this.config.transport;
|
|
104
|
+
transport.sendPresenceJoin?.(scopeKey, metadata);
|
|
105
|
+
}
|
|
106
|
+
// Optimistic local update
|
|
107
|
+
this.handlePresenceEvent({
|
|
108
|
+
action: 'join',
|
|
109
|
+
scopeKey,
|
|
110
|
+
clientId: this.config.clientId,
|
|
111
|
+
actorId: this.config.actorId,
|
|
112
|
+
metadata,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Leave presence for a scope key.
|
|
117
|
+
*/
|
|
118
|
+
leavePresence(scopeKey) {
|
|
119
|
+
if (isRealtimeTransport(this.config.transport)) {
|
|
120
|
+
const transport = this.config.transport;
|
|
121
|
+
transport.sendPresenceLeave?.(scopeKey);
|
|
122
|
+
}
|
|
123
|
+
this.handlePresenceEvent({
|
|
124
|
+
action: 'leave',
|
|
125
|
+
scopeKey,
|
|
126
|
+
clientId: this.config.clientId,
|
|
127
|
+
actorId: this.config.actorId,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Update presence metadata for a scope key.
|
|
132
|
+
*/
|
|
133
|
+
updatePresenceMetadata(scopeKey, metadata) {
|
|
134
|
+
if (isRealtimeTransport(this.config.transport)) {
|
|
135
|
+
const transport = this.config.transport;
|
|
136
|
+
transport.sendPresenceUpdate?.(scopeKey, metadata);
|
|
137
|
+
}
|
|
138
|
+
this.handlePresenceEvent({
|
|
139
|
+
action: 'update',
|
|
140
|
+
scopeKey,
|
|
141
|
+
clientId: this.config.clientId,
|
|
142
|
+
actorId: this.config.actorId,
|
|
143
|
+
metadata,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Handle a single presence event (join/leave/update).
|
|
148
|
+
* Updates the in-memory presence state and emits change event.
|
|
149
|
+
*/
|
|
150
|
+
handlePresenceEvent(event) {
|
|
151
|
+
const current = this.presenceByScopeKey.get(event.scopeKey) ?? [];
|
|
152
|
+
let updated;
|
|
153
|
+
switch (event.action) {
|
|
154
|
+
case 'join':
|
|
155
|
+
// Add new entry (remove existing if present to update)
|
|
156
|
+
updated = [
|
|
157
|
+
...current.filter((e) => e.clientId !== event.clientId),
|
|
158
|
+
{
|
|
159
|
+
clientId: event.clientId,
|
|
160
|
+
actorId: event.actorId,
|
|
161
|
+
joinedAt: Date.now(),
|
|
162
|
+
metadata: event.metadata,
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
break;
|
|
166
|
+
case 'leave':
|
|
167
|
+
updated = current.filter((e) => e.clientId !== event.clientId);
|
|
168
|
+
break;
|
|
169
|
+
case 'update':
|
|
170
|
+
updated = current.map((e) => e.clientId === event.clientId ? { ...e, metadata: event.metadata } : e);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
this.presenceByScopeKey.set(event.scopeKey, updated);
|
|
174
|
+
this.emit('presence:change', {
|
|
175
|
+
scopeKey: event.scopeKey,
|
|
176
|
+
presence: updated,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
createInitialState() {
|
|
180
|
+
const enabled = this.isEnabled();
|
|
181
|
+
return {
|
|
182
|
+
enabled,
|
|
183
|
+
isSyncing: false,
|
|
184
|
+
connectionState: enabled ? 'disconnected' : 'disconnected',
|
|
185
|
+
transportMode: this.detectTransportMode(),
|
|
186
|
+
lastSyncAt: null,
|
|
187
|
+
error: null,
|
|
188
|
+
pendingCount: 0,
|
|
189
|
+
retryCount: 0,
|
|
190
|
+
isRetrying: false,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
isEnabled() {
|
|
194
|
+
const { actorId, clientId } = this.config;
|
|
195
|
+
return (typeof actorId === 'string' &&
|
|
196
|
+
actorId.length > 0 &&
|
|
197
|
+
typeof clientId === 'string' &&
|
|
198
|
+
clientId.length > 0);
|
|
199
|
+
}
|
|
200
|
+
detectTransportMode() {
|
|
201
|
+
if (this.config.realtimeEnabled !== false &&
|
|
202
|
+
isRealtimeTransport(this.config.transport)) {
|
|
203
|
+
return 'realtime';
|
|
204
|
+
}
|
|
205
|
+
return 'polling';
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Get current engine state.
|
|
209
|
+
* Returns the same object reference to avoid useSyncExternalStore infinite loops.
|
|
210
|
+
*/
|
|
211
|
+
getState() {
|
|
212
|
+
return this.state;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get database instance
|
|
216
|
+
*/
|
|
217
|
+
getDb() {
|
|
218
|
+
return this.config.db;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get current actor id (sync scoping).
|
|
222
|
+
*/
|
|
223
|
+
getActorId() {
|
|
224
|
+
return this.config.actorId;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get current client id (device/app install id).
|
|
228
|
+
*/
|
|
229
|
+
getClientId() {
|
|
230
|
+
return this.config.clientId;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Subscribe to sync events
|
|
234
|
+
*/
|
|
235
|
+
on(event, listener) {
|
|
236
|
+
if (!this.listeners.has(event)) {
|
|
237
|
+
this.listeners.set(event, new Set());
|
|
238
|
+
}
|
|
239
|
+
const wrapped = (payload) => {
|
|
240
|
+
listener(payload);
|
|
241
|
+
};
|
|
242
|
+
this.listeners.get(event).add(wrapped);
|
|
243
|
+
return () => {
|
|
244
|
+
this.listeners.get(event)?.delete(wrapped);
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Subscribe to any state change (for useSyncExternalStore)
|
|
249
|
+
*/
|
|
250
|
+
subscribe(callback) {
|
|
251
|
+
// Subscribe to state:change which is emitted by updateState()
|
|
252
|
+
return this.on('state:change', callback);
|
|
253
|
+
}
|
|
254
|
+
emit(event, payload) {
|
|
255
|
+
const eventListeners = this.listeners.get(event);
|
|
256
|
+
if (eventListeners) {
|
|
257
|
+
for (const listener of eventListeners) {
|
|
258
|
+
try {
|
|
259
|
+
listener(payload);
|
|
260
|
+
}
|
|
261
|
+
catch (err) {
|
|
262
|
+
console.error(`[SyncEngine] Error in ${event} listener:`, err);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
updateState(partial) {
|
|
268
|
+
this.state = { ...this.state, ...partial };
|
|
269
|
+
// Emit state:change to notify useSyncExternalStore subscribers
|
|
270
|
+
this.emit('state:change', {});
|
|
271
|
+
}
|
|
272
|
+
setConnectionState(state) {
|
|
273
|
+
const previous = this.state.connectionState;
|
|
274
|
+
if (previous !== state) {
|
|
275
|
+
this.updateState({ connectionState: state });
|
|
276
|
+
this.emit('connection:change', { previous, current: state });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Start the sync engine
|
|
281
|
+
*/
|
|
282
|
+
async start() {
|
|
283
|
+
if (this.isDestroyed) {
|
|
284
|
+
throw new Error('SyncEngine has been destroyed');
|
|
285
|
+
}
|
|
286
|
+
if (!this.isEnabled()) {
|
|
287
|
+
this.updateState({ enabled: false });
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
this.updateState({ enabled: true });
|
|
291
|
+
// Run migration if provided
|
|
292
|
+
if (this.config.migrate && !this.migrated) {
|
|
293
|
+
// Best-effort: push any pending outbox commits before migration
|
|
294
|
+
// (migration may reset the DB, so we try to save unsynced changes)
|
|
295
|
+
try {
|
|
296
|
+
const hasOutbox = await sql `
|
|
297
|
+
select 1 from ${sql.table('sync_outbox_commits')} limit 1
|
|
298
|
+
`
|
|
299
|
+
.execute(this.config.db)
|
|
300
|
+
.then((r) => r.rows.length > 0)
|
|
301
|
+
.catch(() => false);
|
|
302
|
+
if (hasOutbox) {
|
|
303
|
+
// Push all pending commits (best effort)
|
|
304
|
+
let pushed = true;
|
|
305
|
+
while (pushed) {
|
|
306
|
+
const result = await syncPushOnce(this.config.db, this.config.transport, {
|
|
307
|
+
clientId: this.config.clientId,
|
|
308
|
+
actorId: this.config.actorId ?? undefined,
|
|
309
|
+
plugins: this.config.plugins,
|
|
310
|
+
});
|
|
311
|
+
pushed = result.pushed;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
// Best-effort: if push fails (network down, table missing), continue
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
await this.config.migrate(this.config.db);
|
|
320
|
+
this.migrated = true;
|
|
321
|
+
}
|
|
322
|
+
catch (err) {
|
|
323
|
+
const migrationError = err instanceof Error ? err : new Error(String(err));
|
|
324
|
+
this.config.onMigrationError?.(migrationError);
|
|
325
|
+
const error = createSyncError('SYNC_ERROR', 'Migration failed', migrationError);
|
|
326
|
+
this.handleError(error);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// Setup transport-specific handling
|
|
331
|
+
if (this.state.transportMode === 'realtime') {
|
|
332
|
+
this.setupRealtime();
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
this.setupPolling();
|
|
336
|
+
}
|
|
337
|
+
// Initial sync
|
|
338
|
+
await this.sync();
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Stop the sync engine (cleanup without destroy)
|
|
342
|
+
*/
|
|
343
|
+
stop() {
|
|
344
|
+
this.stopPolling();
|
|
345
|
+
this.stopRealtime();
|
|
346
|
+
this.setConnectionState('disconnected');
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Destroy the engine (cannot be restarted)
|
|
350
|
+
*/
|
|
351
|
+
destroy() {
|
|
352
|
+
this.stop();
|
|
353
|
+
this.listeners.clear();
|
|
354
|
+
this.isDestroyed = true;
|
|
355
|
+
if (this.retryTimeoutId) {
|
|
356
|
+
clearTimeout(this.retryTimeoutId);
|
|
357
|
+
this.retryTimeoutId = null;
|
|
358
|
+
}
|
|
359
|
+
if (this.realtimeCatchupTimeoutId) {
|
|
360
|
+
clearTimeout(this.realtimeCatchupTimeoutId);
|
|
361
|
+
this.realtimeCatchupTimeoutId = null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Trigger a manual sync
|
|
366
|
+
*/
|
|
367
|
+
async sync(opts) {
|
|
368
|
+
// Dedupe concurrent sync calls
|
|
369
|
+
if (this.syncPromise) {
|
|
370
|
+
// A sync is already in-flight; queue one more run so we don't miss
|
|
371
|
+
// mutations enqueued during the current cycle (important in realtime mode).
|
|
372
|
+
this.syncRequestedWhileRunning = true;
|
|
373
|
+
return this.syncPromise;
|
|
374
|
+
}
|
|
375
|
+
if (!this.isEnabled() ||
|
|
376
|
+
this.isDestroyed ||
|
|
377
|
+
this.state.connectionState === 'disconnected') {
|
|
378
|
+
return {
|
|
379
|
+
success: false,
|
|
380
|
+
pushedCommits: 0,
|
|
381
|
+
pullRounds: 0,
|
|
382
|
+
pullResponse: { ok: true, subscriptions: [] },
|
|
383
|
+
error: createSyncError('SYNC_ERROR', 'Sync not enabled'),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
this.syncPromise = this.performSyncLoop(opts?.trigger);
|
|
387
|
+
try {
|
|
388
|
+
return await this.syncPromise;
|
|
389
|
+
}
|
|
390
|
+
finally {
|
|
391
|
+
this.syncPromise = null;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
async performSyncLoop(trigger) {
|
|
395
|
+
let lastResult = {
|
|
396
|
+
success: false,
|
|
397
|
+
pushedCommits: 0,
|
|
398
|
+
pullRounds: 0,
|
|
399
|
+
pullResponse: { ok: true, subscriptions: [] },
|
|
400
|
+
error: createSyncError('SYNC_ERROR', 'Sync not started'),
|
|
401
|
+
};
|
|
402
|
+
do {
|
|
403
|
+
this.syncRequestedWhileRunning = false;
|
|
404
|
+
lastResult = await this.performSyncOnce(trigger);
|
|
405
|
+
// After the first iteration, clear trigger context
|
|
406
|
+
trigger = undefined;
|
|
407
|
+
// If the sync failed, let retry logic handle backoff instead of tight looping.
|
|
408
|
+
if (!lastResult.success)
|
|
409
|
+
break;
|
|
410
|
+
} while (this.syncRequestedWhileRunning &&
|
|
411
|
+
!this.isDestroyed &&
|
|
412
|
+
this.isEnabled());
|
|
413
|
+
return lastResult;
|
|
414
|
+
}
|
|
415
|
+
async performSyncOnce(trigger) {
|
|
416
|
+
const timestamp = Date.now();
|
|
417
|
+
this.updateState({ isSyncing: true });
|
|
418
|
+
this.emit('sync:start', { timestamp });
|
|
419
|
+
try {
|
|
420
|
+
const pullApplyTimestamp = Date.now();
|
|
421
|
+
const result = await syncOnce(this.config.db, this.config.transport, this.config.shapes, {
|
|
422
|
+
clientId: this.config.clientId,
|
|
423
|
+
actorId: this.config.actorId ?? undefined,
|
|
424
|
+
plugins: this.config.plugins,
|
|
425
|
+
subscriptions: this.config.subscriptions,
|
|
426
|
+
limitCommits: this.config.limitCommits,
|
|
427
|
+
limitSnapshotRows: this.config.limitSnapshotRows,
|
|
428
|
+
maxSnapshotPages: this.config.maxSnapshotPages,
|
|
429
|
+
stateId: this.config.stateId,
|
|
430
|
+
trigger,
|
|
431
|
+
});
|
|
432
|
+
const syncResult = {
|
|
433
|
+
success: true,
|
|
434
|
+
pushedCommits: result.pushedCommits,
|
|
435
|
+
pullRounds: result.pullRounds,
|
|
436
|
+
pullResponse: result.pullResponse,
|
|
437
|
+
};
|
|
438
|
+
// Update fingerprint mutation timestamps for server-applied changes so wa-sqlite
|
|
439
|
+
// query hooks rerender on remote changes (not just local mutations).
|
|
440
|
+
this.recordMutationTimestampsFromPullResponse(result.pullResponse, pullApplyTimestamp);
|
|
441
|
+
this.updateState({
|
|
442
|
+
isSyncing: false,
|
|
443
|
+
lastSyncAt: Date.now(),
|
|
444
|
+
error: null,
|
|
445
|
+
retryCount: 0,
|
|
446
|
+
isRetrying: false,
|
|
447
|
+
});
|
|
448
|
+
this.emit('sync:complete', {
|
|
449
|
+
timestamp: Date.now(),
|
|
450
|
+
pushedCommits: result.pushedCommits,
|
|
451
|
+
pullRounds: result.pullRounds,
|
|
452
|
+
pullResponse: result.pullResponse,
|
|
453
|
+
});
|
|
454
|
+
// Emit data change for any tables that had changes
|
|
455
|
+
const changedTables = this.extractChangedTables(result.pullResponse);
|
|
456
|
+
if (changedTables.length > 0) {
|
|
457
|
+
this.emit('data:change', {
|
|
458
|
+
scopes: changedTables,
|
|
459
|
+
timestamp: Date.now(),
|
|
460
|
+
});
|
|
461
|
+
this.config.onDataChange?.(changedTables);
|
|
462
|
+
}
|
|
463
|
+
// Refresh outbox stats (fire-and-forget — don't block sync:complete)
|
|
464
|
+
this.refreshOutboxStats().catch(() => { });
|
|
465
|
+
return syncResult;
|
|
466
|
+
}
|
|
467
|
+
catch (err) {
|
|
468
|
+
const error = createSyncError('SYNC_ERROR', err instanceof Error ? err.message : 'Sync failed', err instanceof Error ? err : undefined);
|
|
469
|
+
this.updateState({
|
|
470
|
+
isSyncing: false,
|
|
471
|
+
error,
|
|
472
|
+
retryCount: this.state.retryCount + 1,
|
|
473
|
+
isRetrying: false,
|
|
474
|
+
});
|
|
475
|
+
this.handleError(error);
|
|
476
|
+
// Schedule retry if under max retries
|
|
477
|
+
const maxRetries = this.config.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
478
|
+
if (this.state.retryCount < maxRetries) {
|
|
479
|
+
this.scheduleRetry();
|
|
480
|
+
}
|
|
481
|
+
return {
|
|
482
|
+
success: false,
|
|
483
|
+
pushedCommits: 0,
|
|
484
|
+
pullRounds: 0,
|
|
485
|
+
pullResponse: { ok: true, subscriptions: [] },
|
|
486
|
+
error,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
extractChangedTables(response) {
|
|
491
|
+
const tables = new Set();
|
|
492
|
+
for (const sub of response.subscriptions ?? []) {
|
|
493
|
+
// Extract tables from snapshots
|
|
494
|
+
for (const snapshot of sub.snapshots ?? []) {
|
|
495
|
+
if (snapshot.table) {
|
|
496
|
+
tables.add(snapshot.table);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
// Extract tables from commits
|
|
500
|
+
for (const commit of sub.commits ?? []) {
|
|
501
|
+
for (const change of commit.changes ?? []) {
|
|
502
|
+
if (change.table) {
|
|
503
|
+
tables.add(change.table);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return Array.from(tables);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Apply changes delivered inline over WebSocket for instant UI updates.
|
|
512
|
+
* Returns true if changes were applied and cursor updated successfully,
|
|
513
|
+
* false if anything failed (caller should fall back to HTTP sync).
|
|
514
|
+
*/
|
|
515
|
+
async applyWsDeliveredChanges(changes, cursor) {
|
|
516
|
+
try {
|
|
517
|
+
await this.config.db.transaction().execute(async (trx) => {
|
|
518
|
+
for (const change of changes) {
|
|
519
|
+
try {
|
|
520
|
+
const handler = this.config.shapes.get(change.table);
|
|
521
|
+
if (!handler)
|
|
522
|
+
continue;
|
|
523
|
+
await handler.applyChange({ trx }, change);
|
|
524
|
+
}
|
|
525
|
+
catch {
|
|
526
|
+
// Best-effort: individual change failures are fine
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
// Update subscription cursors
|
|
530
|
+
const stateId = this.config.stateId ?? 'default';
|
|
531
|
+
await sql `
|
|
532
|
+
update ${sql.table('sync_subscription_state')}
|
|
533
|
+
set ${sql.ref('cursor')} = ${sql.val(cursor)}
|
|
534
|
+
where ${sql.ref('state_id')} = ${sql.val(stateId)}
|
|
535
|
+
and ${sql.ref('cursor')} < ${sql.val(cursor)}
|
|
536
|
+
`.execute(trx);
|
|
537
|
+
});
|
|
538
|
+
// Update mutation timestamps BEFORE emitting data:change so that
|
|
539
|
+
// React hooks re-querying the DB see fresh fingerprints immediately.
|
|
540
|
+
const now = Date.now();
|
|
541
|
+
for (const change of changes) {
|
|
542
|
+
if (!change.table || !change.row_id)
|
|
543
|
+
continue;
|
|
544
|
+
if (change.op === 'delete') {
|
|
545
|
+
this.mutationTimestamps.delete(`${change.table}:${change.row_id}`);
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
this.bumpMutationTimestamp(change.table, change.row_id, now);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
// Emit data change for immediate UI update
|
|
552
|
+
const changedTables = [...new Set(changes.map((c) => c.table))];
|
|
553
|
+
if (changedTables.length > 0) {
|
|
554
|
+
this.emit('data:change', {
|
|
555
|
+
scopes: changedTables,
|
|
556
|
+
timestamp: Date.now(),
|
|
557
|
+
});
|
|
558
|
+
this.config.onDataChange?.(changedTables);
|
|
559
|
+
}
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Handle WS-delivered changes: apply them and decide whether to skip HTTP pull.
|
|
568
|
+
* Falls back to full HTTP sync when conditions require it.
|
|
569
|
+
*/
|
|
570
|
+
async handleWsDelivery(changes, cursor) {
|
|
571
|
+
// If a sync is already in-flight, let it handle everything
|
|
572
|
+
if (this.syncPromise) {
|
|
573
|
+
this.sync({ trigger: 'ws' });
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
// If there are pending outbox commits, need to push via HTTP
|
|
577
|
+
if (this.state.pendingCount > 0) {
|
|
578
|
+
this.sync({ trigger: 'ws' });
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
// If afterPull plugins exist, inline WS changes may require transforms
|
|
582
|
+
// (e.g. decryption). Fall back to HTTP sync and do not apply inline payload.
|
|
583
|
+
const hasAfterPullPlugins = this.config.plugins?.some((p) => typeof p.afterPull === 'function');
|
|
584
|
+
if (hasAfterPullPlugins) {
|
|
585
|
+
this.sync({ trigger: 'ws' });
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
// Apply changes + update cursor
|
|
589
|
+
const applied = await this.applyWsDeliveredChanges(changes, cursor);
|
|
590
|
+
if (!applied) {
|
|
591
|
+
this.sync({ trigger: 'ws' });
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
// All clear — skip HTTP pull entirely
|
|
595
|
+
this.updateState({
|
|
596
|
+
lastSyncAt: Date.now(),
|
|
597
|
+
error: null,
|
|
598
|
+
retryCount: 0,
|
|
599
|
+
isRetrying: false,
|
|
600
|
+
});
|
|
601
|
+
this.emit('sync:complete', {
|
|
602
|
+
timestamp: Date.now(),
|
|
603
|
+
pushedCommits: 0,
|
|
604
|
+
pullRounds: 0,
|
|
605
|
+
pullResponse: { ok: true, subscriptions: [] },
|
|
606
|
+
});
|
|
607
|
+
this.refreshOutboxStats().catch(() => { });
|
|
608
|
+
}
|
|
609
|
+
timestampCounter = 0;
|
|
610
|
+
nextPreciseTimestamp(now) {
|
|
611
|
+
// Use sub-millisecond precision by combining timestamp with atomic counter
|
|
612
|
+
// This prevents race conditions in concurrent mutations while maintaining
|
|
613
|
+
// millisecond-level compatibility with existing code.
|
|
614
|
+
return now + (this.timestampCounter++ % 1000) / 1000;
|
|
615
|
+
}
|
|
616
|
+
bumpMutationTimestamp(table, rowId, now) {
|
|
617
|
+
const key = `${table}:${rowId}`;
|
|
618
|
+
const preciseNow = this.nextPreciseTimestamp(now);
|
|
619
|
+
const prev = this.mutationTimestamps.get(key) ?? 0;
|
|
620
|
+
this.mutationTimestamps.set(key, Math.max(preciseNow, prev + 0.001));
|
|
621
|
+
}
|
|
622
|
+
bumpTableMutationTimestamp(table, now) {
|
|
623
|
+
const preciseNow = this.nextPreciseTimestamp(now);
|
|
624
|
+
const prev = this.tableMutationTimestamps.get(table) ?? 0;
|
|
625
|
+
this.tableMutationTimestamps.set(table, Math.max(preciseNow, prev + 0.001));
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Record local mutations that were already applied to the DB.
|
|
629
|
+
*
|
|
630
|
+
* This updates in-memory mutation timestamps (for fingerprint-based rerenders),
|
|
631
|
+
* and emits a single `data:change` event for the affected tables.
|
|
632
|
+
*
|
|
633
|
+
* This is intentionally separate from applyLocalMutation() so callers that
|
|
634
|
+
* perform their own DB transactions (e.g. `useMutations`) can still keep UI
|
|
635
|
+
* updates correct without double-writing.
|
|
636
|
+
*/
|
|
637
|
+
recordLocalMutations(inputs, now = Date.now()) {
|
|
638
|
+
const affectedTables = new Set();
|
|
639
|
+
for (const input of inputs) {
|
|
640
|
+
if (!input.table || !input.rowId)
|
|
641
|
+
continue;
|
|
642
|
+
affectedTables.add(input.table);
|
|
643
|
+
if (input.op === 'delete') {
|
|
644
|
+
this.mutationTimestamps.delete(`${input.table}:${input.rowId}`);
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
this.bumpMutationTimestamp(input.table, input.rowId, now);
|
|
648
|
+
}
|
|
649
|
+
if (affectedTables.size > 0) {
|
|
650
|
+
this.emit('data:change', {
|
|
651
|
+
scopes: Array.from(affectedTables),
|
|
652
|
+
timestamp: Date.now(),
|
|
653
|
+
});
|
|
654
|
+
this.config.onDataChange?.(Array.from(affectedTables));
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
recordMutationTimestampsFromPullResponse(response, now) {
|
|
658
|
+
for (const sub of response.subscriptions ?? []) {
|
|
659
|
+
// Mark snapshot tables as changed so bootstrap/resnapshot updates
|
|
660
|
+
// propagate without storing per-row timestamps for massive snapshots.
|
|
661
|
+
for (const snapshot of sub.snapshots ?? []) {
|
|
662
|
+
if (!snapshot.table)
|
|
663
|
+
continue;
|
|
664
|
+
this.bumpTableMutationTimestamp(snapshot.table, now);
|
|
665
|
+
}
|
|
666
|
+
for (const commit of sub.commits ?? []) {
|
|
667
|
+
for (const change of commit.changes ?? []) {
|
|
668
|
+
const table = change.table;
|
|
669
|
+
const rowId = change.row_id;
|
|
670
|
+
if (!table || !rowId)
|
|
671
|
+
continue;
|
|
672
|
+
if (change.op === 'delete') {
|
|
673
|
+
this.mutationTimestamps.delete(`${table}:${rowId}`);
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
this.bumpMutationTimestamp(table, rowId, now);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
scheduleRetry() {
|
|
683
|
+
if (this.retryTimeoutId) {
|
|
684
|
+
clearTimeout(this.retryTimeoutId);
|
|
685
|
+
}
|
|
686
|
+
const delay = calculateRetryDelay(this.state.retryCount);
|
|
687
|
+
this.updateState({ isRetrying: true });
|
|
688
|
+
this.retryTimeoutId = setTimeout(() => {
|
|
689
|
+
this.retryTimeoutId = null;
|
|
690
|
+
if (!this.isDestroyed) {
|
|
691
|
+
this.sync();
|
|
692
|
+
}
|
|
693
|
+
}, delay);
|
|
694
|
+
}
|
|
695
|
+
handleError(error) {
|
|
696
|
+
this.emit('sync:error', error);
|
|
697
|
+
this.config.onError?.(error);
|
|
698
|
+
}
|
|
699
|
+
setupPolling() {
|
|
700
|
+
this.stopPolling();
|
|
701
|
+
const interval = this.config.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
702
|
+
this.pollerId = setInterval(() => {
|
|
703
|
+
if (!this.state.isSyncing && !this.isDestroyed) {
|
|
704
|
+
this.sync();
|
|
705
|
+
}
|
|
706
|
+
}, interval);
|
|
707
|
+
this.setConnectionState('connected');
|
|
708
|
+
}
|
|
709
|
+
stopPolling() {
|
|
710
|
+
if (this.pollerId) {
|
|
711
|
+
clearInterval(this.pollerId);
|
|
712
|
+
this.pollerId = null;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
setupRealtime() {
|
|
716
|
+
if (!isRealtimeTransport(this.config.transport)) {
|
|
717
|
+
console.warn('[SyncEngine] realtimeEnabled=true but transport does not support realtime. Falling back to polling.');
|
|
718
|
+
this.updateState({ transportMode: 'polling' });
|
|
719
|
+
this.setupPolling();
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
this.setConnectionState('connecting');
|
|
723
|
+
const transport = this.config.transport;
|
|
724
|
+
// Wire up presence events if transport supports them
|
|
725
|
+
if (transport.onPresenceEvent) {
|
|
726
|
+
this.realtimePresenceUnsub = transport.onPresenceEvent((event) => {
|
|
727
|
+
if (event.action === 'snapshot' && event.entries) {
|
|
728
|
+
this.updatePresence(event.scopeKey, event.entries);
|
|
729
|
+
}
|
|
730
|
+
else if (event.action === 'join' ||
|
|
731
|
+
event.action === 'leave' ||
|
|
732
|
+
event.action === 'update') {
|
|
733
|
+
this.handlePresenceEvent({
|
|
734
|
+
action: event.action,
|
|
735
|
+
scopeKey: event.scopeKey,
|
|
736
|
+
clientId: event.clientId ?? '',
|
|
737
|
+
actorId: event.actorId ?? '',
|
|
738
|
+
metadata: event.metadata,
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
this.realtimeDisconnect = transport.connect({ clientId: this.config.clientId }, (event) => {
|
|
744
|
+
if (event.event === 'sync') {
|
|
745
|
+
const hasInlineChanges = Array.isArray(event.data.changes) && event.data.changes.length > 0;
|
|
746
|
+
const cursor = event.data.cursor;
|
|
747
|
+
if (hasInlineChanges && typeof cursor === 'number') {
|
|
748
|
+
// WS delivered changes + cursor — may skip HTTP pull
|
|
749
|
+
this.handleWsDelivery(event.data.changes, cursor);
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
// Cursor-only wake-up or no cursor — must HTTP sync
|
|
753
|
+
this.sync({ trigger: 'ws' });
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}, (state) => {
|
|
757
|
+
switch (state) {
|
|
758
|
+
case 'connected': {
|
|
759
|
+
const wasConnectedBefore = this.hasRealtimeConnectedOnce;
|
|
760
|
+
this.hasRealtimeConnectedOnce = true;
|
|
761
|
+
this.setConnectionState('connected');
|
|
762
|
+
this.stopFallbackPolling();
|
|
763
|
+
this.sync();
|
|
764
|
+
if (wasConnectedBefore) {
|
|
765
|
+
this.scheduleRealtimeReconnectCatchupSync();
|
|
766
|
+
}
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
case 'connecting':
|
|
770
|
+
this.setConnectionState('connecting');
|
|
771
|
+
break;
|
|
772
|
+
case 'disconnected':
|
|
773
|
+
this.setConnectionState('reconnecting');
|
|
774
|
+
this.startFallbackPolling();
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
stopRealtime() {
|
|
780
|
+
if (this.realtimeCatchupTimeoutId) {
|
|
781
|
+
clearTimeout(this.realtimeCatchupTimeoutId);
|
|
782
|
+
this.realtimeCatchupTimeoutId = null;
|
|
783
|
+
}
|
|
784
|
+
if (this.realtimePresenceUnsub) {
|
|
785
|
+
this.realtimePresenceUnsub();
|
|
786
|
+
this.realtimePresenceUnsub = null;
|
|
787
|
+
}
|
|
788
|
+
if (this.realtimeDisconnect) {
|
|
789
|
+
this.realtimeDisconnect();
|
|
790
|
+
this.realtimeDisconnect = null;
|
|
791
|
+
}
|
|
792
|
+
this.stopFallbackPolling();
|
|
793
|
+
}
|
|
794
|
+
scheduleRealtimeReconnectCatchupSync() {
|
|
795
|
+
if (this.realtimeCatchupTimeoutId) {
|
|
796
|
+
clearTimeout(this.realtimeCatchupTimeoutId);
|
|
797
|
+
}
|
|
798
|
+
this.realtimeCatchupTimeoutId = setTimeout(() => {
|
|
799
|
+
this.realtimeCatchupTimeoutId = null;
|
|
800
|
+
if (this.isDestroyed || !this.isEnabled())
|
|
801
|
+
return;
|
|
802
|
+
if (this.state.connectionState !== 'connected')
|
|
803
|
+
return;
|
|
804
|
+
this.sync().catch(() => {
|
|
805
|
+
// Best-effort catch-up sync after reconnect.
|
|
806
|
+
});
|
|
807
|
+
}, REALTIME_RECONNECT_CATCHUP_DELAY_MS);
|
|
808
|
+
}
|
|
809
|
+
startFallbackPolling() {
|
|
810
|
+
if (this.fallbackPollerId)
|
|
811
|
+
return;
|
|
812
|
+
const interval = this.config.realtimeFallbackPollMs ?? 30_000;
|
|
813
|
+
this.fallbackPollerId = setInterval(() => {
|
|
814
|
+
if (!this.state.isSyncing && !this.isDestroyed) {
|
|
815
|
+
this.sync();
|
|
816
|
+
}
|
|
817
|
+
}, interval);
|
|
818
|
+
}
|
|
819
|
+
stopFallbackPolling() {
|
|
820
|
+
if (this.fallbackPollerId) {
|
|
821
|
+
clearInterval(this.fallbackPollerId);
|
|
822
|
+
this.fallbackPollerId = null;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Clear all in-memory mutation state and emit data:change so UI re-renders.
|
|
827
|
+
* Call this after deleting local data (e.g. reset flow) so that React hooks
|
|
828
|
+
* recompute fingerprints from scratch instead of seeing stale timestamps.
|
|
829
|
+
*/
|
|
830
|
+
resetLocalState() {
|
|
831
|
+
const tables = [...this.tableMutationTimestamps.keys()];
|
|
832
|
+
this.mutationTimestamps.clear();
|
|
833
|
+
this.tableMutationTimestamps.clear();
|
|
834
|
+
if (tables.length > 0) {
|
|
835
|
+
this.emit('data:change', {
|
|
836
|
+
scopes: tables,
|
|
837
|
+
timestamp: Date.now(),
|
|
838
|
+
});
|
|
839
|
+
this.config.onDataChange?.(tables);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Reconnect
|
|
844
|
+
*/
|
|
845
|
+
reconnect() {
|
|
846
|
+
if (this.isDestroyed || !this.isEnabled())
|
|
847
|
+
return;
|
|
848
|
+
if (this.state.transportMode === 'realtime' &&
|
|
849
|
+
isRealtimeTransport(this.config.transport)) {
|
|
850
|
+
// If we previously disconnected, we need to re-register callbacks via connect().
|
|
851
|
+
if (!this.realtimeDisconnect) {
|
|
852
|
+
this.setupRealtime();
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
this.config.transport.reconnect();
|
|
856
|
+
}
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
// Polling mode: restart the poller and trigger a sync immediately.
|
|
860
|
+
if (this.state.transportMode === 'polling') {
|
|
861
|
+
this.setupPolling();
|
|
862
|
+
// Trigger sync in background - errors are handled internally by sync()
|
|
863
|
+
this.sync().catch((err) => {
|
|
864
|
+
console.error('Unexpected error during reconnect sync:', err);
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Disconnect (pause syncing)
|
|
870
|
+
*/
|
|
871
|
+
disconnect() {
|
|
872
|
+
this.stop();
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Refresh outbox statistics
|
|
876
|
+
*/
|
|
877
|
+
async refreshOutboxStats(options) {
|
|
878
|
+
const db = this.config.db;
|
|
879
|
+
const res = await sql `
|
|
880
|
+
select
|
|
881
|
+
${sql.ref('status')},
|
|
882
|
+
count(${sql.ref('id')}) as ${sql.ref('count')}
|
|
883
|
+
from ${sql.table('sync_outbox_commits')}
|
|
884
|
+
group by ${sql.ref('status')}
|
|
885
|
+
`.execute(db);
|
|
886
|
+
const rows = res.rows;
|
|
887
|
+
const stats = {
|
|
888
|
+
pending: 0,
|
|
889
|
+
sending: 0,
|
|
890
|
+
failed: 0,
|
|
891
|
+
acked: 0,
|
|
892
|
+
total: 0,
|
|
893
|
+
};
|
|
894
|
+
for (const row of rows) {
|
|
895
|
+
const count = Number(row.count);
|
|
896
|
+
switch (row.status) {
|
|
897
|
+
case 'pending':
|
|
898
|
+
stats.pending = count;
|
|
899
|
+
break;
|
|
900
|
+
case 'sending':
|
|
901
|
+
stats.sending = count;
|
|
902
|
+
break;
|
|
903
|
+
case 'failed':
|
|
904
|
+
stats.failed = count;
|
|
905
|
+
break;
|
|
906
|
+
case 'acked':
|
|
907
|
+
stats.acked = count;
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
stats.total += count;
|
|
911
|
+
}
|
|
912
|
+
this.updateState({ pendingCount: stats.pending + stats.failed });
|
|
913
|
+
if (options?.emit !== false) {
|
|
914
|
+
this.emit('outbox:change', {
|
|
915
|
+
pendingCount: stats.pending,
|
|
916
|
+
sendingCount: stats.sending,
|
|
917
|
+
failedCount: stats.failed,
|
|
918
|
+
ackedCount: stats.acked,
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
return stats;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Get pending conflicts with operation details from outbox
|
|
925
|
+
*/
|
|
926
|
+
async getConflicts() {
|
|
927
|
+
// Join with outbox to get operation details
|
|
928
|
+
const res = await sql `
|
|
929
|
+
select
|
|
930
|
+
${sql.ref('c.id')},
|
|
931
|
+
${sql.ref('c.outbox_commit_id')},
|
|
932
|
+
${sql.ref('c.client_commit_id')},
|
|
933
|
+
${sql.ref('c.op_index')},
|
|
934
|
+
${sql.ref('c.result_status')},
|
|
935
|
+
${sql.ref('c.message')},
|
|
936
|
+
${sql.ref('c.code')},
|
|
937
|
+
${sql.ref('c.server_version')},
|
|
938
|
+
${sql.ref('c.server_row_json')},
|
|
939
|
+
${sql.ref('c.created_at')},
|
|
940
|
+
${sql.ref('oc.operations_json')}
|
|
941
|
+
from ${sql.table('sync_conflicts')} as ${sql.ref('c')}
|
|
942
|
+
inner join ${sql.table('sync_outbox_commits')} as ${sql.ref('oc')}
|
|
943
|
+
on ${sql.ref('oc.id')} = ${sql.ref('c.outbox_commit_id')}
|
|
944
|
+
where ${sql.ref('c.resolved_at')} is null
|
|
945
|
+
order by ${sql.ref('c.created_at')} desc
|
|
946
|
+
`.execute(this.config.db);
|
|
947
|
+
const rows = res.rows;
|
|
948
|
+
return rows.map((row) => {
|
|
949
|
+
// Extract operation details from outbox
|
|
950
|
+
let table = '';
|
|
951
|
+
let rowId = '';
|
|
952
|
+
let localPayload = null;
|
|
953
|
+
if (row.operations_json) {
|
|
954
|
+
try {
|
|
955
|
+
const operations = JSON.parse(row.operations_json);
|
|
956
|
+
if (Array.isArray(operations)) {
|
|
957
|
+
const op = operations[row.op_index];
|
|
958
|
+
if (isRecord(op)) {
|
|
959
|
+
if (typeof op.table === 'string')
|
|
960
|
+
table = op.table;
|
|
961
|
+
if (typeof op.row_id === 'string')
|
|
962
|
+
rowId = op.row_id;
|
|
963
|
+
localPayload =
|
|
964
|
+
op.payload === null
|
|
965
|
+
? null
|
|
966
|
+
: isRecord(op.payload)
|
|
967
|
+
? op.payload
|
|
968
|
+
: null;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
catch {
|
|
973
|
+
// Ignore parse errors
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return {
|
|
977
|
+
id: row.id,
|
|
978
|
+
outboxCommitId: row.outbox_commit_id,
|
|
979
|
+
clientCommitId: row.client_commit_id,
|
|
980
|
+
opIndex: row.op_index,
|
|
981
|
+
resultStatus: row.result_status,
|
|
982
|
+
message: row.message,
|
|
983
|
+
code: row.code,
|
|
984
|
+
serverVersion: row.server_version,
|
|
985
|
+
serverRowJson: row.server_row_json,
|
|
986
|
+
createdAt: row.created_at,
|
|
987
|
+
table,
|
|
988
|
+
rowId,
|
|
989
|
+
localPayload,
|
|
990
|
+
};
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Update subscriptions dynamically
|
|
995
|
+
*/
|
|
996
|
+
updateSubscriptions(subscriptions) {
|
|
997
|
+
this.config.subscriptions = subscriptions;
|
|
998
|
+
// Trigger a sync to apply new subscriptions
|
|
999
|
+
this.sync();
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Apply local mutations immediately to the database and emit change events.
|
|
1003
|
+
* Used for instant UI updates before the sync cycle completes.
|
|
1004
|
+
*/
|
|
1005
|
+
async applyLocalMutation(inputs) {
|
|
1006
|
+
const db = this.config.db;
|
|
1007
|
+
const shapes = this.config.shapes;
|
|
1008
|
+
const affectedTables = new Set();
|
|
1009
|
+
const now = Date.now();
|
|
1010
|
+
await db.transaction().execute(async (trx) => {
|
|
1011
|
+
for (const input of inputs) {
|
|
1012
|
+
const handler = shapes.get(input.table);
|
|
1013
|
+
if (!handler)
|
|
1014
|
+
continue;
|
|
1015
|
+
affectedTables.add(input.table);
|
|
1016
|
+
const change = {
|
|
1017
|
+
table: input.table,
|
|
1018
|
+
row_id: input.rowId,
|
|
1019
|
+
op: input.op,
|
|
1020
|
+
scopes: {},
|
|
1021
|
+
// For delete ops, row_json should be null; for upserts, default to empty object
|
|
1022
|
+
row_json: input.op === 'delete' ? null : (input.payload ?? {}),
|
|
1023
|
+
// null indicates local optimistic change (no server version yet)
|
|
1024
|
+
row_version: null,
|
|
1025
|
+
};
|
|
1026
|
+
await handler.applyChange({ trx }, change);
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
// Track mutation timestamps for fingerprint-based rerender optimization (in-memory only)
|
|
1030
|
+
this.recordLocalMutations(inputs
|
|
1031
|
+
.filter((i) => affectedTables.has(i.table))
|
|
1032
|
+
.map((i) => ({ table: i.table, rowId: i.rowId, op: i.op })), now);
|
|
1033
|
+
}
|
|
1034
|
+
/**
|
|
1035
|
+
* Clear failed commits from the outbox.
|
|
1036
|
+
* Use this to discard commits that keep failing (e.g., version conflicts).
|
|
1037
|
+
*/
|
|
1038
|
+
async clearFailedCommits() {
|
|
1039
|
+
const db = this.config.db;
|
|
1040
|
+
const res = await sql `
|
|
1041
|
+
delete from ${sql.table('sync_outbox_commits')}
|
|
1042
|
+
where ${sql.ref('status')} = ${sql.val('failed')}
|
|
1043
|
+
`.execute(db);
|
|
1044
|
+
const count = Number(res.numAffectedRows ?? 0);
|
|
1045
|
+
await this.refreshOutboxStats();
|
|
1046
|
+
return count;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Clear all pending and failed commits from the outbox.
|
|
1050
|
+
* Use this to reset the outbox completely (e.g., for testing).
|
|
1051
|
+
*/
|
|
1052
|
+
async clearAllCommits() {
|
|
1053
|
+
const db = this.config.db;
|
|
1054
|
+
const res = await sql `
|
|
1055
|
+
delete from ${sql.table('sync_outbox_commits')}
|
|
1056
|
+
where ${sql.ref('status')} in (${sql.join([
|
|
1057
|
+
sql.val('pending'),
|
|
1058
|
+
sql.val('failed'),
|
|
1059
|
+
])})
|
|
1060
|
+
`.execute(db);
|
|
1061
|
+
const count = Number(res.numAffectedRows ?? 0);
|
|
1062
|
+
await this.refreshOutboxStats();
|
|
1063
|
+
return count;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
//# sourceMappingURL=SyncEngine.js.map
|