leptos-sync-core 0.9.0

Core synchronization library for Leptos applications
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Real-time synchronization engine for live collaboration

use crate::crdt::{Mergeable, ReplicaId};
use crate::storage::{Storage, LocalStorage};
use crate::transport::SyncTransport;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{broadcast, RwLock};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RealtimeSyncError {
    #[error("Transport error: {0}")]
    Transport(String),
    #[error("Storage error: {0}")]
    Storage(String),
    #[error("Serialization error: {0}")]
    Serialization(String),
    #[error("Subscription not found: {0}")]
    SubscriptionNotFound(String),
    #[error("Invalid operation: {0}")]
    InvalidOperation(String),
}

/// Real-time synchronization event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RealtimeEvent {
    /// Document changed
    DocumentChanged {
        key: String,
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
        change_type: ChangeType,
    },
    /// User joined
    UserJoined {
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
        user_info: Option<UserInfo>,
    },
    /// User left
    UserLeft {
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
    },
    /// Sync started
    SyncStarted {
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
    },
    /// Sync completed
    SyncCompleted {
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
        changes_synced: usize,
    },
    /// Conflict detected
    ConflictDetected {
        key: String,
        replica_id: ReplicaId,
        timestamp: DateTime<Utc>,
        conflict_type: String,
    },
}

/// Type of change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChangeType {
    Created,
    Updated,
    Deleted,
    Merged,
}

/// User information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInfo {
    pub name: Option<String>,
    pub avatar: Option<String>,
    pub color: Option<String>,
}

/// Real-time synchronization manager
pub struct RealtimeSyncManager<Tr>
where
    Tr: SyncTransport + Clone + Send + Sync + 'static,
{
    replica_id: ReplicaId,
    transport: Tr,
    storage: Arc<Storage>,
    event_sender: broadcast::Sender<RealtimeEvent>,
    subscriptions: Arc<RwLock<HashMap<String, Subscription>>>,
    active_users: Arc<RwLock<HashMap<ReplicaId, UserInfo>>>,
    sync_state: Arc<RwLock<SyncState>>,
    heartbeat_interval: std::time::Duration,
    presence_timeout: std::time::Duration,
}

/// Subscription to real-time events
pub struct Subscription {
    pub id: String,
    pub event_types: Vec<String>,
    pub callback: Box<dyn Fn(RealtimeEvent) + Send + Sync>,
}

/// Synchronization state
#[derive(Debug, Clone)]
pub struct SyncState {
    pub is_syncing: bool,
    pub last_sync: Option<DateTime<Utc>>,
    pub connected_users: usize,
    pub pending_changes: usize,
    pub sync_errors: Vec<String>,
}

