refactor: extract sleep_ms to utils/time.rs, deduplicate in hooks and skeletons
This commit is contained in:
parent
ce14c476b5
commit
9da060ac32
@ -1,25 +1,10 @@
|
||||
use dioxus::prelude::*;
|
||||
use crate::utils::time::sleep_ms;
|
||||
|
||||
/// 骨架屏 pulse 动画延迟(毫秒)
|
||||
/// 加载时间低于此值时骨架屏只显示静态灰色块,避免 pulse 动画一闪而过
|
||||
const SKELETON_PULSE_DELAY_MS: u32 = 200;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
async fn sleep_ms(ms: u32) {
|
||||
use wasm_bindgen::JsCast;
|
||||
let js_code = format!("new Promise(r => setTimeout(r, {}))", ms);
|
||||
if let Ok(promise_val) = js_sys::eval(&js_code) {
|
||||
if let Ok(promise) = promise_val.dyn_into::<js_sys::Promise>() {
|
||||
let _ = wasm_bindgen_futures::JsFuture::from(promise).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
async fn sleep_ms(ms: u32) {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(ms as u64)).await;
|
||||
}
|
||||
|
||||
/// 延迟 pulse 动画的骨架屏包装组件
|
||||
///
|
||||
/// 骨架屏区域**立即显示**(灰色静态占位块),避免空白闪烁。
|
||||
|
||||
@ -1,24 +1,9 @@
|
||||
use dioxus::prelude::*;
|
||||
use crate::utils::time::sleep_ms;
|
||||
|
||||
/// 骨架屏最小显示延迟(毫秒)。加载时间低于此值时不会显示骨架屏,避免闪烁。
|
||||
pub const MIN_SKELETON_DELAY_MS: u32 = 200;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
async fn sleep_ms(ms: u32) {
|
||||
use wasm_bindgen::JsCast;
|
||||
let js_code = format!("new Promise(r => setTimeout(r, {}))", ms);
|
||||
if let Ok(promise_val) = js_sys::eval(&js_code) {
|
||||
if let Ok(promise) = promise_val.dyn_into::<js_sys::Promise>() {
|
||||
let _ = wasm_bindgen_futures::JsFuture::from(promise).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
async fn sleep_ms(ms: u32) {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(ms as u64)).await;
|
||||
}
|
||||
|
||||
/// 延迟加载状态 Hook。
|
||||
///
|
||||
/// 当 `is_loading` 返回 true 时,延迟 `MIN_SKELETON_DELAY_MS` 毫秒后才返回 true;
|
||||
|
||||
@ -11,6 +11,7 @@ mod pages;
|
||||
mod router;
|
||||
mod tasks;
|
||||
mod theme;
|
||||
mod utils;
|
||||
|
||||
fn main() {
|
||||
#[cfg(feature = "server")]
|
||||
|
||||
2
src/utils/mod.rs
Normal file
2
src/utils/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod time;
|
||||
pub mod text;
|
||||
1
src/utils/text.rs
Normal file
1
src/utils/text.rs
Normal file
@ -0,0 +1 @@
|
||||
// Placeholder for text utilities (will be created in a later task)
|
||||
15
src/utils/time.rs
Normal file
15
src/utils/time.rs
Normal file
@ -0,0 +1,15 @@
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub async fn sleep_ms(ms: u32) {
|
||||
use wasm_bindgen::JsCast;
|
||||
let js_code = format!("new Promise(r => setTimeout(r, {}))", ms);
|
||||
if let Ok(promise_val) = js_sys::eval(&js_code) {
|
||||
if let Ok(promise) = promise_val.dyn_into::<js_sys::Promise>() {
|
||||
let _ = wasm_bindgen_futures::JsFuture::from(promise).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub async fn sleep_ms(ms: u32) {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(ms as u64)).await;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user