dioxus_provider/
platform.rs1use std::time::Duration;
7
8#[cfg(not(target_family = "wasm"))]
10use std::time::{SystemTime, UNIX_EPOCH};
11#[cfg(target_family = "wasm")]
12use web_time::{SystemTime, UNIX_EPOCH};
13
14#[cfg(not(target_family = "wasm"))]
16use tokio::time::sleep as tokio_sleep;
17#[cfg(target_family = "wasm")]
18use wasmtimer::tokio::sleep as wasm_sleep;
19
20use dioxus::prelude::spawn as dioxus_spawn;
22
23pub mod time {
25 use super::*;
26
27 pub fn now_secs() -> u64 {
29 SystemTime::now()
30 .duration_since(UNIX_EPOCH)
31 .unwrap()
32 .as_secs()
33 }
34
35 pub async fn sleep(duration: Duration) {
37 #[cfg(not(target_family = "wasm"))]
38 tokio_sleep(duration).await;
39 #[cfg(target_family = "wasm")]
40 wasm_sleep(duration).await;
41 }
42
43 pub fn format_relative_time(timestamp: u64) -> String {
45 let now = now_secs();
46 let diff = now.saturating_sub(timestamp);
47
48 if diff < 60 {
49 format!("{diff}s ago")
50 } else if diff < 3600 {
51 format!("{}m ago", diff / 60)
52 } else {
53 format!("{}h ago", diff / 3600)
54 }
55 }
56}
57
58pub mod task {
60 use super::*;
61
62 pub fn spawn<F>(future: F)
64 where
65 F: std::future::Future<Output = ()> + 'static,
66 {
67 dioxus_spawn(future);
68 }
69
70 pub fn spawn_named<F>(#[allow(unused_variables)] name: &'static str, future: F)
72 where
73 F: std::future::Future<Output = ()> + 'static,
74 {
75 dioxus_spawn(async move {
76 crate::debug_log!("Starting task: {}", name);
77 future.await;
78 crate::debug_log!("Completed task: {}", name);
79 });
80 }
81}
82
83pub mod config {
85 use super::*;
86
87 pub const DEFAULT_CLEANUP_INTERVAL: Duration = Duration::from_secs(30);
89
90 pub const DEFAULT_MAX_CACHE_SIZE: usize = 1000;
92
93 pub const DEFAULT_UNUSED_THRESHOLD: Duration = Duration::from_secs(300);
95}
96
97pub use config::*;
98pub use time::{format_relative_time, now_secs, sleep};