1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/* MIT License Copyright (c) 2021 Philipp Schuster Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //! 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/). //! For this, it offers the macros `set_interval!(callback, ms)` and `set_timeout!(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 \ //! (--> but useful/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 itself is really simple. The biggest part are the convenient macros. //! I over-engineered them a little to learn more about macros. //! //! ## Compatibility //! Version 1.0.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) use std::collections::HashSet; use std::sync::atomic::AtomicU64; use std::sync::Mutex; use lazy_static::lazy_static; use tokio::time::Duration; /// **INTERNAL** Use macro [`set_timeout`] instead! /// /// Creates a future that glues the tokio sleep function (timeout) together with /// the provided callback. pub async fn _set_timeout(f: impl Fn(), ms: u64) { tokio::time::sleep(Duration::from_millis(ms)).await; f(); } /// **INTERNAL** Used to manage intervals created by macro [`set_interval`]! /// Helps to assign unique IDs to intervals and stop them. pub struct IntervalManager { /// Monotonic incrementing counter from 0 to max value used for interval IDs. pub counter: AtomicU64, /// Contains only the IDs of dispatched and not cleared intervals. pub running_intervals: Mutex<HashSet<u64>>, } lazy_static! { /// **INTERNAL** Used to manage intervals created by macro [`set_interval`]! pub static ref INTERVAL_MANAGER: IntervalManager = IntervalManager { counter: AtomicU64::new(0), running_intervals: Mutex::new(HashSet::new()) }; } /// Creates a future that glues the tokio interval function together with /// the provided callback. Helper function for [`_set_interval_spawn`]. async fn _set_interval(f: impl Fn() + Send + 'static, ms: u64, id: u64) { // own scope -> early drop lock { let mut map = INTERVAL_MANAGER.running_intervals.lock().unwrap(); map.insert(id); } let mut int = tokio::time::interval(Duration::from_millis(ms)); // the first tick in tokios interval returns immediately. Because we want behaviour // similar to Javascript in this library, we work around this. int.tick().await; // this looks like it runs for ever, but this is not how tokio works // at least with tokio 1.6.0 and Rust 1.52 this stops executing // when the tokio runtime gets dropped. loop { int.tick().await; // breaks the loop let map = INTERVAL_MANAGER.running_intervals.lock().unwrap(); if !map.contains(&id) { break; } f(); } } /// **INTERNAL** Use macro [`set_interval`] instead! /// /// Acquires a new ID, creates an interval future and returns the ID. The future gets /// dispatched as tokio task. pub fn _set_interval_spawn(f: impl Fn() + Send + 'static, ms: u64) -> u64 { let id = INTERVAL_MANAGER .counter .fetch_add(1, std::sync::atomic::Ordering::SeqCst); tokio::spawn(_set_interval(f, ms, id)); id } /// 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. pub fn clear_interval(id: u64) { let mut set = INTERVAL_MANAGER.running_intervals.lock().unwrap(); if set.contains(&id) { set.remove(&id); } } /// 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 got dropped. /// /// As in Javascript, a timeout may only have side effects and no return type. /// You don't get a handle to manually wait for it, you must ensure, that the tokio /// runtime lives long enough. /// /// # Parameters /// * `#1` expression, closure-expression, block or identifier (which points to a closure). /// The code that represents the callback function. /// * `#2` time delay in milliseconds /// /// # Example /// ```rust /// use tokio::time::Duration; /// use tokio_js_set_interval::set_timeout; /// /// #[tokio::main] /// async fn main() { /// set_timeout!(println!("hello1"), 0); /// println!("hello2"); /// // prevent that tokios runtime gets dropped too early /// // order of output should be /// // "hello2" /// // "hello1" /// tokio::time::sleep(Duration::from_millis(1)).await; /// } /// ``` #[macro_export] macro_rules! set_timeout { // match for identifier, i.e. a closure, that is behind a variable ($cb:ident, $ms:literal) => { tokio::spawn($crate::_set_timeout($cb, $ms)); }; // match for direct closure expression (|| $cb:expr, $ms:literal) => { tokio::spawn($crate::_set_timeout(|| $cb, $ms)); }; // match for direct move closure expression (move || $cb:expr, $ms:literal) => { tokio::spawn($crate::_set_timeout(move || $cb, $ms)); }; // match for expr, like `set_timeout!(println!())` ($cb:expr, $ms:literal) => { $crate::set_timeout!(|| $cb, $ms); }; // match for block ($cb:block, $ms:literal) => { $crate::set_timeout!(|| $cb, $ms); }; } /// 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. /// /// As in Javascript, an interval may only have side effects and no return type. /// You don't get a handle to manually wait for it, you must ensure, that the tokio /// runtime lives long enough. /// /// The macro returns an numeric ID. Similar to Javascript, you can use thie ID to /// clear/stop intervals with [`clear_interval`]. /// /// # Parameters /// * `#1` expression, closure-expression, block or identifier (which points to a closure). /// The code that represents the callback function. /// * `#2` time delay in milliseconds /// /// # Example /// ```rust /// use tokio::time::Duration; /// use tokio_js_set_interval::set_interval; /// /// #[tokio::main] /// async fn main() { /// set_interval!(println!("hello1"), 50); /// // If you want to clear the interval later: save the ID /// // let id = set_interval!(println!("hello1"), 50); /// println!("hello2"); /// // prevent that tokios runtime gets dropped too early /// // "hello1" should get printed 2 times (50*2 == 100 < 120) /// tokio::time::sleep(Duration::from_millis(120)).await; /// } /// ``` #[macro_export] macro_rules! set_interval { // match for identifier, i.e. a closure, that is behind a variable ($cb:ident, $ms:literal) => { // not so nice, need to wrap the identifier in another closure $crate::set_interval!(move || $cb(), $ms) }; // match for direct closure expression (|| $cb:expr, $ms:literal) => { $crate::_set_interval_spawn(|| $cb, $ms) }; // match for direct move closure expression (move || $cb:expr, $ms:literal) => { $crate::_set_interval_spawn(move || $cb, $ms) }; // match for expr, like `set_interval!(println!())` ($cb:expr, $ms:literal) => { $crate::set_interval!(|| $cb, $ms); }; // match for block ($cb:block, $ms:literal) => { $crate::set_interval!(|| $cb, $ms); }; } #[cfg(test)] mod tests { use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; use super::*; #[tokio::test] async fn test_set_timeout_macro_all_argument_variants_builds() { // macro takes expression set_timeout!(println!("hello1"), 4); // macro takes block set_timeout!({ println!("hello2") }, 3); // macro takes direct closure expressions set_timeout!(|| println!("hello3"), 2); // macro takes direct move closure expressions set_timeout!(move || println!("hello4"), 2); // macro takes identifiers (which must point to closures) let closure = || println!("hello5"); set_timeout!(closure, 1); } #[tokio::test] async fn test_set_interval_macro_all_argument_variants_builds() { // macro takes expression set_interval!(println!("hello1"), 4); // macro takes block set_interval!({ println!("hello2") }, 3); // macro takes direct closure expressions set_interval!(|| println!("hello3"), 2); // macro takes direct move closure expressions set_interval!(move || println!("hello4"), 2); // macro takes identifiers (which must point to closures) let closure = || println!("hello5"); set_interval!(closure, 1); } /// Test can't been automated because the test is correct /// if stdout is correct. Its just a visual test which can /// be executed manually. /// Output should be /// ```text /// hello1 /// hello3 /// hello5 /// hello2 /// hello4 /// ``` #[tokio::test] async fn test_set_timeout_visual() { println!("hello1"); set_timeout!(println!("hello2"), 0); println!("hello3"); set_timeout!(println!("hello4"), 0); println!("hello5"); // give enough execution time, before tokio gets dropped // two tokio sleeps: timeouts will usually get scheduled in between tokio::time::sleep(Duration::from_millis(1)).await; tokio::time::sleep(Duration::from_millis(1)).await; } /// Test can't been automated because the test is correct /// if stdout is correct. Its just a visual test which can /// be executed manually. #[tokio::test] async fn test_set_interval_visual() { let id = set_interval!(println!("should be printed 3 times"), 50); // give the timeout enough execution time tokio::time::sleep(Duration::from_millis(151)).await; tokio::time::sleep(Duration::from_millis(0)).await; println!("interval id is: {}", id); } #[tokio::test] async fn test_set_timeout() { let counter = Arc::new(AtomicU64::new(0)); { let counter = counter.clone(); // the block is required to change the return type to "()" set_timeout!( move || { counter.fetch_add(1, Ordering::SeqCst); }, 50 ); } { let counter = counter.clone(); // the block is required to change the return type to "()" set_timeout!( move || { counter.fetch_add(1, Ordering::SeqCst); }, 50 ); } // give the tokio runtime enough execution time tokio::time::sleep(Duration::from_millis(110)).await; assert_eq!(counter.load(Ordering::SeqCst), 2); } #[tokio::test] async fn test_set_interval() { let counter = Arc::new(AtomicU64::new(0)); { let counter = counter.clone(); // the block is required to change the return type to "()" set_interval!( move || { counter.fetch_add(1, Ordering::SeqCst); }, 50 ); } // give the tokio runtime enough execution time to execute the interval 3 times tokio::time::sleep(Duration::from_millis(180)).await; assert_eq!(counter.load(Ordering::SeqCst), 3); } #[tokio::test] async fn test_clear_interval() { let counter = Arc::new(AtomicU64::new(0)); let interval_id; { let counter = counter.clone(); // the block is required to change the return type to "()" interval_id = set_interval!( move || { counter.fetch_add(1, Ordering::SeqCst); }, 50 ); } // give the tokio runtime enough execution time to execute the interval 3 times tokio::time::sleep(Duration::from_millis(180)).await; assert_eq!(counter.load(Ordering::SeqCst), 3); clear_interval(interval_id); tokio::time::sleep(Duration::from_millis(200)).await; assert_eq!(counter.load(Ordering::SeqCst), 3); } }