Skip to content

Commit

Permalink
Use working wasmtimer for time::every
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Feb 3, 2025
1 parent 599d8b5 commit 6a584af
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 78 deletions.
57 changes: 11 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ tracing = "0.1"
unicode-segmentation = "1.0"
url = "2.5"
wasm-bindgen-futures = "0.4"
wasm-timer = "0.2"
wasmtimer = "0.4.1"
web-sys = "0.3.69"
web-time = "1.1"
wgpu = "23.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ iced.features = ["debug", "webgl", "fira-sans"]

uuid = { version = "1.0", features = ["js"] }
web-sys = { workspace = true, features = ["Window", "Storage"] }
wasm-timer.workspace = true
wasmtimer.workspace = true

[dev-dependencies]
iced_test.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ impl SavedState {
.set_item("state", &json)
.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(())
}
Expand Down
2 changes: 1 addition & 1 deletion futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ tokio.features = ["rt", "rt-multi-thread", "time"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures.workspace = true
wasm-timer.workspace = true
wasmtimer.workspace = true
45 changes: 17 additions & 28 deletions futures/src/backend/wasm/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,30 @@ impl crate::Executor for Executor {

pub mod time {
//! Listen and react to time.
use crate::subscription::{self, Hasher, Subscription};
use crate::BoxStream;
use crate::subscription::Subscription;

use wasmtimer::std::Instant;

/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every(
duration: std::time::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;
pub fn every(duration: std::time::Duration) -> Subscription<Instant> {
Subscription::run_with(duration, |duration| {
use futures::stream::StreamExt;

std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
let mut interval = wasmtimer::tokio::interval(*duration);
interval.set_missed_tick_behavior(
wasmtimer::tokio::MissedTickBehavior::Skip,
);

fn stream(
self: Box<Self>,
_input: subscription::EventStream,
) -> BoxStream<Self::Output> {
use futures::stream::StreamExt;
let stream = {
futures::stream::unfold(interval, |mut interval| async move {
Some((interval.tick().await, interval))
})
};

wasm_timer::Interval::new(self.0)
.map(|_| wasm_timer::Instant::now())
.boxed_local()
}
stream.boxed()
})
}
}

0 comments on commit 6a584af

Please sign in to comment.