From d21969cb8106b14a3f99e6a15142b3594a360b6f Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 26 Sep 2024 05:10:19 +0200 Subject: [PATCH] feat: add promise abort for lua --- dist/lua/annotations.lua | 24 ++++++++++++++++++++++++ src/ffi/lua/ext/a_sync.rs | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/dist/lua/annotations.lua b/dist/lua/annotations.lua index 04055ba..6f59f7a 100644 --- a/dist/lua/annotations.lua +++ b/dist/lua/annotations.lua @@ -18,6 +18,9 @@ local NilPromise = {} --- block until promise is ready function NilPromise:await() end +--- cancel promise execution +function NilPromise:abort() end + ---@param cb fun() callback to invoke ---invoke callback asynchronously as soon as promise is ready function NilPromise:and_then(cb) end @@ -30,6 +33,9 @@ local StringPromise = {} --- @return string function StringPromise:await() end +--- cancel promise execution +function StringPromise:abort() end + ---@param cb fun(x: string) callback to invoke ---invoke callback asynchronously as soon as promise is ready function StringPromise:and_then(cb) end @@ -40,6 +46,8 @@ local StringArrayPromise = {} --- block until promise is ready and return value --- @return string[] function StringArrayPromise:await() end +--- cancel promise execution +function StringArrayPromise:abort() end ---@param cb fun(x: string[]) callback to invoke ---invoke callback asynchronously as soon as promise is ready function StringArrayPromise:and_then(cb) end @@ -50,6 +58,8 @@ local ClientPromise = {} --- block until promise is ready and return value --- @return Client function ClientPromise:await() end +--- cancel promise execution +function ClientPromise:abort() end ---@param cb fun(x: Client) callback to invoke ---invoke callback asynchronously as soon as promise is ready function ClientPromise:and_then(cb) end @@ -60,6 +70,8 @@ local WorkspacePromise = {} --- block until promise is ready and return value --- @return Workspace function WorkspacePromise:await() end +--- cancel promise execution +function WorkspacePromise:abort() end ---@param cb fun(x: Workspace) callback to invoke ---invoke callback asynchronously as soon as promise is ready function WorkspacePromise:and_then(cb) end @@ -70,6 +82,8 @@ local WorkspaceEventPromise = {} --- block until promise is ready and return value --- @return WorkspaceEvent function WorkspaceEventPromise:await() end +--- cancel promise execution +function WorkspaceEventPromise:abort() end ---@param cb fun(x: WorkspaceEvent) callback to invoke ---invoke callback asynchronously as soon as promise is ready function WorkspaceEventPromise:and_then(cb) end @@ -80,6 +94,8 @@ local BufferControllerPromise = {} --- block until promise is ready and return value --- @return BufferController function BufferControllerPromise:await() end +--- cancel promise execution +function BufferControllerPromise:abort() end ---@param cb fun(x: BufferController) callback to invoke ---invoke callback asynchronously as soon as promise is ready function BufferControllerPromise:and_then(cb) end @@ -90,6 +106,8 @@ local CursorPromise = {} --- block until promise is ready and return value --- @return Cursor function CursorPromise:await() end +--- cancel promise execution +function CursorPromise:abort() end ---@param cb fun(x: Cursor) callback to invoke ---invoke callback asynchronously as soon as promise is ready function CursorPromise:and_then(cb) end @@ -100,6 +118,8 @@ local MaybeCursorPromise = {} --- block until promise is ready and return value --- @return Cursor | nil function MaybeCursorPromise:await() end +--- cancel promise execution +function MaybeCursorPromise:abort() end ---@param cb fun(x: Cursor | nil) callback to invoke ---invoke callback asynchronously as soon as promise is ready function MaybeCursorPromise:and_then(cb) end @@ -110,6 +130,8 @@ local TextChangePromise = {} --- block until promise is ready and return value --- @return TextChange function TextChangePromise:await() end +--- cancel promise execution +function TextChangePromise:abort() end ---@param cb fun(x: TextChange) callback to invoke ---invoke callback asynchronously as soon as promise is ready function TextChangePromise:and_then(cb) end @@ -120,6 +142,8 @@ local MaybeTextChangePromise = {} --- block until promise is ready and return value --- @return TextChange | nil function MaybeTextChangePromise:await() end +--- cancel promise execution +function MaybeTextChangePromise:abort() end ---@param cb fun(x: TextChange | nil) callback to invoke ---invoke callback asynchronously as soon as promise is ready function MaybeTextChangePromise:and_then(cb) end diff --git a/src/ffi/lua/ext/a_sync.rs b/src/ffi/lua/ext/a_sync.rs index 54bc20c..d92af72 100644 --- a/src/ffi/lua/ext/a_sync.rs +++ b/src/ffi/lua/ext/a_sync.rs @@ -51,6 +51,13 @@ impl LuaUserData for Promise { .map_err(LuaError::runtime)? }, }); + methods.add_method_mut("abort", |_, this, ()| match this.0.take() { + None => Err(LuaError::runtime("Promise already awaited")), + Some(x) => { + x.abort(); + Ok(()) + }, + }); methods.add_method_mut("and_then", |_, this, (cb,):(LuaFunction,)| match this.0.take() { None => Err(LuaError::runtime("Promise already awaited")), Some(x) => {