Skip to content

Commit 632ec58

Browse files
committed
refactor: add types to Promise. finally() for more reliable cleanup
1 parent 3992e62 commit 632ec58

9 files changed

Lines changed: 423 additions & 278 deletions

File tree

lua/opencode/api/command.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local M = {}
2525
function M.command(command)
2626
require("opencode.cli.server")
2727
.get_port()
28-
:next(function(port)
28+
:next(function(port) ---@param port number
2929
-- No need to register SSE here - commands don't trigger any.
3030
-- (except maybe the `input_*` commands? but no reason for user to use those).
3131
require("opencode.cli.client").tui_execute_command(command, port)

lua/opencode/api/prompt.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function M.prompt(prompt, opts)
2525

2626
require("opencode.cli.server")
2727
.get_port()
28-
:next(function(port)
28+
:next(function(port) ---@param port number
2929
if opts.clear then
3030
return require("opencode.promise").new(function(resolve)
3131
require("opencode.cli.client").tui_execute_command("prompt.clear", port, function()
@@ -35,7 +35,7 @@ function M.prompt(prompt, opts)
3535
end
3636
return port
3737
end)
38-
:next(function(port)
38+
:next(function(port) ---@param port number
3939
local rendered = opts.context:render(prompt)
4040
local plaintext = opts.context.plaintext(rendered.output)
4141
return require("opencode.promise").new(function(resolve)
@@ -44,7 +44,7 @@ function M.prompt(prompt, opts)
4444
end)
4545
end)
4646
end)
47-
:next(function(port)
47+
:next(function(port) ---@param port number
4848
require("opencode.events").subscribe()
4949

5050
if opts.submit then
@@ -57,7 +57,7 @@ function M.prompt(prompt, opts)
5757
vim.notify(err, vim.log.levels.ERROR, { title = "opencode" })
5858
return true
5959
end)
60-
:next(function()
60+
:finally(function()
6161
opts.context:clear()
6262
end)
6363
end

lua/opencode/cli/client.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ end
246246
---Select session in `opencode`.
247247
---
248248
---@param port number
249-
---@param session_id number
249+
---@param session_id string
250250
function M.select_session(port, session_id)
251251
M.call(port, "/tui/select-session", "POST", { sessionID = session_id }, nil)
252252
end

lua/opencode/cli/server.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ end
202202
---3. Calling `opts.provider.start` and polling for the port.
203203
---
204204
---@param launch boolean? Whether to launch a new server if none found. Defaults to true.
205+
---@return Promise<number>
205206
function M.get_port(launch)
206207
launch = launch ~= false
207208
return require("opencode.promise").new(function(resolve, reject)

lua/opencode/events.lua

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,23 @@ function M.subscribe()
2222

2323
require("opencode.cli.server")
2424
.get_port(false)
25-
:next(function(port)
26-
require("opencode.cli.client").sse_subscribe(
27-
port,
28-
---@param response opencode.cli.client.Event
29-
function(response)
30-
heartbeat_timer:stop()
31-
heartbeat_timer:start(
32-
OPENCODE_HEARTBEAT_INTERVAL_MS + 5000,
33-
0,
34-
vim.schedule_wrap(require("opencode.events").unsubscribe)
35-
)
36-
37-
vim.api.nvim_exec_autocmds("User", {
38-
pattern = "OpencodeEvent:" .. response.type,
39-
data = {
40-
event = response,
41-
port = port,
42-
},
43-
})
44-
end
45-
)
25+
:next(function(port) ---@param port number
26+
require("opencode.cli.client").sse_subscribe(port, function(response) ---@param response opencode.cli.client.Event
27+
heartbeat_timer:stop()
28+
heartbeat_timer:start(
29+
OPENCODE_HEARTBEAT_INTERVAL_MS + 5000,
30+
0,
31+
vim.schedule_wrap(require("opencode.events").unsubscribe)
32+
)
33+
34+
vim.api.nvim_exec_autocmds("User", {
35+
pattern = "OpencodeEvent:" .. response.type,
36+
data = {
37+
event = response,
38+
port = port,
39+
},
40+
})
41+
end)
4642
end)
4743
:catch(function(err)
4844
vim.notify("Failed to subscribe to SSE: " .. err, vim.log.levels.WARN)

0 commit comments

Comments
 (0)