Skip to content

Commit 0921d3d

Browse files
cameronrsudo-tee
authored andcommitted
fix(api): fix race condition in submit_input_prompt
As discussed in #95, there's a race condition if a display_route was used and now we're submitting an input prompt. To fix, we wait for the session to fully render before submitting the input
1 parent ff17557 commit 0921d3d

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lua/opencode/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function M.submit_input_prompt()
274274
-- we're displaying /help or something similar, need to clear that and refresh
275275
-- the session data before sending the command
276276
state.display_route = nil
277-
ui.render_output()
277+
ui.render_output(true)
278278
end
279279

280280
input_window.handle_submit()

lua/opencode/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function M.open(opts)
7777
-- and the windows were closed so we need to do a full refresh. This mostly happens
7878
-- when opening the window after having closed it since we're not currently clearing
7979
-- the session on api.close()
80-
ui.render_output(false)
80+
ui.render_output()
8181
end
8282
end
8383
end

lua/opencode/ui/renderer.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ local function fetch_session()
114114
return require('opencode.session').get_messages(session)
115115
end
116116

117+
---Request all of the session data from the opencode server and render it
118+
---@return Promise<OpencodeMessage[]>
117119
function M.render_full_session()
118120
if not output_window.mounted() or not state.api_client then
119-
return
121+
return Promise.new():resolve(nil)
120122
end
121123

122-
fetch_session():and_then(M._render_full_session_data)
124+
return fetch_session():and_then(M._render_full_session_data)
123125
end
124126

125127
function M._render_full_session_data(session_data)

lua/opencode/ui/ui.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,16 @@ function M.clear_output()
174174
-- state.restore_points = {}
175175
end
176176

177-
function M.render_output(_)
178-
renderer.render_full_session()
177+
---Force a full rerender of the output buffer. Should be done synchronously if
178+
---called before submitting input or doing something that might generate events
179+
---from opencode
180+
---@param synchronous? boolean If true, waits until session is fully rendered
181+
function M.render_output(synchronous)
182+
local ret = renderer.render_full_session()
183+
184+
if ret and synchronous then
185+
ret:wait()
186+
end
179187
end
180188

181189
function M.render_lines(lines)

0 commit comments

Comments
 (0)