Skip to content

Commit 6a584af

Browse files
committed
Use working wasmtimer for time::every
1 parent 599d8b5 commit 6a584af

File tree

6 files changed

+33
-78
lines changed

6 files changed

+33
-78
lines changed

Cargo.lock

Lines changed: 11 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ tracing = "0.1"
184184
unicode-segmentation = "1.0"
185185
url = "2.5"
186186
wasm-bindgen-futures = "0.4"
187-
wasm-timer = "0.2"
187+
wasmtimer = "0.4.1"
188188
web-sys = "0.3.69"
189189
web-time = "1.1"
190190
wgpu = "23.0"

examples/todos/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ iced.features = ["debug", "webgl", "fira-sans"]
2424

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

2929
[dev-dependencies]
3030
iced_test.workspace = true

examples/todos/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@ impl SavedState {
578578
.set_item("state", &json)
579579
.map_err(|_| SaveError::Write)?;
580580

581-
let _ = wasm_timer::Delay::new(std::time::Duration::from_secs(2)).await;
581+
let _ =
582+
wasmtimer::tokio::sleep(std::time::Duration::from_secs(2)).await;
582583

583584
Ok(())
584585
}

futures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ tokio.features = ["rt", "rt-multi-thread", "time"]
4141

4242
[target.'cfg(target_arch = "wasm32")'.dependencies]
4343
wasm-bindgen-futures.workspace = true
44-
wasm-timer.workspace = true
44+
wasmtimer.workspace = true

futures/src/backend/wasm/wasm_bindgen.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,30 @@ impl crate::Executor for Executor {
1616

1717
pub mod time {
1818
//! Listen and react to time.
19-
use crate::subscription::{self, Hasher, Subscription};
20-
use crate::BoxStream;
19+
use crate::subscription::Subscription;
20+
21+
use wasmtimer::std::Instant;
2122

2223
/// Returns a [`Subscription`] that produces messages at a set interval.
2324
///
2425
/// The first message is produced after a `duration`, and then continues to
2526
/// produce more messages every `duration` after that.
26-
pub fn every(
27-
duration: std::time::Duration,
28-
) -> Subscription<wasm_timer::Instant> {
29-
subscription::from_recipe(Every(duration))
30-
}
31-
32-
#[derive(Debug)]
33-
struct Every(std::time::Duration);
34-
35-
impl subscription::Recipe for Every {
36-
type Output = wasm_timer::Instant;
37-
38-
fn hash(&self, state: &mut Hasher) {
39-
use std::hash::Hash;
27+
pub fn every(duration: std::time::Duration) -> Subscription<Instant> {
28+
Subscription::run_with(duration, |duration| {
29+
use futures::stream::StreamExt;
4030

41-
std::any::TypeId::of::<Self>().hash(state);
42-
self.0.hash(state);
43-
}
31+
let mut interval = wasmtimer::tokio::interval(*duration);
32+
interval.set_missed_tick_behavior(
33+
wasmtimer::tokio::MissedTickBehavior::Skip,
34+
);
4435

45-
fn stream(
46-
self: Box<Self>,
47-
_input: subscription::EventStream,
48-
) -> BoxStream<Self::Output> {
49-
use futures::stream::StreamExt;
36+
let stream = {
37+
futures::stream::unfold(interval, |mut interval| async move {
38+
Some((interval.tick().await, interval))
39+
})
40+
};
5041

51-
wasm_timer::Interval::new(self.0)
52-
.map(|_| wasm_timer::Instant::now())
53-
.boxed_local()
54-
}
42+
stream.boxed()
43+
})
5544
}
5645
}

0 commit comments

Comments
 (0)