Use working wasmtimer for time::every

This commit is contained in:
Kitsu 2025-02-02 22:44:05 -03:00
parent 599d8b560b
commit 6a584af141
6 changed files with 34 additions and 79 deletions

57
Cargo.lock generated
View file

@ -2535,7 +2535,7 @@ dependencies = [
"smol", "smol",
"tokio", "tokio",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-timer", "wasmtimer",
] ]
[[package]] [[package]]
@ -4112,17 +4112,6 @@ version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba"
[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.6",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.3" version = "0.12.3"
@ -4130,21 +4119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [ dependencies = [
"lock_api", "lock_api",
"parking_lot_core 0.9.10", "parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc"
dependencies = [
"cfg-if",
"instant",
"libc",
"redox_syscall 0.2.16",
"smallvec",
"winapi",
] ]
[[package]] [[package]]
@ -4665,15 +4640,6 @@ dependencies = [
"font-types", "font-types",
] ]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.4.1" version = "0.4.1"
@ -5813,7 +5779,7 @@ dependencies = [
"serde_json", "serde_json",
"tracing-subscriber", "tracing-subscriber",
"uuid", "uuid",
"wasm-timer", "wasmtimer",
"web-sys", "web-sys",
] ]
@ -6459,18 +6425,17 @@ dependencies = [
] ]
[[package]] [[package]]
name = "wasm-timer" name = "wasmtimer"
version = "0.2.5" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be0ecb0db480561e9a7642b5d3e4187c128914e58aa84330b9493e3eb68c5e7f" checksum = "0048ad49a55b9deb3953841fa1fc5858f0efbcb7a18868c899a360269fac1b23"
dependencies = [ dependencies = [
"futures", "futures",
"js-sys", "js-sys",
"parking_lot 0.11.2", "parking_lot",
"pin-utils", "pin-utils",
"slab",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
] ]
[[package]] [[package]]
@ -6657,7 +6622,7 @@ dependencies = [
"js-sys", "js-sys",
"log", "log",
"naga", "naga",
"parking_lot 0.12.3", "parking_lot",
"profiling", "profiling",
"raw-window-handle 0.6.2", "raw-window-handle 0.6.2",
"smallvec", "smallvec",
@ -6685,7 +6650,7 @@ dependencies = [
"log", "log",
"naga", "naga",
"once_cell", "once_cell",
"parking_lot 0.12.3", "parking_lot",
"profiling", "profiling",
"raw-window-handle 0.6.2", "raw-window-handle 0.6.2",
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
@ -6725,7 +6690,7 @@ dependencies = [
"ndk-sys 0.5.0+25.2.9519653", "ndk-sys 0.5.0+25.2.9519653",
"objc", "objc",
"once_cell", "once_cell",
"parking_lot 0.12.3", "parking_lot",
"profiling", "profiling",
"range-alloc", "range-alloc",
"raw-window-handle 0.6.2", "raw-window-handle 0.6.2",

View file

@ -184,7 +184,7 @@ tracing = "0.1"
unicode-segmentation = "1.0" unicode-segmentation = "1.0"
url = "2.5" url = "2.5"
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
wasm-timer = "0.2" wasmtimer = "0.4.1"
web-sys = "0.3.69" web-sys = "0.3.69"
web-time = "1.1" web-time = "1.1"
wgpu = "23.0" wgpu = "23.0"

View file

@ -24,7 +24,7 @@ iced.features = ["debug", "webgl", "fira-sans"]
uuid = { version = "1.0", features = ["js"] } uuid = { version = "1.0", features = ["js"] }
web-sys = { workspace = true, features = ["Window", "Storage"] } web-sys = { workspace = true, features = ["Window", "Storage"] }
wasm-timer.workspace = true wasmtimer.workspace = true
[dev-dependencies] [dev-dependencies]
iced_test.workspace = true iced_test.workspace = true

View file

@ -578,7 +578,8 @@ impl SavedState {
.set_item("state", &json) .set_item("state", &json)
.map_err(|_| SaveError::Write)?; .map_err(|_| SaveError::Write)?;
let _ = wasm_timer::Delay::new(std::time::Duration::from_secs(2)).await; let _ =
wasmtimer::tokio::sleep(std::time::Duration::from_secs(2)).await;
Ok(()) Ok(())
} }

View file

@ -41,4 +41,4 @@ tokio.features = ["rt", "rt-multi-thread", "time"]
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures.workspace = true wasm-bindgen-futures.workspace = true
wasm-timer.workspace = true wasmtimer.workspace = true

View file

@ -16,41 +16,30 @@ impl crate::Executor for Executor {
pub mod time { pub mod time {
//! Listen and react to time. //! Listen and react to time.
use crate::subscription::{self, Hasher, Subscription}; use crate::subscription::Subscription;
use crate::BoxStream;
use wasmtimer::std::Instant;
/// Returns a [`Subscription`] that produces messages at a set interval. /// Returns a [`Subscription`] that produces messages at a set interval.
/// ///
/// The first message is produced after a `duration`, and then continues to /// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that. /// produce more messages every `duration` after that.
pub fn every( pub fn every(duration: std::time::Duration) -> Subscription<Instant> {
duration: std::time::Duration, Subscription::run_with(duration, |duration| {
) -> Subscription<wasm_timer::Instant> {
subscription::from_recipe(Every(duration))
}
#[derive(Debug)]
struct Every(std::time::Duration);
impl subscription::Recipe for Every {
type Output = wasm_timer::Instant;
fn hash(&self, state: &mut Hasher) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: subscription::EventStream,
) -> BoxStream<Self::Output> {
use futures::stream::StreamExt; use futures::stream::StreamExt;
wasm_timer::Interval::new(self.0) let mut interval = wasmtimer::tokio::interval(*duration);
.map(|_| wasm_timer::Instant::now()) interval.set_missed_tick_behavior(
.boxed_local() wasmtimer::tokio::MissedTickBehavior::Skip,
} );
let stream = {
futures::stream::unfold(interval, |mut interval| async move {
Some((interval.tick().await, interval))
})
};
stream.boxed()
})
} }
} }