tokio-js-set-interval
The crate tokio-js-set-interval
allows you to use setInterval(callback, ms)
and
setTimeout(callback, ms)
as in Javascript inside a tokio
runtime (https://tokio.rs/).
The library provides the macros:
- set_interval!(callback, ms)
,
- set_interval_async!(future, ms)
,
- set_timeout!(callback, ms)
,
- and set_timeout_async!(async_callback, ms)
.
Cargo.toml ```toml [dependencies]
tokio-js-set-interval = "
code.rs ```rust use std::time::Duration; use tokiojssetinterval::{setinterval, settimeout, clearinterval};
async fn main() { settimeout!(println!("hello from timeout"), 25); setinterval!(println!("hello from interval"), 10); // you can clear intervals if you want let id = setinterval!(println!("hello from interval"), 10); clearinterval(id);
// give enough time before tokios runtime exits
tokio::time::sleep(Duration::from_millis(40)).await;
} ```
They behave similar to their Javascript counterparts, with a few exceptions:
tokio
runtime lives long enough.()
\
=> all actions must be done via side effectsI'm not an expert in tokio
(or async/await/futures in Rust in general) and I don't
know if this follows best practises. But it helped me to understand how tokio
works.
I hope it may be helpful to some of you too.
The functionality behind is rather simple. However, it took me some time to figure out what kind of
input the macros should accept and how the generic arguments of the functions behind the macros
need to be structured. Especially the *_async!()
versions of the macros were quite complicated
during the development.
Version 1.2.0 is developed with tokio@1.21.0
and rust@1.63.0
. The minimum supported tokio
version is 1.0.x
and the MSRV depends on the tokio
version. For the latest tokio version,
the MSRV
is 1.49.0
.