Expand description
Wind UI framework synchronization.
§Wind Advanced Synchronization - Real-time Document & UI Sync
File Responsibilities: This module implements advanced synchronization features that keep Wind’s frontend state in sync with Mountain’s backend state in real-time. It handles document changes, UI state updates, and broadcast updates across the editor ecosystem.
Architectural Role in Wind-Mountain Connection:
The WindAdvancedSync module is responsible for:
- Document Synchronization: Real-time tracking and synchronization of document changes between Wind (frontend editor) and Mountain (backend services)
- UI State Sync: Synchronizing UI state across multiple editor windows
- Cursor positions
- Selection ranges
- Zoom levels
- Theme and layout
- Real-time Broadcasting: Broadcasting updates to interested subscribers
- Conflict Detection: Identifying and handling conflicting changes
- Performance Tracking: Monitoring sync performance and health
Synchronization Architecture:
Three Sync Layers:
1. Document Synchronization (Every 5 seconds):
Wind Editor (User Edits)
|
| Detect changes
v
WindAdvancedSync
|
| Check for conflicts
v
Mountain Services
|
| Apply changes
v
File System / Storage2. UI State Synchronization (Every 1 second):
Wind UI Window
|
| Capture state (cursor, selection, zoom)
v
WindAdvancedSync
|
| Update internal state
v
Apply to other windows3. Real-time Updates (Every 100ms):
Subscribed Listeners
^
| Broadcast updates
|
WindAdvancedSync
|
| Queue updates
v
Update QueueDocument Synchronization States:
enum SyncState {
Modified, // Changed locally, not synced
Synced, // Successfully synchronized
Conflicted, // Conflicts need resolution
Offline, // Cannot sync (offline)
}Change Types Supported:
enum ChangeType {
Update, // File content updated
Insert, // New file created
Delete, // File deleted
Move, // File moved/renamed
Other, // Other changes
}Conflict Detection (Microsoft-Inspired):
Detection Criteria:
- Document modified recently (within 10 seconds of last sync)
- Document is already in conflicted state
- Multiple simultaneous changes detected
Conflict Response:
return Err(format!(
"Conflict detected: Document {} was modified recently ({}s ago)",
document_id,
current_time - document.last_modified
));Error Recovery (Circuit Breaker Pattern):
Circuit Breaker States:
- Closed (Normal): Operations proceed normally
- Open (Degraded): Too many failures, slow down sync interval
- Half-Open (Testing): Slowly testing if system recovered
Recovery Logic:
- Track consecutive failures (max 3)
- On reaching limit: Increase sync interval to 30s
- On success: Reset interval to 5s
- Provides protection against cascading failures
Trackable Metrics:
Sync.Status:
SyncStatus {
total_documents:u32, // All tracked documents
synced_documents:u32, // Successfully synced
conflicted_documents:u32, // Have conflicts
offline_documents:u32, // Cannot sync
last_sync_duration_ms:u64, // Time for last sync
}UI State Tracked:
UIStateSynchronization {
active_editor: Option<String>,
cursor_positions: HashMap<String, (u32, u32)>, // Document -> (line, col)
selection_ranges: HashMap<String, (u32, u32)>, // Document -> (start, end)
view_state: ViewState {
zoom_level: f32,
sidebar_visible: bool,
panel_visible: bool,
status_bar_visible: bool,
},
theme: String,
layout: LayoutState,
}Real-time Update System:
Update Flow:
- Queue updates as they occur
- Subscriber management per target
- Periodic broadcast (100ms)
- Emit events via Tauri
Subscription Model:
// Subscribe to updates for a target
sync.subscribe_to_updates("file-changes", "window-1").await?;
// Queue updates
sync.queue_update(RealTimeUpdate { target:"file-changes".to_string(), data:modified_content })
.await?;
// Broadcasts go to:
// - "real-time-update-window-1"
// - "real-time-update-window-2"
// etc...Tauri Commands:
mountain_add_document_for_sync- Add document to sync trackingmountain_get_sync_status- Get current sync statusmountain_subscribe_to_updates- Subscribe to real-time updates
Events Emitted:
mountain_sync_status_update- Sync status changesmountain_performance_update- Performance metricsreal-time-update-{subscriber}- Real-time updates
Initialization:
// In Mountain setup
let sync = Arc::new(WindAdvancedSync::new(runtime));
app_handle.manage(sync.clone());
// Start sync tasks
let sync_clone = sync.clone();
tokio::spawn(async move {
sync_clone.start_synchronization().await;
});Usage Examples:
Add Document for Sync:
// From Wind TypeScript
await invoke('mountain_add_document_for_sync', {
documentId: 'file-123',
filePath: '/project/src/main.rs'
});Check Sync Status:
const status = await invoke('mountain_get_sync_status');
console.log(`Synced: ${status.syncedDocuments}/${status.totalDocuments}`);Subscribe to Updates:
await invoke('mountain_subscribe_to_updates', {
target: 'file-changes',
subscriber: 'my-window-id'
});
// Listen for updates
app.handle.listen('real-time-update-my-window-id', (event) => {
console.log('Real-time update:', event.payload);
});Performance Tracking:
Metrics Collected:
- Total messages sent/received
- Average latency
- Connection uptime
- Error count
- Sync duration
Logged on Every Operation:
trace!(
"Document sync completed: {} success, {} errors, {:.2}ms",
success_count,
error_count,
sync_duration.as_millis()
);Integration with Other Modules:
TauriIPCServer:
- Used for broadcasting events to Wind
- Emits sync and performance updates
AdvancedFeatures:
- Collaboration sessions work with document sync
- Shared focus on real-time updates
StatusReporter:
- Sync status reported to Sky for monitoring
- Performance metrics shared
Future Enhancements:
- Operational Transformation: For collaborative editing
- Conflict Resolution UI: User-facing conflict resolution
- Delta Sync: Only sync changed portions
- Sync Prioritization: Prioritize active documents
- Offline Sync Support: Queue changes when offline
Structs§
- Document
Change - Document change
- Document
Synchronization - Document synchronization state
- Grid
Layout - Grid layout
- Layout
State - Editor layout state
- Real
Time Update - Real-time update
- Real
Time Updates - Real-time updates manager
- Sync
Status - Synchronization status
- Synchronized
Document - Single synchronized document
- UIState
Synchronization - UI state synchronization
- View
State - View state
- Wind
Advanced Sync - Advanced Wind synchronization features
Enums§
- Change
Type - Change type for document modifications
- Sync
State - Document synchronization state
Functions§
- initialize_
wind_ advanced_ sync - Initialize Wind advanced synchronization
- mountain_
add_ document_ for_ sync - Tauri command to add document for synchronization
- mountain_
get_ sync_ status - Tauri command to get sync status
- mountain_
subscribe_ to_ updates - Tauri command to subscribe to updates