Make wait async again

This commit is contained in:
Filip Tibell 2023-01-22 14:39:57 -05:00
parent f8a2eb79d4
commit af0c6d882e
No known key found for this signature in database

View file

@ -1,6 +1,7 @@
use std::{thread::sleep, time::Duration}; use std::time::Duration;
use mlua::{Function, Lua, Result, Table, Value}; use mlua::{Function, Lua, Result, Table, Value};
use tokio::time;
use crate::utils::table_builder::ReadonlyTableBuilder; use crate::utils::table_builder::ReadonlyTableBuilder;
@ -26,15 +27,15 @@ pub async fn new(lua: &Lua) -> Result<Table> {
.with_value("defer", Value::Function(task_defer))? .with_value("defer", Value::Function(task_defer))?
.with_value("delay", Value::Function(task_delay))? .with_value("delay", Value::Function(task_delay))?
.with_value("spawn", Value::Function(task_spawn))? .with_value("spawn", Value::Function(task_spawn))?
.with_function("wait", wait)? .with_async_function("wait", wait)?
.build() .build()
} }
// FIXME: It does seem possible to properly make an async wait // FIXME: It does seem possible to properly make an async wait
// function with mlua right now, something breaks when using // function with mlua right now, something breaks when using
// async wait functions inside of coroutines // async wait functions inside of coroutines
fn wait(_: &Lua, duration: Option<f32>) -> Result<f32> { async fn wait(_: &Lua, duration: Option<f32>) -> Result<f32> {
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION); let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
sleep(Duration::from_secs_f32(secs)); time::sleep(Duration::from_secs_f32(secs)).await;
Ok(secs) Ok(secs)
} }