Crate tokio_js_set_interval

Source
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§

set_interval
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.
set_interval_async
Async version of set_interval. Instead of a closure, this macro accepts a non-async closure that produces futures.
set_timeout
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.
set_timeout_async
Async version of set_timeout. Instead of a closure, this macro accepts a future.

Structs§

IntervalManager
INTERNAL Used to manage intervals created by macro set_interval! Helps to assign unique IDs to intervals and stop them.

Functions§

_set_interval_spawn
INTERNAL Use macro set_interval instead!
_set_interval_spawn_async
INTERNAL Use macro set_interval_async instead!
_set_timeout
INTERNAL Use macro set_timeout instead!
_set_timeout_async
INTERNAL Use macro set_timeout instead!
clear_interval
Clears an interval that was created via set_interval!. You have to pass the unique numeric ID as parameter. Throws no error, if the ID is unknown.