impl<Tr> RealtimeSyncManager<Tr>
where
    Tr: SyncTransport + Clone + Send + Sync + 'static,
{
    pub fn new(
        replica_id: ReplicaId,
        transport: Tr,
        storage: Arc<Storage>,
    ) -> Self {
        let (event_sender, _) = broadcast::channel(1000);
        
        Self {
            replica_id,
            transport,
            storage,
            event_sender,
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
            active_users: Arc::new(RwLock::new(HashMap::new())),
            sync_state: Arc::new(RwLock::new(SyncState {
                is_syncing: false,
                last_sync: None,
                connected_users: 0,
                pending_changes: 0,
                sync_errors: Vec::new(),
            })),
            heartbeat_interval: std::time::Duration::from_secs(30),
            presence_timeout: std::time::Duration::from_secs(120),
        }
    }

    /// Start real-time synchronization
    pub async fn start(&mut self) -> Result<(), RealtimeSyncError> {
        let mut state = self.sync_state.write().await;
        state.is_syncing = true;
        drop(state);

        // Announce presence
        self.announce_presence().await?;

        // Start heartbeat
        self.start_heartbeat().await;

        // Start presence monitoring
        self.start_presence_monitoring().await;

        // Emit sync started event
        self.emit_event(RealtimeEvent::SyncStarted {
            replica_id: self.replica_id,
            timestamp: Utc::now(),
        }).await;

        Ok(())
    }

    /// Stop real-time synchronization
    pub async fn stop(&mut self) -> Result<(), RealtimeSyncError> {
        let mut state = self.sync_state.write().await;
        state.is_syncing = false;
        drop(state);

        // Announce departure
        self.announce_departure().await?;

        // Emit sync completed event
        self.emit_event(RealtimeEvent::SyncCompleted {
            replica_id: self.replica_id,
            timestamp: Utc::now(),
            changes_synced: 0,
        }).await;

        Ok(())
    }

    /// Subscribe to real-time events
    pub async fn subscribe(
        &self,
        event_types: Vec<String>,
        callback: Box<dyn Fn(RealtimeEvent) + Send + Sync>,
    ) -> Result<String, RealtimeSyncError> {
        let subscription_id = uuid::Uuid::new_v4().to_string();
        
        let subscription = Subscription {
            id: subscription_id.clone(),
            event_types,
            callback,
        };

        let mut subscriptions = self.subscriptions.write().await;
        subscriptions.insert(subscription_id.clone(), subscription);

        Ok(subscription_id)
    }

    /// Unsubscribe from real-time events
    pub async fn unsubscribe(&self, subscription_id: &str) -> Result<(), RealtimeSyncError> {
        let mut subscriptions = self.subscriptions.write().await;
        
        if subscriptions.remove(subscription_id).is_some() {
            Ok(())
        } else {
            Err(RealtimeSyncError::SubscriptionNotFound(subscription_id.to_string()))
        }
    }

    /// Broadcast a change to all connected peers
    pub async fn broadcast_change<T: Mergeable + Serialize + Clone>(
        &self,
        key: &str,
        value: &T,
        change_type: ChangeType,
    ) -> Result<(), RealtimeSyncError> {
        // Store locally first
        self.storage.set(key, value).await
            .map_err(|e| RealtimeSyncError::Storage(e.to_string()))?;

        // Serialize and send via transport
        let change_message = ChangeMessage {
            key: key.to_string(),
            data: serde_json::to_vec(value)
                .map_err(|e| RealtimeSyncError::Serialization(e.to_string()))?,
            replica_id: self.replica_id,
            timestamp: Utc::now(),
            change_type: change_type.clone(),
        };

        let message_bytes = serde_json::to_vec(&change_message)
            .map_err(|e| RealtimeSyncError::Serialization(e.to_string()))?;

        self.transport.send(&message_bytes).await
            .map_err(|e| RealtimeSyncError::Transport(e.to_string()))?;

        // Emit local event
        self.emit_event(RealtimeEvent::DocumentChanged {
            key: key.to_string(),
            replica_id: self.replica_id,
            timestamp: Utc::now(),
            change_type,
        }).await;

        Ok(())
    }

    /// Process incoming changes from peers
    pub async fn process_incoming_changes(&mut self) -> Result<usize, RealtimeSyncError> {
        let messages = self.transport.receive().await
            .map_err(|e| RealtimeSyncError::Transport(e.to_string()))?;

        let mut changes_processed = 0;

        for message_bytes in messages {
            if let Ok(change_message) = serde_json::from_slice::<ChangeMessage>(&message_bytes) {
                // Process the change
                self.process_change(change_message).await?;
                changes_processed += 1;
            }
        }

        // Update sync state
        let mut state = self.sync_state.write().await;
        state.last_sync = Some(Utc::now());
        state.pending_changes = state.pending_changes.saturating_sub(changes_processed);

        Ok(changes_processed)
    }

    /// Get current synchronization state
    pub async fn get_sync_state(&self) -> SyncState {
        self.sync_state.read().await.clone()
    }

    /// Get active users
    pub async fn get_active_users(&self) -> HashMap<ReplicaId, UserInfo> {
        self.active_users.read().await.clone()
    }

    /// Announce presence to peers
    async fn announce_presence(&self) -> Result<(), RealtimeSyncError> {
        let presence_message = PresenceMessage {
            replica_id: self.replica_id,
            timestamp: Utc::now(),
            user_info: None, // Could be populated with actual user info
        };

        let message_bytes = serde_json::to_vec(&presence_message)
            .map_err(|e| RealtimeSyncError::Serialization(e.to_string()))?;

        self.transport.send(&message_bytes).await
            .map_err(|e| RealtimeSyncError::Transport(e.to_string()))?;

        Ok(())
    }

    /// Announce departure to peers
    async fn announce_departure(&self) -> Result<(), RealtimeSyncError> {
        let departure_message = DepartureMessage {
            replica_id: self.replica_id,
            timestamp: Utc::now(),
        };

        let message_bytes = serde_json::to_vec(&departure_message)
            .map_err(|e| RealtimeSyncError::Serialization(e.to_string()))?;

        self.transport.send(&message_bytes).await
            .map_err(|e| RealtimeSyncError::Transport(e.to_string()))?;

        Ok(())
    }

    /// Start heartbeat mechanism
    async fn start_heartbeat(&self) {
        let transport = self.transport.clone();
        let replica_id = self.replica_id;
        let interval = self.heartbeat_interval;

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);
            
            loop {
                interval_timer.tick().await;
                
                // Send heartbeat
                let heartbeat_message = HeartbeatMessage {
                    replica_id,
                    timestamp: Utc::now(),
                };

                if let Ok(message_bytes) = serde_json::to_vec(&heartbeat_message) {
                    let _ = transport.send(&message_bytes).await;
                }
            }
        });
    }

    /// Start presence monitoring
    async fn start_presence_monitoring(&self) {
        let active_users = self.active_users.clone();
        let timeout = self.presence_timeout;

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(std::time::Duration::from_secs(60));
            
            loop {
                interval_timer.tick().await;
                
                let now = Utc::now();
                let mut users = active_users.write().await;
                
                // Remove users who haven't sent heartbeat recently
                users.retain(|_, user_info| {
                    // This is a simplified check - in reality you'd track last heartbeat
                    true
                });
            }
        });
    }

    /// Process an incoming change
    async fn process_change(&mut self, change_message: ChangeMessage) -> Result<(), RealtimeSyncError> {
        // Emit event for the change
        self.emit_event(RealtimeEvent::DocumentChanged {
            key: change_message.key.clone(),
            replica_id: change_message.replica_id,
            timestamp: change_message.timestamp,
            change_type: change_message.change_type,
        }).await;

        Ok(())
    }

    /// Emit an event to all subscribers
    async fn emit_event(&self, event: RealtimeEvent) {
        let subscriptions = self.subscriptions.read().await;
        
        for subscription in subscriptions.values() {
            // Check if subscription is interested in this event type
            let event_type = match &event {
                RealtimeEvent::DocumentChanged { .. } => "document_changed",
                RealtimeEvent::UserJoined { .. } => "user_joined",
                RealtimeEvent::UserLeft { .. } => "user_left",
                RealtimeEvent::SyncStarted { .. } => "sync_started",
                RealtimeEvent::SyncCompleted { .. } => "sync_completed",
                RealtimeEvent::ConflictDetected { .. } => "conflict_detected",
            };

            if subscription.event_types.contains(&event_type.to_string()) 
                || subscription.event_types.contains(&"*".to_string()) {
                (subscription.callback)(event.clone());
            }
        }
    }
}

