Merge pull request #31 from hexedtech/feat/lua-promise-abort

feat(lua): cancel promise
This commit is contained in:
frelodev 2024-10-01 18:57:53 +02:00 committed by GitHub
commit b585708f8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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:cancel() 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

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