2023-01-22 19:39:57 +00:00
|
|
|
use std::time::Duration;
|
2023-01-21 18:33:33 +00:00
|
|
|
|
2023-01-22 20:23:56 +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
|
|
|
|
2023-01-21 20:48:56 +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;
|
|
|
|
|
2023-01-22 20:23:56 +00:00
|
|
|
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
|
|
|
}
|
2023-01-22 01:11:17 +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
|
2023-01-22 20:23:56 +00:00
|
|
|
async fn task_wait(_: &Lua, duration: Option<f32>) -> Result<f32> {
|
2023-01-22 01:11:17 +00:00
|
|
|
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
|
2023-01-22 19:39:57 +00:00
|
|
|
time::sleep(Duration::from_secs_f32(secs)).await;
|
2023-01-22 01:11:17 +00:00
|
|
|
Ok(secs)
|
|
|
|
}
|