Skip to content

Commit 1ff69f2

Browse files
cameronrsudo-tee
authored andcommitted
fix(server_job): promise chain
The and_then/catch handlers didn't really make sense and we don't need them anyway.
1 parent 4b901d4 commit 1ff69f2

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

lua/opencode/server_job.lua

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,42 @@ end
2727
function M.call_api(url, method, body)
2828
local call_promise = Promise.new()
2929
state.job_count = state.job_count + 1
30+
31+
local request_entry = { nil, call_promise }
32+
table.insert(M.requests, request_entry)
33+
34+
-- Remove completed promises from list, update job_count
35+
local function remove_from_requests()
36+
for i, entry in ipairs(M.requests) do
37+
if entry == request_entry then
38+
table.remove(M.requests, i)
39+
break
40+
end
41+
end
42+
state.job_count = #M.requests
43+
end
44+
3045
local opts = {
3146
url = url,
3247
method = method or 'GET',
3348
headers = { ['Content-Type'] = 'application/json' },
3449
proxy = '',
3550
callback = function(response)
51+
remove_from_requests()
3652
handle_api_response(response, function(err, result)
3753
if err then
38-
local ok, pcall_err = pcall(call_promise.reject, call_promise, err)
54+
local ok, pcall_err = pcall(function()
55+
call_promise:reject(err)
56+
end)
3957
if not ok then
4058
vim.schedule(function()
4159
vim.notify('Error while handling API error response: ' .. vim.inspect(pcall_err))
4260
end)
4361
end
4462
else
45-
local ok, pcall_err = pcall(call_promise.resolve, call_promise, result)
63+
local ok, pcall_err = pcall(function()
64+
call_promise:resolve(result)
65+
end)
4666
if not ok then
4767
vim.schedule(function()
4868
vim.notify('Error while handling API response: ' .. vim.inspect(pcall_err))
@@ -52,7 +72,10 @@ function M.call_api(url, method, body)
5272
end)
5373
end,
5474
on_error = function(err)
55-
local ok, pcall_err = pcall(call_promise.reject, call_promise, err)
75+
remove_from_requests()
76+
local ok, pcall_err = pcall(function()
77+
call_promise:reject(err)
78+
end)
5679
if not ok then
5780
vim.schedule(function()
5881
vim.notify('Error while handling API on_error: ' .. vim.inspect(pcall_err))
@@ -65,29 +88,8 @@ function M.call_api(url, method, body)
6588
opts.body = body and vim.json.encode(body) or '{}'
6689
end
6790

68-
local request_entry = { opts, call_promise }
69-
table.insert(M.requests, request_entry)
70-
71-
-- Remove completed promises from list, update job_count
72-
local function remove_from_requests()
73-
for i, entry in ipairs(M.requests) do
74-
if entry == request_entry then
75-
table.remove(M.requests, i)
76-
break
77-
end
78-
end
79-
state.job_count = #M.requests
80-
end
81-
82-
call_promise:and_then(function(result)
83-
remove_from_requests()
84-
return result
85-
end)
86-
87-
call_promise:catch(function(err)
88-
remove_from_requests()
89-
error(err)
90-
end)
91+
-- add opts to request_entry for request tracking
92+
request_entry[1] = opts
9193

9294
curl.request(opts)
9395
return call_promise
@@ -98,7 +100,7 @@ end
98100
--- @param method string|nil HTTP method (default: 'GET')
99101
--- @param body table|nil|boolean Request body (will be JSON encoded)
100102
--- @param on_chunk fun(chunk: string) Callback invoked for each chunk of data received
101-
--- @return Job job The underlying job instance
103+
--- @return table The underlying job instance
102104
function M.stream_api(url, method, body, on_chunk)
103105
local opts = {
104106
url = url,
@@ -125,7 +127,7 @@ function M.stream_api(url, method, body, on_chunk)
125127
opts.body = body and vim.json.encode(body) or '{}'
126128
end
127129

128-
return curl.request(opts)
130+
return curl.request(opts) --[[@as table]]
129131
end
130132

131133
function M.ensure_server()

0 commit comments

Comments
 (0)