use rong::*;
use std::path::Path;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
let rong = Rong::<RongJS>::builder().shared().build().unwrap();
rong.call(|runtime, _receiver| async move {
let ctx = runtime.context();
ctx.global()
.set(
"print",
JSFunc::new(&ctx, |msg: String| println!("{}", msg)),
)
.unwrap();
rong_timer::init(&ctx)?;
let current_dir = std::env::current_dir().unwrap();
let js_file_name = "timer_script.js";
let possible_paths = [
current_dir
.join("modules/rong_timer/examples")
.join(js_file_name),
current_dir.join("examples").join(js_file_name),
current_dir.join(js_file_name),
];
let js_path = possible_paths
.iter()
.find(|path| Path::new(path).exists())
.ok_or_else(|| {
HostError::new(
rong::error::E_NOT_FOUND,
format!(
"Could not find timer_script.js in any of the expected locations.\n\
Tried:\n\
- {}\n\
- {}\n\
- {}\n\
Current directory: {}",
possible_paths[0].display(),
possible_paths[1].display(),
possible_paths[2].display(),
current_dir.display()
),
)
})?;
println!("Found JS file at: {}", js_path.display());
ctx.eval::<()>(Source::from_path(&ctx, js_path).await?)?;
println!("Timers set up. Waiting for 5 seconds...");
sleep(Duration::from_millis(5500)).await;
println!("Program ending...");
Ok(())
})
.await
.unwrap();
}