Skip to content

Commit

Permalink
Replace nested callbacks with coroutines in threads widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Jan 28, 2023
1 parent e11545b commit d213b7d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lua/dap/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,34 +316,35 @@ function threads_spec.fetch_children(thread, cb)
elseif thread.threads then
cb(thread.threads)
elseif session then
local is_stopped = thread.stopped
local params = { threadId = thread.id }
local on_frames = function(err, resp)
coroutine.wrap(function()
local co = coroutine.running()
local is_stopped = thread.stopped
if not is_stopped then
session:_pause(thread.id, function(err, result)
coroutine.resume(co, err, result)
end)
coroutine.yield()
end
local params = { threadId = thread.id }
local err, resp = session:request('stackTrace', params)
if err then
utils.notify('Error fetching stackTrace: ' .. err.message, vim.log.levels.WARN)
utils.notify('Error fetching stackTrace: ' .. utils.fmt_error(err), vim.log.levels.WARN)
else
thread.frames = resp.stackFrames
if is_stopped then
cb(threads_spec.get_children(thread))
end
if not is_stopped then
local err0 = session:request('continue', params)
if err0 then
utils.notify('Error on continue: ' .. utils.fmt_error(err), vim.log.levels.WARN)
else
thread.stopped = false
session:request('continue', params, function(err0)
if err0 then
utils.notify('Error on continue: ' .. err.message, vim.log.levels.WARN)
end
cb(threads_spec.get_children(thread))
end)
local progress = require('dap.progress')
progress.report('Thread resumed: ' .. tostring(thread.id))
progress.report('Running: ' .. session.config.name)
end
end
end
local get_frames = function()
session:request('stackTrace', params, on_frames)
end
if is_stopped then
get_frames()
else
session:_pause(thread.id, get_frames)
end
cb(threads_spec.get_children(thread))
end)()
else
cb({})
end
Expand Down

0 comments on commit d213b7d

Please sign in to comment.