leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
//! Offline Support Module
//!
//! This module provides comprehensive offline support features for the Helios charting library,
//! including Progressive Web App capabilities, service worker management, and offline data storage.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::RwLock;

pub mod cache_management;
pub mod offline_storage;
pub mod pwa_features;
pub mod service_worker;
pub mod sync_management;

pub use cache_management::*;
pub use offline_storage::*;
pub use pwa_features::*;
pub use service_worker::*;
pub use sync_management::*;

/// Offline support manager that coordinates all offline functionality
#[derive(Debug, Clone)]
pub struct OfflineSupportManager {
    service_worker: Arc<RwLock<ServiceWorkerManager>>,
    cache_manager: Arc<RwLock<CacheManager>>,
    offline_storage: Arc<RwLock<OfflineStorageManager>>,
    sync_manager: Arc<RwLock<SyncManager>>,
    pwa_features: Arc<RwLock<PwaFeaturesManager>>,
    config: OfflineSupportConfig,
    stats: Arc<RwLock<OfflineSupportStats>>,
}

/// Configuration for offline support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OfflineSupportConfig {
    pub service_worker_config: ServiceWorkerConfig,
    pub cache_config: CacheConfig,
    pub storage_config: OfflineStorageConfig,
    pub sync_config: SyncConfig,
    pub pwa_config: PwaConfig,
    pub enable_offline_mode: bool,
    pub auto_sync_enabled: bool,
    pub cache_size_limit: usize,
    pub sync_interval: u64,
}

/// Offline support statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OfflineSupportStats {
    pub is_online: bool,
    pub cache_hit_ratio: f64,
    pub offline_data_size: usize,
    pub sync_operations: u64,
    pub failed_sync_operations: u64,
    pub service_worker_status: ServiceWorkerStatus,
    pub pwa_install_status: PwaInstallStatus,
    pub total_offline_requests: u64,
    pub successful_offline_requests: u64,
    pub optimization_benefits: HashMap<String, f64>,
    pub total_optimizations_applied: u32,
}

/// Offline support errors
#[derive(Error, Debug)]
pub enum OfflineSupportError {
    #[error("Service worker error: {message}")]
    ServiceWorkerError { message: String },

    #[error("Cache management error: {message}")]
    CacheManagementError { message: String },

    #[error("Offline storage error: {message}")]
    OfflineStorageError { message: String },

    #[error("Sync management error: {message}")]
    SyncManagementError { message: String },

    #[error("PWA features error: {message}")]
    PwaFeaturesError { message: String },

    #[error("Configuration error: {message}")]
    ConfigurationError { message: String },

    #[error("Network error: {message}")]
    NetworkError { message: String },
}

impl From<ServiceWorkerError> for OfflineSupportError {
    fn from(err: ServiceWorkerError) -> Self {
        OfflineSupportError::ServiceWorkerError {
            message: err.to_string(),
        }
    }
}

impl From<CacheManagementError> for OfflineSupportError {
    fn from(err: CacheManagementError) -> Self {
        OfflineSupportError::CacheManagementError {
            message: err.to_string(),
        }
    }
}

impl From<OfflineStorageError> for OfflineSupportError {
    fn from(err: OfflineStorageError) -> Self {
        OfflineSupportError::OfflineStorageError {
            message: err.to_string(),
        }
    }
}

impl From<SyncManagementError> for OfflineSupportError {
    fn from(err: SyncManagementError) -> Self {
        OfflineSupportError::SyncManagementError {
            message: err.to_string(),
        }
    }
}

impl From<PwaFeaturesError> for OfflineSupportError {
    fn from(err: PwaFeaturesError) -> Self {
        OfflineSupportError::PwaFeaturesError {
            message: err.to_string(),
        }
    }
}

impl OfflineSupportManager {
    /// Create a new offline support manager
    pub fn new(config: OfflineSupportConfig) -> Self {
        Self {
            service_worker: Arc::new(RwLock::new(ServiceWorkerManager::new(
                config.service_worker_config.clone(),
            ))),
            cache_manager: Arc::new(RwLock::new(CacheManager::new(config.cache_config.clone()))),
            offline_storage: Arc::new(RwLock::new(OfflineStorageManager::new(
                config.storage_config.clone(),
            ))),
            sync_manager: Arc::new(RwLock::new(SyncManager::new(config.sync_config.clone()))),
            pwa_features: Arc::new(RwLock::new(PwaFeaturesManager::new(
                config.pwa_config.clone(),
            ))),
            config,
            stats: Arc::new(RwLock::new(OfflineSupportStats::default())),
        }
    }

