Skip to content

Commit 097874c

Browse files
committed
fix(server_job): set job_count to #M.requests
I've still seen occasional issues where job_count is either too high or too low. I think this is the most correct way to set it. Also, have to wrap notifys since we're in a fast context at that point.
1 parent 5e883c4 commit 097874c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lua/opencode/server_job.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,46 @@ function M.call_api(url, method, body)
3737
if err then
3838
local ok, pcall_err = pcall(call_promise.reject, call_promise, err)
3939
if not ok then
40-
vim.notify('Error while handling API error response: ' .. vim.inspect(pcall_err))
40+
vim.schedule(function()
41+
vim.notify('Error while handling API error response: ' .. vim.inspect(pcall_err))
42+
end)
4143
end
42-
state.job_count = state.job_count - 1
4344
else
4445
local ok, pcall_err = pcall(call_promise.resolve, call_promise, result)
4546
if not ok then
46-
vim.notify('Error while handling API response: ' .. vim.inspect(pcall_err))
47+
vim.schedule(function()
48+
vim.notify('Error while handling API response: ' .. vim.inspect(pcall_err))
49+
end)
4750
end
48-
state.job_count = state.job_count - 1
4951
end
5052
end)
5153
end,
5254
on_error = function(err)
5355
local ok, pcall_err = pcall(call_promise.reject, call_promise, err)
5456
if not ok then
55-
vim.notify('Error while handling API on_error: ' .. vim.inspect(pcall_err))
57+
vim.schedule(function()
58+
vim.notify('Error while handling API on_error: ' .. vim.inspect(pcall_err))
59+
end)
5660
end
57-
state.job_count = state.job_count - 1
5861
end,
5962
}
6063

6164
if body ~= nil then
6265
opts.body = body and vim.json.encode(body) or '{}'
6366
end
6467

65-
-- For promise tracking, remove promises that complete from requests
66-
-- NOTE: can remove the request tracking code when we're happy with
67-
-- request reliability
6868
local request_entry = { opts, call_promise }
6969
table.insert(M.requests, request_entry)
7070

71+
-- Remove completed promises from list, update job_count
7172
local function remove_from_requests()
7273
for i, entry in ipairs(M.requests) do
7374
if entry == request_entry then
7475
table.remove(M.requests, i)
7576
break
7677
end
7778
end
79+
state.job_count = #M.requests
7880
end
7981

8082
call_promise:and_then(function(result)

0 commit comments

Comments
 (0)