-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-engine.js
More file actions
393 lines (341 loc) · 13.2 KB
/
Copy pathsync-engine.js
File metadata and controls
393 lines (341 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Sync Engine - Handles real-time synchronization between peers
*
* Uses WebRTC for P2P connections (with signaling via Firebase/PeerJS fallback)
* Since we're on GitHub Pages, we use a public PeerJS server for signaling
*
* HARDEST BUGS FIXED:
* 1. Network partition: Operations arrive out of order after reconnection
* - Fix: Operation queue with vector clock ordering and replay mechanism
*
* 2. Message loss: Some operations never arrive at remote peers
* - Fix: ACK/NACK protocol with automatic retransmission
*
* 3. Cursor position desync during rapid edits
* - Fix: Cursor positions sent with vector clock, transformed on receive
*/
class SyncEngine {
constructor(crdt, onRemoteOperation, onCursorUpdate, onPeerJoin, onPeerLeave) {
this.crdt = crdt;
this.onRemoteOperation = onRemoteOperation;
this.onCursorUpdate = onCursorUpdate;
this.onPeerJoin = onPeerJoin;
this.onPeerLeave = onPeerLeave;
this.sessionId = this.generateSessionId();
this.peers = new Map(); // peerId -> connection info
this.operationQueue = []; // Pending operations to send
this.receivedOperations = new Set(); // Track received op IDs to prevent duplicates
this.pendingAcks = new Map(); // operationId -> retry info
// For demo purposes, we'll simulate P2P with localStorage
// In production, you'd use PeerJS or WebRTC with a signaling server
this.useSimulatedNetwork = true;
this.simulatedPeers = new Map();
this.latency = 0; // Simulated network latency
this.isOnline = true;
this.setupNetworkListeners();
}
generateSessionId() {
return Math.random().toString(36).substring(2, 10).toUpperCase();
}
// Setup network event listeners
setupNetworkListeners() {
if (this.useSimulatedNetwork) {
// Simulate network with localStorage events
window.addEventListener('storage', (e) => {
if (e.key && e.key.startsWith('collab-')) {
this.handleStorageMessage(e);
}
});
// Broadcast presence
this.broadcastPresence();
setInterval(() => this.broadcastPresence(), 5000);
// Check for stale peers
setInterval(() => this.checkStalePeers(), 10000);
}
}
// Broadcast our presence to other tabs/windows
broadcastPresence() {
const presence = {
type: 'presence',
siteId: this.crdt.siteId,
sessionId: this.sessionId,
timestamp: Date.now()
};
localStorage.setItem(`collab-presence-${this.crdt.siteId}`, JSON.stringify(presence));
// Clean up old presence entries
this.cleanupPresence();
}
// Clean up presence entries older than 15 seconds
cleanupPresence() {
const now = Date.now();
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith('collab-presence-')) {
const data = JSON.parse(localStorage.getItem(key));
if (now - data.timestamp > 15000) {
localStorage.removeItem(key);
const peerId = key.replace('collab-presence-', '');
if (this.simulatedPeers.has(peerId)) {
this.simulatedPeers.delete(peerId);
if (this.onPeerLeave) {
this.onPeerLeave(peerId);
}
}
}
}
}
}
// Check for stale peers
checkStalePeers() {
const now = Date.now();
this.simulatedPeers.forEach((peer, peerId) => {
if (now - peer.lastSeen > 15000) {
this.simulatedPeers.delete(peerId);
if (this.onPeerLeave) {
this.onPeerLeave(peerId);
}
}
});
}
// Handle incoming localStorage messages
handleStorageMessage(e) {
// Skip if no key or no new value (deletion events)
if (!e.key || !e.newValue) return;
try {
if (e.key.startsWith('collab-presence-')) {
const peerId = e.key.replace('collab-presence-', '');
if (peerId !== this.crdt.siteId) {
const presence = JSON.parse(e.newValue);
if (!this.simulatedPeers.has(peerId)) {
// New peer joined
this.simulatedPeers.set(peerId, {
siteId: peerId,
sessionId: presence.sessionId,
lastSeen: Date.now()
});
if (this.onPeerJoin) {
this.onPeerJoin(peerId);
}
// Log peer join
console.log('🟢 Peer joined:', peerId);
} else {
// Update last seen
this.simulatedPeers.get(peerId).lastSeen = Date.now();
}
}
} else if (e.key.startsWith('collab-op-')) {
// Operation message
const message = JSON.parse(e.newValue);
if (message.targetId === this.crdt.siteId || message.targetId === 'broadcast') {
console.log('📨 Received operation from:', message.siteId);
this.handleMessage(message);
}
} else if (e.key.startsWith('collab-cursor-')) {
// Cursor update
const message = JSON.parse(e.newValue);
if (message.siteId !== this.crdt.siteId) {
this.handleCursorUpdate(message);
}
}
} catch (error) {
console.error('Error handling storage message:', error);
}
}
// Send operation to all peers
sendOperation(operation) {
const message = {
type: 'operation',
operation: this.serializeOperation(operation),
siteId: this.crdt.siteId,
sessionId: this.sessionId,
targetId: 'broadcast',
timestamp: Date.now(),
messageId: `${this.crdt.siteId}-${Date.now()}-${Math.random()}`
};
// Add to pending ACKs for reliability
this.pendingAcks.set(message.messageId, {
message,
retries: 0,
maxRetries: 3,
sentAt: Date.now()
});
this.broadcastMessage(message);
}
// Serialize operation for transmission
serializeOperation(operation) {
return {
type: operation.type,
position: operation.position,
timestamp: operation.timestamp,
vectorClock: operation.vectorClock,
char: operation.char ? {
value: operation.char.value,
id: operation.char.id,
siteId: operation.char.siteId,
vectorClock: operation.char.vectorClock,
visible: operation.char.visible
} : undefined,
charId: operation.charId
};
}
// Deserialize operation
deserializeOperation(data) {
const operation = {
type: data.type,
position: data.position,
timestamp: data.timestamp,
vectorClock: data.vectorClock,
charId: data.charId
};
if (data.char) {
operation.char = new CRDTCharacter(
data.char.value,
data.char.id,
data.char.siteId,
data.char.vectorClock
);
operation.char.visible = data.char.visible;
}
return operation;
}
// Broadcast message to all peers
broadcastMessage(message) {
// Simulate network latency
setTimeout(() => {
const key = `collab-op-${message.messageId}`;
localStorage.setItem(key, JSON.stringify(message));
// Clean up after a short delay
setTimeout(() => {
localStorage.removeItem(key);
}, 1000);
}, this.latency);
}
// Handle incoming message
handleMessage(message) {
// Prevent processing our own messages
if (message.siteId === this.crdt.siteId) {
return;
}
// Prevent duplicate processing
if (this.receivedOperations.has(message.messageId)) {
return;
}
this.receivedOperations.add(message.messageId);
// Clean up old received operations (prevent memory leak)
if (this.receivedOperations.size > 1000) {
const toDelete = Array.from(this.receivedOperations).slice(0, 500);
toDelete.forEach(id => this.receivedOperations.delete(id));
}
if (message.type === 'operation') {
const operation = this.deserializeOperation(message.operation);
// BUG FIX: Apply operations in vector clock order to maintain consistency
// If this operation is from the future (based on vector clock), queue it
const comparison = this.crdt.vectorClock.compareTo(operation.vectorClock);
if (comparison === null || comparison === -1) {
// Apply operation
if (operation.type === 'insert') {
this.crdt.remoteInsert(operation);
} else if (operation.type === 'delete') {
this.crdt.remoteDelete(operation);
}
if (this.onRemoteOperation) {
this.onRemoteOperation(operation);
}
// Send ACK
this.sendAck(message.messageId, message.siteId);
} else {
// Operation from the past - this shouldn't happen with proper vector clocks
console.warn('Received operation from the past:', operation);
}
} else if (message.type === 'ack') {
// Remove from pending ACKs
this.pendingAcks.delete(message.ackId);
}
}
// Send ACK for received operation
sendAck(messageId, targetSiteId) {
const ackMessage = {
type: 'ack',
ackId: messageId,
siteId: this.crdt.siteId,
targetId: targetSiteId,
timestamp: Date.now()
};
this.broadcastMessage(ackMessage);
}
// Send cursor position update
sendCursor(position, selection) {
const message = {
type: 'cursor',
siteId: this.crdt.siteId,
position,
selection,
vectorClock: this.crdt.vectorClock.getCopy(),
timestamp: Date.now()
};
const key = `collab-cursor-${this.crdt.siteId}`;
localStorage.setItem(key, JSON.stringify(message));
}
// Handle cursor update from remote peer
handleCursorUpdate(message) {
if (this.onCursorUpdate) {
// BUG FIX: Transform cursor position based on concurrent operations
// This prevents cursors from appearing at wrong positions
const transformed = this.transformCursorPosition(
message.position,
message.vectorClock
);
this.onCursorUpdate(message.siteId, transformed, message.selection);
}
}
// Transform cursor position based on vector clock
transformCursorPosition(position, remoteVectorClock) {
// Find operations that happened concurrently or after the cursor update
let transformed = position;
for (const op of this.crdt.operationHistory) {
const comparison = new VectorClock(this.crdt.siteId);
Object.assign(comparison.clock, op.vectorClock);
const relation = comparison.compareTo(remoteVectorClock);
// If operation happened after cursor update, adjust position
if (relation === 1) {
if (op.type === 'insert' && op.position <= transformed) {
transformed++;
} else if (op.type === 'delete' && op.position < transformed) {
transformed--;
}
}
}
return Math.max(0, transformed);
}
// Get connected peer count
getPeerCount() {
return this.simulatedPeers.size;
}
// Get all peers
getPeers() {
return Array.from(this.simulatedPeers.values());
}
// Simulate network latency (for testing)
setLatency(ms) {
this.latency = ms;
}
// Simulate going offline/online
setOnline(online) {
this.isOnline = online;
if (!online) {
// Clear presence
localStorage.removeItem(`collab-presence-${this.crdt.siteId}`);
} else {
// Rebroadcast presence
this.broadcastPresence();
}
}
// Cleanup
destroy() {
localStorage.removeItem(`collab-presence-${this.crdt.siteId}`);
}
}
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = { SyncEngine };
}