    /// Initialize offline support
    pub async fn initialize(&self) -> Result<(), OfflineSupportError> {
        if !self.config.enable_offline_mode {
            return Ok(());
        }

        // Initialize all subsystems
        self.service_worker.read().await.initialize().await?;
        self.cache_manager.read().await.initialize().await?;
        self.offline_storage.read().await.initialize().await?;
        self.sync_manager.read().await.initialize().await?;
        self.pwa_features.read().await.initialize().await?;

        // Update statistics
        self.update_stats().await;

        Ok(())
    }

    /// Handle offline request
    pub async fn handle_offline_request(
        &self,
        request: OfflineRequest,
    ) -> Result<OfflineResponse, OfflineSupportError> {
        let mut stats = self.stats.write().await;
        stats.total_offline_requests += 1;

        // Try cache first
        if let Ok(cached_response) = self
            .cache_manager
            .read()
            .await
            .get_cached_response(&request.url)
            .await
        {
            stats.successful_offline_requests += 1;
            return Ok(cached_response);
        }

        // Try offline storage (only for specific keys, not URLs)
        if request.url.starts_with("data://") {
            let key = request.url.strip_prefix("data://").unwrap_or(&request.url);
            if let Ok(stored_data) = self.offline_storage.read().await.get_data(key).await {
                stats.successful_offline_requests += 1;
                return Ok(OfflineResponse {
                    url: request.url,
                    data: stored_data,
                    timestamp: std::time::SystemTime::now(),
                    source: OfflineResponseSource::OfflineStorage,
                });
            }
        }

        // Return offline fallback
        Ok(OfflineResponse {
            url: request.url,
            data: b"Offline - Data not available".to_vec(),
            timestamp: std::time::SystemTime::now(),
            source: OfflineResponseSource::OfflineFallback,
        })
    }

    /// Sync data when online
    pub async fn sync_data(&self) -> Result<(), OfflineSupportError> {
        if !self.config.auto_sync_enabled {
            return Ok(());
        }

        let result = self.sync_manager.read().await.sync_pending_data().await;

        let mut stats = self.stats.write().await;
        stats.sync_operations += 1;
        if result.is_err() {
            stats.failed_sync_operations += 1;
        }

        result.map_err(Into::into)
    }

    /// Get current statistics
    pub async fn get_stats(&self) -> OfflineSupportStats {
        self.stats.read().await.clone()
    }

    /// Update configuration
    pub async fn update_config(
        &mut self,
        config: OfflineSupportConfig,
    ) -> Result<(), OfflineSupportError> {
        self.config = config.clone();

        // Update subsystem configurations
        {
            let mut service_worker = self.service_worker.write().await;
            service_worker
                .update_config(config.service_worker_config)
                .await?;
        }

        {
            let mut cache_manager = self.cache_manager.write().await;
            cache_manager.update_config(config.cache_config).await?;
        }

        {
            let mut offline_storage = self.offline_storage.write().await;
            offline_storage.update_config(config.storage_config).await?;
        }

        {
            let mut sync_manager = self.sync_manager.write().await;
            sync_manager.update_config(config.sync_config).await?;
        }

        {
            let mut pwa_features = self.pwa_features.write().await;
            pwa_features.update_config(config.pwa_config).await?;
        }

        Ok(())
    }

    /// Check if device is online
    pub async fn is_online(&self) -> bool {
        self.stats.read().await.is_online
    }

    /// Set online status
    pub async fn set_online_status(&self, is_online: bool) {
        let mut stats = self.stats.write().await;
        stats.is_online = is_online;
    }

    /// Update statistics
    async fn update_stats(&self) {
        let mut stats = self.stats.write().await;

        // Collect stats from all subsystems
        let service_worker_stats = self.service_worker.read().await.get_stats().await;
        let cache_stats = self.cache_manager.read().await.get_stats().await;
        let storage_stats = self.offline_storage.read().await.get_stats().await;
        let sync_stats = self.sync_manager.read().await.get_stats().await;
        let pwa_stats = self.pwa_features.read().await.get_stats().await;

        // Update combined stats
        stats.cache_hit_ratio = cache_stats.hit_ratio;
        stats.offline_data_size = storage_stats.total_data_size;
        stats.sync_operations = sync_stats.total_syncs;
        stats.failed_sync_operations = sync_stats.failed_syncs;
        stats.service_worker_status = service_worker_stats.status;
        stats.pwa_install_status = pwa_stats.install_status;

        // Update optimization benefits
        stats.optimization_benefits.insert(
            "service_worker".to_string(),
            service_worker_stats.optimization_benefit,
        );
        stats.optimization_benefits.insert(
            "cache_management".to_string(),
            cache_stats.optimization_benefit,
        );
        stats.optimization_benefits.insert(
            "offline_storage".to_string(),
            storage_stats.optimization_benefit,
        );
        stats.optimization_benefits.insert(
            "sync_management".to_string(),
            sync_stats.optimization_benefit,
        );
        stats
            .optimization_benefits
            .insert("pwa_features".to_string(), pwa_stats.optimization_benefit);

        stats.total_optimizations_applied = service_worker_stats.optimizations_applied
            + cache_stats.optimizations_applied
            + storage_stats.optimizations_applied
            + sync_stats.optimizations_applied
            + pwa_stats.optimizations_applied;
    }
}