/// Change message for broadcasting updates
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChangeMessage {
    key: String,
    data: Vec<u8>,
    replica_id: ReplicaId,
    timestamp: DateTime<Utc>,
    change_type: ChangeType,
}

/// Presence message for announcing user presence
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PresenceMessage {
    replica_id: ReplicaId,
    timestamp: DateTime<Utc>,
    user_info: Option<UserInfo>,
}

/// Departure message for announcing user departure
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DepartureMessage {
    replica_id: ReplicaId,
    timestamp: DateTime<Utc>,
}

/// Heartbeat message for keeping connections alive
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HeartbeatMessage {
    replica_id: ReplicaId,
    timestamp: DateTime<Utc>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::memory::MemoryStorage;
    use crate::transport::memory::InMemoryTransport;

    #[tokio::test]
    async fn test_realtime_sync_manager_creation() {
        let storage = Arc::new(Storage::memory());
        let transport = InMemoryTransport::new();
        let replica_id = ReplicaId::default();
        
        let manager = RealtimeSyncManager::new(replica_id, transport, storage);
        assert_eq!(manager.replica_id, replica_id);
    }

    #[tokio::test]
    async fn test_subscription_management() {
        let storage = Arc::new(Storage::memory());
        let transport = InMemoryTransport::new();
        let replica_id = ReplicaId::default();
        
        let manager = RealtimeSyncManager::new(replica_id, transport, storage);
        
        // Subscribe to events
        let callback = Box::new(|_event: RealtimeEvent| {});
        let subscription_id = manager.subscribe(
            vec!["document_changed".to_string()],
            callback
        ).await.unwrap();
        
        // Unsubscribe
        let result = manager.unsubscribe(&subscription_id).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_sync_state_management() {
        let storage = Arc::new(Storage::memory());
        let transport = InMemoryTransport::new();
        let replica_id = ReplicaId::default();
        
        let manager = RealtimeSyncManager::new(replica_id, transport, storage);
        
        let state = manager.get_sync_state().await;
        assert!(!state.is_syncing);
        assert_eq!(state.connected_users, 0);
        assert_eq!(state.pending_changes, 0);
    }
}