Skip to content

Commit ce61b67

Browse files
committed
test(replay): refactor to use defer_fn
Was seeing some potential weirdness with the uv timer so switching to defer_fn.
1 parent be76486 commit ce61b67

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

tests/manual/renderer_replay.lua

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ local helpers = require('tests.helpers')
44
local output_window = require('opencode.ui.output_window')
55
local config = require('opencode.config')
66

7+
local topbar = require('opencode.ui.topbar')
8+
local footer = require('opencode.ui.footer')
9+
local loading_animation = require('opencode.ui.loading_animation')
10+
711
local M = {}
812

913
M.events = {}
1014
M.current_index = 0
11-
M.timer = nil
15+
M.stop = false
1216
M.last_loaded_file = nil
1317
M.headless_mode = false
1418

@@ -67,31 +71,29 @@ function M.emit_event(event)
6771

6872
local index = M.current_index
6973
local count = #M.events
70-
vim.schedule(function()
71-
local id = event.properties.info and event.properties.info.id
72-
or event.properties.part and event.properties.part.id
73-
or event.properties.id
74-
or event.properties.permissionID
75-
or event.properties.partID
76-
or event.properties.messageID
77-
or ''
78-
vim.notify(
79-
'Event '
80-
.. index
81-
.. '/'
82-
.. count
83-
.. ': '
84-
.. event.type
85-
.. ' '
86-
.. id
87-
.. ' lines_set: '
88-
.. output_window._lines_set
89-
.. ' set_calls: '
90-
.. output_window._set_calls,
91-
vim.log.levels.INFO
92-
)
93-
helpers.replay_event(event)
94-
end)
74+
local id = event.properties.info and event.properties.info.id
75+
or event.properties.part and event.properties.part.id
76+
or event.properties.id
77+
or event.properties.permissionID
78+
or event.properties.partID
79+
or event.properties.messageID
80+
or ''
81+
vim.notify(
82+
'Event '
83+
.. index
84+
.. '/'
85+
.. count
86+
.. ': '
87+
.. event.type
88+
.. ' '
89+
.. id
90+
.. ' lines_set: '
91+
.. output_window._lines_set
92+
.. ' set_calls: '
93+
.. output_window._set_calls,
94+
vim.log.levels.INFO
95+
)
96+
helpers.replay_event(event)
9597
end
9698

9799
function M.replay_next(steps)
@@ -118,6 +120,7 @@ function M.replay_next(steps)
118120
end
119121

120122
function M.replay_all(delay_ms)
123+
M.stop = false
121124
if #M.events == 0 then
122125
M.load_events()
123126
else
@@ -126,51 +129,58 @@ function M.replay_all(delay_ms)
126129

127130
delay_ms = delay_ms or 50
128131

129-
if M.timer then
130-
---@diagnostic disable-next-line: undefined-field
131-
M.timer:stop()
132-
M.timer = nil
133-
end
134-
135132
if delay_ms == 0 then
136133
M.replay_next(#M.events)
134+
vim.notify(
135+
'Renders: footer: '
136+
.. footer.render_calls
137+
.. ' topbar: '
138+
.. topbar.render_calls
139+
.. ' animation:'
140+
.. loading_animation.render_calls
141+
)
137142
return
138143
end
139144

140145
state.job_count = 1
141146

142-
M.timer = vim.loop.new_timer()
143-
---@diagnostic disable-next-line: undefined-field
144-
M.timer:start(0, delay_ms, function()
147+
local function tick()
145148
if M.current_index >= #M.events then
146-
if M.timer then
147-
---@diagnostic disable-next-line: undefined-field
148-
M.timer:stop()
149-
M.timer = nil
150-
end
151149
state.job_count = 0
150+
vim.notify(
151+
('Renders: footer: %d topbar: %d animation: %d'):format(
152+
footer.render_calls,
153+
topbar.render_calls,
154+
loading_animation.render_calls
155+
)
156+
)
157+
152158
if M.headless_mode then
153159
M.dump_buffer_and_quit()
154160
end
155161
return
156162
end
157163

164+
if M.stop then
165+
M.stop = false
166+
state.job_count = 0
167+
vim.notify('Replay stopped at event ' .. M.current_index .. '/' .. #M.events, vim.log.levels.INFO)
168+
return
169+
end
170+
158171
M.replay_next()
159-
end)
172+
173+
vim.defer_fn(tick, delay_ms)
174+
end
175+
176+
vim.defer_fn(tick, delay_ms)
160177
end
161178

162179
function M.replay_stop()
163-
if M.timer then
164-
---@diagnostic disable-next-line: undefined-field
165-
M.timer:stop()
166-
M.timer = nil
167-
state.job_count = 0
168-
vim.notify('Replay stopped at event ' .. M.current_index .. '/' .. #M.events, vim.log.levels.INFO)
169-
end
180+
M.stop = true
170181
end
171182

172183
function M.reset()
173-
M.replay_stop()
174184
M.current_index = 0
175185
M.clear()
176186
end
@@ -180,7 +190,7 @@ function M.show_status()
180190
'Replay Status:\n Events loaded: %d\n Current index: %d\n Playing: %s',
181191
#M.events,
182192
M.current_index,
183-
M.timer and 'yes' or 'no'
193+
not M.stop
184194
)
185195
vim.notify(status, vim.log.levels.INFO)
186196
end

0 commit comments

Comments
 (0)