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
tokioruntime 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 thetokioruntime. Unlike in Javascript, it will only be executed, if after the specified time passed, thetokioruntime 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 thetokioruntime. Unlike in Javascript, it will only be executed, if after the specified time passed, thetokioruntime 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§
- Interval
Manager - 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_intervalinstead! - _set_
interval_ spawn_ async - INTERNAL Use macro
set_interval_asyncinstead! - _set_
timeout - INTERNAL Use macro
set_timeoutinstead! - _set_
timeout_ async - INTERNAL Use macro
set_timeoutinstead! - 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.