mirror of
https://github.com/lune-org/lune.git
synced 2024-12-13 05:20:37 +00:00
Add missing task.cancel API
This commit is contained in:
parent
3689eb17d2
commit
8ab5855ccc
6 changed files with 37 additions and 2 deletions
1
lune.yml
1
lune.yml
|
@ -74,6 +74,7 @@ globals:
|
|||
- required: false
|
||||
type: table
|
||||
# Task
|
||||
task.cancel:
|
||||
task.defer:
|
||||
args:
|
||||
- type: thread | function
|
||||
|
|
|
@ -53,6 +53,7 @@ declare process: {
|
|||
}
|
||||
|
||||
declare task: {
|
||||
cancel: (t: thread) -> (),
|
||||
defer: <A..., R...>(f: thread | (A...) -> (R...), A...) -> (R...),
|
||||
delay: <A..., R...>(duration: number?, f: thread | (A...) -> (R...), A...) -> (R...),
|
||||
spawn: <A..., R...>(f: thread | (A...) -> (R...), A...) -> (R...),
|
||||
|
|
|
@ -21,6 +21,10 @@ pub struct WaitingThread<'a> {
|
|||
pub fn new<'a>(lua: &'a Lua, _threads: &Arc<Mutex<Vec<WaitingThread<'a>>>>) -> Result<Table<'a>> {
|
||||
// TODO: Figure out how to insert into threads vec
|
||||
ReadonlyTableBuilder::new(lua)?
|
||||
.with_function("cancel", |lua, thread: Thread| {
|
||||
thread.reset(lua.create_function(|_, _: ()| Ok(()))?)?;
|
||||
Ok(())
|
||||
})?
|
||||
.with_async_function(
|
||||
"defer",
|
||||
|lua, (func, args): (Function, Variadic<Value>)| async move {
|
||||
|
@ -31,7 +35,7 @@ pub fn new<'a>(lua: &'a Lua, _threads: &Arc<Mutex<Vec<WaitingThread<'a>>>>) -> R
|
|||
)?
|
||||
.with_async_function(
|
||||
"delay",
|
||||
|lua, (func, duration, args): (Function, Option<f32>, Variadic<Value>)| async move {
|
||||
|lua, (duration, func, args): (Option<f32>, Function, Variadic<Value>)| async move {
|
||||
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
|
||||
time::sleep(Duration::from_secs_f32(secs)).await;
|
||||
let thread = lua.create_thread(func)?;
|
||||
|
|
|
@ -81,6 +81,7 @@ mod tests {
|
|||
net_request_redirect: "net/request/redirect",
|
||||
net_json_decode: "net/json/decode",
|
||||
net_json_encode: "net/json/encode",
|
||||
task_cancel: "task/cancel",
|
||||
task_defer: "task/defer",
|
||||
task_delay: "task/delay",
|
||||
task_spawn: "task/spawn",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::future::Future;
|
||||
|
||||
use mlua::{FromLuaMulti, Lua, Result, Table, ToLuaMulti};
|
||||
use mlua::{FromLuaMulti, Lua, Result, Table, ToLuaMulti, Value};
|
||||
|
||||
pub struct ReadonlyTableBuilder<'lua> {
|
||||
lua: &'lua Lua,
|
||||
|
@ -13,6 +13,11 @@ impl<'lua> ReadonlyTableBuilder<'lua> {
|
|||
Ok(Self { lua, tab })
|
||||
}
|
||||
|
||||
pub fn with_value(self, key: &'static str, value: Value) -> Result<Self> {
|
||||
self.tab.raw_set(key, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with_table(self, key: &'static str, value: Table) -> Result<Self> {
|
||||
self.tab.raw_set(key, value)?;
|
||||
Ok(self)
|
||||
|
|
23
src/tests/task/cancel.luau
Normal file
23
src/tests/task/cancel.luau
Normal file
|
@ -0,0 +1,23 @@
|
|||
-- Cancel should cancel any deferred or delayed threads
|
||||
|
||||
local flag: boolean = false
|
||||
local thread = task.defer(function()
|
||||
flag = true
|
||||
end)
|
||||
local thread2 = task.delay(0, function()
|
||||
flag = true
|
||||
end)
|
||||
task.cancel(thread)
|
||||
task.cancel(thread2)
|
||||
task.wait(0.1)
|
||||
assert(not flag, "Cancel should handle non-immediate threads")
|
||||
|
||||
-- Cancellation should be as immediate as possible
|
||||
|
||||
local flag2: number = 1
|
||||
task.spawn(function()
|
||||
flag2 = 2
|
||||
task.wait()
|
||||
flag2 = 3
|
||||
end)
|
||||
assert(flag2 == 2, "Cancel should properly handle yielding threads")
|
Loading…
Reference in a new issue