Add missing minimum task wait/delay

This commit is contained in:
Filip Tibell 2023-01-23 19:59:35 -05:00
parent 1011db6372
commit 8020d93144
No known key found for this signature in database

View file

@ -11,6 +11,8 @@ use crate::utils::{
task::{run_registered_task, TaskRunMode}, task::{run_registered_task, TaskRunMode},
}; };
const MINIMUM_WAIT_OR_DELAY_DURATION: f32 = 10.0 / 1_000.0; // 10ms
pub fn create(lua: &Lua) -> LuaResult<()> { pub fn create(lua: &Lua) -> LuaResult<()> {
// HACK: There is no way to call coroutine.close directly from the mlua // HACK: There is no way to call coroutine.close directly from the mlua
// crate, so we need to fetch the function and store it in the registry // crate, so we need to fetch the function and store it in the registry
@ -109,11 +111,11 @@ async fn task_spawn<'a>(lua: &'a Lua, tof: LuaValue<'a>) -> LuaResult<LuaThread<
async fn task_wait(lua: &Lua, duration: Option<f32>) -> LuaResult<f32> { async fn task_wait(lua: &Lua, duration: Option<f32>) -> LuaResult<f32> {
let start = Instant::now(); let start = Instant::now();
run_registered_task(lua, TaskRunMode::Blocking, async move { run_registered_task(lua, TaskRunMode::Blocking, async move {
Timer::after( Timer::after(Duration::from_secs_f32(
duration duration
.map(Duration::from_secs_f32) .map(|d| d.max(MINIMUM_WAIT_OR_DELAY_DURATION))
.unwrap_or(Duration::ZERO), .unwrap_or(MINIMUM_WAIT_OR_DELAY_DURATION),
) ))
.await; .await;
Ok(()) Ok(())
}) })