Skip to content

Commit e93708d

Browse files
committed
perf(event_manager): schedule every callback
1 parent 776d321 commit e93708d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lua/opencode/event_manager.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,14 @@ function EventManager:emit(event_name, data)
214214
local event = { type = event_name, properties = data }
215215

216216
if require('opencode.config').debug.capture_streamed_events then
217-
table.insert(self.captured_events, vim.deepcopy(event))
217+
vim.schedule(function()
218+
table.insert(self.captured_events, vim.deepcopy(event))
219+
end)
218220
end
219221

220222
-- schedule events to allow for similar pieces of state to be updated
221223
for _, callback in ipairs(listeners) do
222-
pcall(callback, data)
224+
pcall(vim.schedule_wrap(callback), data)
223225
end
224226
end
225227

tests/unit/event_manager_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ describe('EventManager', function()
3030

3131
event_manager:emit('test_event', { test = 'data' })
3232

33+
-- Wait for scheduled callback to execute
34+
vim.wait(100, function()
35+
return callback_called
36+
end)
37+
3338
assert.is_true(callback_called)
3439
assert.are.same({ test = 'data' }, received_data)
3540
end)
@@ -48,6 +53,11 @@ describe('EventManager', function()
4853

4954
event_manager:emit('test_event', {})
5055

56+
-- Wait for scheduled callbacks to execute
57+
vim.wait(100, function()
58+
return callback1_called and callback2_called
59+
end)
60+
5161
assert.is_true(callback1_called)
5262
assert.is_true(callback2_called)
5363
end)

0 commit comments

Comments
 (0)