feat: add promise abort for lua

This commit is contained in:
əlemi 2024-09-26 05:10:19 +02:00 committed by alemi.dev
parent 5e2e711b55
commit d21969cb81
2 changed files with 31 additions and 0 deletions

View file

@ -18,6 +18,9 @@ local NilPromise = {}
--- block until promise is ready --- block until promise is ready
function NilPromise:await() end function NilPromise:await() end
--- cancel promise execution
function NilPromise:abort() end
---@param cb fun() callback to invoke ---@param cb fun() callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function NilPromise:and_then(cb) end function NilPromise:and_then(cb) end
@ -30,6 +33,9 @@ local StringPromise = {}
--- @return string --- @return string
function StringPromise:await() end function StringPromise:await() end
--- cancel promise execution
function StringPromise:abort() end
---@param cb fun(x: string) callback to invoke ---@param cb fun(x: string) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function StringPromise:and_then(cb) end function StringPromise:and_then(cb) end
@ -40,6 +46,8 @@ local StringArrayPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return string[] --- @return string[]
function StringArrayPromise:await() end function StringArrayPromise:await() end
--- cancel promise execution
function StringArrayPromise:abort() end
---@param cb fun(x: string[]) callback to invoke ---@param cb fun(x: string[]) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function StringArrayPromise:and_then(cb) end function StringArrayPromise:and_then(cb) end
@ -50,6 +58,8 @@ local ClientPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return Client --- @return Client
function ClientPromise:await() end function ClientPromise:await() end
--- cancel promise execution
function ClientPromise:abort() end
---@param cb fun(x: Client) callback to invoke ---@param cb fun(x: Client) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function ClientPromise:and_then(cb) end function ClientPromise:and_then(cb) end
@ -60,6 +70,8 @@ local WorkspacePromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return Workspace --- @return Workspace
function WorkspacePromise:await() end function WorkspacePromise:await() end
--- cancel promise execution
function WorkspacePromise:abort() end
---@param cb fun(x: Workspace) callback to invoke ---@param cb fun(x: Workspace) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function WorkspacePromise:and_then(cb) end function WorkspacePromise:and_then(cb) end
@ -70,6 +82,8 @@ local WorkspaceEventPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return WorkspaceEvent --- @return WorkspaceEvent
function WorkspaceEventPromise:await() end function WorkspaceEventPromise:await() end
--- cancel promise execution
function WorkspaceEventPromise:abort() end
---@param cb fun(x: WorkspaceEvent) callback to invoke ---@param cb fun(x: WorkspaceEvent) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function WorkspaceEventPromise:and_then(cb) end function WorkspaceEventPromise:and_then(cb) end
@ -80,6 +94,8 @@ local BufferControllerPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return BufferController --- @return BufferController
function BufferControllerPromise:await() end function BufferControllerPromise:await() end
--- cancel promise execution
function BufferControllerPromise:abort() end
---@param cb fun(x: BufferController) callback to invoke ---@param cb fun(x: BufferController) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function BufferControllerPromise:and_then(cb) end function BufferControllerPromise:and_then(cb) end
@ -90,6 +106,8 @@ local CursorPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return Cursor --- @return Cursor
function CursorPromise:await() end function CursorPromise:await() end
--- cancel promise execution
function CursorPromise:abort() end
---@param cb fun(x: Cursor) callback to invoke ---@param cb fun(x: Cursor) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function CursorPromise:and_then(cb) end function CursorPromise:and_then(cb) end
@ -100,6 +118,8 @@ local MaybeCursorPromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return Cursor | nil --- @return Cursor | nil
function MaybeCursorPromise:await() end function MaybeCursorPromise:await() end
--- cancel promise execution
function MaybeCursorPromise:abort() end
---@param cb fun(x: Cursor | nil) callback to invoke ---@param cb fun(x: Cursor | nil) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function MaybeCursorPromise:and_then(cb) end function MaybeCursorPromise:and_then(cb) end
@ -110,6 +130,8 @@ local TextChangePromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return TextChange --- @return TextChange
function TextChangePromise:await() end function TextChangePromise:await() end
--- cancel promise execution
function TextChangePromise:abort() end
---@param cb fun(x: TextChange) callback to invoke ---@param cb fun(x: TextChange) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function TextChangePromise:and_then(cb) end function TextChangePromise:and_then(cb) end
@ -120,6 +142,8 @@ local MaybeTextChangePromise = {}
--- block until promise is ready and return value --- block until promise is ready and return value
--- @return TextChange | nil --- @return TextChange | nil
function MaybeTextChangePromise:await() end function MaybeTextChangePromise:await() end
--- cancel promise execution
function MaybeTextChangePromise:abort() end
---@param cb fun(x: TextChange | nil) callback to invoke ---@param cb fun(x: TextChange | nil) callback to invoke
---invoke callback asynchronously as soon as promise is ready ---invoke callback asynchronously as soon as promise is ready
function MaybeTextChangePromise:and_then(cb) end function MaybeTextChangePromise:and_then(cb) end

View file

@ -51,6 +51,13 @@ impl LuaUserData for Promise {
.map_err(LuaError::runtime)? .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() { methods.add_method_mut("and_then", |_, this, (cb,):(LuaFunction,)| match this.0.take() {
None => Err(LuaError::runtime("Promise already awaited")), None => Err(LuaError::runtime("Promise already awaited")),
Some(x) => { Some(x) => {