diff options
| author | 2025-02-02 22:44:05 -0300 | |
|---|---|---|
| committer | 2025-02-02 22:44:05 -0300 | |
| commit | 6a584af1411b713fab52400712031bfa82bccd18 (patch) | |
| tree | cddab91bcd26390a4d78f8176e6e9c5b3774ce84 /futures | |
| parent | 599d8b560bec8036c5ddda62a7bf0a540bdec396 (diff) | |
| download | iced-6a584af1411b713fab52400712031bfa82bccd18.tar.gz iced-6a584af1411b713fab52400712031bfa82bccd18.tar.bz2 iced-6a584af1411b713fab52400712031bfa82bccd18.zip | |
Use working wasmtimer for time::every
Diffstat (limited to '')
| -rw-r--r-- | futures/Cargo.toml | 2 | ||||
| -rw-r--r-- | futures/src/backend/wasm/wasm_bindgen.rs | 45 | 
2 files changed, 18 insertions, 29 deletions
| diff --git a/futures/Cargo.toml b/futures/Cargo.toml index a6fcfde1..3984ce83 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -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 diff --git a/futures/src/backend/wasm/wasm_bindgen.rs b/futures/src/backend/wasm/wasm_bindgen.rs index f7846c01..4811e7f4 100644 --- a/futures/src/backend/wasm/wasm_bindgen.rs +++ b/futures/src/backend/wasm/wasm_bindgen.rs @@ -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() +        })      }  } | 
