Use of async_std::task::sleep instead of tokio::time::sleep in examples (#2912)
* Use of async_std::task::sleep instead of tokio::time::sleep * Make the clock example run on wasm * Add control_focus and eval examples to Cargo.toml * Use web-time on desktop; It just falls back to std on non-wasm platforms --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
1 parent
ffb5c98449
commit
1dfa1b5e7f
9 files changed
+47
-26
No files matched your search
+5
-7
@@ -1,14 +1,14 @@
|
||||
//! A simple little clock that updates the time every few milliseconds.
|
||||
//!
|
||||
//! Neither Rust nor Tokio have an interval function, so we just sleep until the next update.
|
||||
//! Tokio timer's don't work on WASM though, so you'll need to use a slightly different approach if you're targeting the web.
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use tokio::time::sleep;
|
||||
use web_time::Instant;
|
||||
|
||||
const STYLE: &str = asset!("./examples/assets/clock.css");
|
||||
|
||||
fn main() {
|
||||
launch_desktop(app);
|
||||
launch(app);
|
||||
}
|
||||
|
||||
fn app() -> Element {
|
||||
@@ -16,12 +16,10 @@ fn app() -> Element {
|
||||
|
||||
use_future(move || async move {
|
||||
// Save our initial timea
|
||||
let start = std::time::Instant::now();
|
||||
let start = Instant::now();
|
||||
|
||||
loop {
|
||||
// In lieu of an interval, we just sleep until the next update
|
||||
let now = tokio::time::Instant::now();
|
||||
tokio::time::sleep_until(now + std::time::Duration::from_millis(27)).await;
|
||||
sleep(std::time::Duration::from_millis(27)).await;
|
||||
|
||||
// Update the time, using a more precise approach of getting the duration since we started the timer
|
||||
millis.set(start.elapsed().as_millis() as i64);
|
||||
|
||||
Reference in new issue
Block a user