/// Offline request structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OfflineRequest {
    pub url: String,
    pub method: String,
    pub headers: HashMap<String, String>,
    pub body: Option<Vec<u8>>,
}

/// Offline response structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OfflineResponse {
    pub url: String,
    pub data: Vec<u8>,
    pub timestamp: std::time::SystemTime,
    pub source: OfflineResponseSource,
}

/// Offline response source
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum OfflineResponseSource {
    Cache,
    OfflineStorage,
    OfflineFallback,
}

impl Default for OfflineSupportConfig {
    fn default() -> Self {
        Self {
            service_worker_config: ServiceWorkerConfig::default(),
            cache_config: CacheConfig::default(),
            storage_config: OfflineStorageConfig::default(),
            sync_config: SyncConfig::default(),
            pwa_config: PwaConfig::default(),
            enable_offline_mode: true,
            auto_sync_enabled: true,
            cache_size_limit: 50 * 1024 * 1024, // 50MB
            sync_interval: 300,                 // 5 minutes
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_offline_support_config() -> OfflineSupportConfig {
        OfflineSupportConfig {
            service_worker_config: ServiceWorkerConfig::default(),
            cache_config: CacheConfig::default(),
            storage_config: OfflineStorageConfig::default(),
            sync_config: SyncConfig::default(),
            pwa_config: PwaConfig::default(),
            enable_offline_mode: true,
            auto_sync_enabled: true,
            cache_size_limit: 10 * 1024 * 1024, // 10MB for testing
            sync_interval: 60,                  // 1 minute for testing
        }
    }

    #[tokio::test]
    async fn test_offline_support_manager_creation() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_offline_requests, 0);
        assert_eq!(stats.sync_operations, 0);
        assert_eq!(stats.total_optimizations_applied, 0);
    }

    #[tokio::test]
    async fn test_offline_support_initialization() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        let result = manager.initialize().await;
        assert!(result.is_ok());

        let stats = manager.get_stats().await;
        assert!(stats.total_optimizations_applied > 0);
    }

    #[tokio::test]
    async fn test_offline_request_handling() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        let _ = manager.initialize().await;

        let request = OfflineRequest {
            url: "https://example.com/data".to_string(),
            method: "GET".to_string(),
            headers: HashMap::new(),
            body: None,
        };

        let response = manager.handle_offline_request(request).await;
        assert!(response.is_ok());

        let response = response.unwrap();
        assert_eq!(response.url, "https://example.com/data");
        assert_eq!(response.source, OfflineResponseSource::OfflineFallback);
    }

    #[tokio::test]
    async fn test_data_sync() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        let _ = manager.initialize().await;

        let result = manager.sync_data().await;
        assert!(result.is_ok());

        let stats = manager.get_stats().await;
        assert!(stats.sync_operations > 0);
    }

    #[tokio::test]
    async fn test_online_status_management() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        // Initially offline
        assert!(!manager.is_online().await);

        // Set online
        manager.set_online_status(true).await;
        assert!(manager.is_online().await);

        // Set offline
        manager.set_online_status(false).await;
        assert!(!manager.is_online().await);
    }

    #[tokio::test]
    async fn test_config_update() {
        let config = create_test_offline_support_config();
        let mut manager = OfflineSupportManager::new(config);

        let new_config = create_test_offline_support_config();
        let result = manager.update_config(new_config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_offline_support_statistics() {
        let config = create_test_offline_support_config();
        let manager = OfflineSupportManager::new(config);

        // Initialize to populate stats
        let _ = manager.initialize().await;

        let stats = manager.get_stats().await;
        assert!(stats.optimization_benefits.len() > 0);
        assert!(stats.total_optimizations_applied > 0);
    }
}