Crates.io | interval-task |
lib.rs | interval-task |
version | 0.2.0 |
source | src |
created_at | 2024-06-16 03:49:13.431679 |
updated_at | 2024-06-21 10:56:58.797188 |
description | Repeatedly execute a `Task` with fixed time delay. more than `setInterval` |
homepage | |
repository | https://github.com/ogios/interval-task |
max_upload_size | |
id | 1273254 |
size | 22,288 |
This crate provides a runner
to simulate what setInterval
does in JS which is repeatedly executing a task every given Duration
.
But since in rust we can't have that kind of flexibility like js, the runner here provides much more usage except for just pass in the function and delay.
Also provide a channel
which wraps up runner
.
Examples of executing task 120 times in 1 second
use interval_task::{
channel::{self, TASK_DONE},
runner::ExternalRunnerExt,
};
fn no_blocking() {
let (r, mut runner) = channel::new(Duration::from_micros(1_000_000 / 120));
runner.start().unwrap();
let start = Instant::now();
for _ in 0..120 {
r.recv_blocking().unwrap();
}
println!("Elapsed: {:?}", start.elapsed());
runner.close().unwrap();
}
// manually call `close`
fn external_close_example() {
use interval_task::runner::{self, ExternalRunnerExt};
let mut count = 0;
let (s, r) = async_channel::bounded(1);
let f = move || {
if count == 119 {
s.send_blocking(0).unwrap();
} else {
count += 1
}
};
let mut runner = runner::new_external_close_runner(Duration::from_micros(1_000_000 / 120));
runner.set_task(Box::new(f));
runner.start().unwrap(); // start runner
let start = Instant::now(); // start count
r.recv_blocking().unwrap(); // wait for signal from `Task`
println!("Elapsed: {:?}", start.elapsed());
runner.close().unwrap();
}
// close inside `Task` by returning `bool`
fn internal_close_example() {
use interval_task::runner::{self, InternalRunnerExt};
use std::time::{Duration, Instant};
let mut count = 0;
let mut ins = Instant::now();
let f = move || {
if count == 119 {
println!("{}", ins.elapsed().as_secs_f64());
true
} else {
if count == 0 {
ins = Instant::now();
}
count += 1;
false
}
};
let mut runner = runner::new_internal_close_runner(Duration::from_micros(1_000_000 / 120));
runner.set_task(Box::new(f));
runner.start().unwrap();
runner.join().unwrap(); // wait for `Task` close inside
}