Expand description

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

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

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

Version 1.2.0 is developed with:

  • tokio @ 1.6.0 (but should also work with 1.0.0)
  • rustc @ 1.52.1 (but should also work with 1.45.2)

Macros

  • Creates a timeout that behaves similar to setInterval(callback, ms) in Javascript for the tokio runtime. Unlike in Javascript, it will only be executed, if after the specified time passed, the tokio runtime still lives, i.e. didn’t got dropped.
  • Async version of set_interval. Instead of a closure, this macro accepts a non-async closure that produces futures.
  • Creates a timeout that behaves similar to setTimeout(callback, ms) in Javascript for the tokio runtime. Unlike in Javascript, it will only be executed, if after the specified time passed, the tokio runtime still lives, i.e. didn’t get dropped.
  • Async version of set_timeout. Instead of a closure, this macro accepts a future.

Structs

Functions