lune/src/lib/globals/task.rs

28 lines
769 B
Rust
Raw Normal View History

2023-01-22 19:39:57 +00:00
use std::time::Duration;
2023-01-21 18:33:33 +00:00
use mlua::{Lua, Result};
2023-01-22 19:39:57 +00:00
use tokio::time;
2023-01-21 18:33:33 +00:00
use crate::utils::table_builder::ReadonlyTableBuilder;
2023-01-21 18:33:33 +00:00
const DEFAULT_SLEEP_DURATION: f32 = 1.0 / 60.0;
pub async fn create(lua: Lua) -> Result<Lua> {
lua.globals().raw_set(
"task",
ReadonlyTableBuilder::new(&lua)?
.with_async_function("wait", task_wait)?
.build()?,
)?;
Ok(lua)
2023-01-21 18:33:33 +00:00
}
// FIXME: It does seem possible to properly make an async wait
// function with mlua right now, something breaks when using
// async wait functions inside of coroutines
async fn task_wait(_: &Lua, duration: Option<f32>) -> Result<f32> {
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
2023-01-22 19:39:57 +00:00
time::sleep(Duration::from_secs_f32(secs)).await;
Ok(secs)
}