#timeout #set-timeout #tokio #interval #set-interval

tokio-js-set-interval

Allows you to use setInterval(callback, ms) and setTimeout(callback, ms) as in Javascript inside a tokio runtime. The library provides the macros set_interval!(callback, ms) and set_timeout!(callback, ms)

8 stable releases

1.3.0 Jun 25, 2023
1.2.1 Jun 25, 2023
1.2.0 Sep 12, 2022
1.1.0 Jun 15, 2021
1.0.3 May 17, 2021

#404 in Asynchronous

Download history 53/week @ 2024-06-14 121/week @ 2024-06-21 109/week @ 2024-06-28 97/week @ 2024-07-05 81/week @ 2024-07-12 43/week @ 2024-07-19 51/week @ 2024-07-26 72/week @ 2024-08-02 72/week @ 2024-08-09 37/week @ 2024-08-16 11/week @ 2024-08-23 11/week @ 2024-08-30 22/week @ 2024-09-06 25/week @ 2024-09-13 76/week @ 2024-09-20 30/week @ 2024-09-27

155 downloads per month

MIT license

34KB
418 lines

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).

How to use

Cargo.toml

[dependencies]
# don't forget that you also need "tokio" for the tokio runtime!
tokio-js-set-interval = "<latest-version>"

code.rs

use std::time::Duration;
use tokio_js_set_interval::{set_interval, set_timeout, clear_interval};

#[tokio::main]
async fn main() {
    set_timeout!(println!("hello from timeout"), 25);
    set_interval!(println!("hello from interval"), 10);
    // you can clear intervals if you want
    let id = set_interval!(println!("hello from interval"), 10);
    clear_interval(id);

    // give enough time before tokios runtime exits
    tokio::time::sleep(Duration::from_millis(40)).await;
}

Restrictions

They behave similar to their Javascript counterparts, with a few exceptions:

  • They only get executed if the tokio runtime lives long enough.
  • on order to compile, the callback must return the union type, i.e. ()
    => all actions must be done via side effects
  • again, there is NO GUARANTEE that the tasks will get executed
    (=> however useful and convenient for low priority background tasks and for the learning effect of course)

Trivia

I'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.

Compatibility & MSRV

  • MSRV is 1.66
  • Tokio is supported starting with version 1.0. Note that Rust >= 1.73 will deny tokio <1.18.

Dependencies

~2–7.5MB
~46K SLoC