use std::time::Duration;
#[cfg(not(target_family = "wasm"))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(target_family = "wasm")]
use web_time::{SystemTime, UNIX_EPOCH};
#[cfg(not(target_family = "wasm"))]
use tokio::time::sleep as tokio_sleep;
#[cfg(target_family = "wasm")]
use wasmtimer::tokio::sleep as wasm_sleep;
use dioxus::prelude::spawn as dioxus_spawn;
pub mod time {
use super::*;
pub fn now_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
}
pub async fn sleep(duration: Duration) {
#[cfg(not(target_family = "wasm"))]
tokio_sleep(duration).await;
#[cfg(target_family = "wasm")]
wasm_sleep(duration).await;
}
pub fn format_relative_time(timestamp: u64) -> String {
let now = now_secs();
let diff = now.saturating_sub(timestamp);
if diff < 60 {
format!("{diff}s ago")
} else if diff < 3600 {
format!("{}m ago", diff / 60)
} else {
format!("{}h ago", diff / 3600)
}
}
}
pub mod task {
use super::*;
pub fn spawn<F>(future: F)
where
F: std::future::Future<Output = ()> + 'static,
{
dioxus_spawn(future);
}
pub fn spawn_named<F>(#[allow(unused_variables)] name: &'static str, future: F)
where
F: std::future::Future<Output = ()> + 'static,
{
dioxus_spawn(async move {
crate::debug_log!("Starting task: {}", name);
future.await;
crate::debug_log!("Completed task: {}", name);
});
}
}
pub mod config {
use super::*;
pub const DEFAULT_CLEANUP_INTERVAL: Duration = Duration::from_secs(30);
pub const DEFAULT_MAX_CACHE_SIZE: usize = 1000;
pub const DEFAULT_UNUSED_THRESHOLD: Duration = Duration::from_secs(300);
}
pub use config::*;
pub use time::{format_relative_time, now_secs, sleep};