Skip to content

Commit 5d578e7

Browse files
committed
refactor(renderer): clean up event subscriptions
1 parent 835acb0 commit 5d578e7

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

lua/opencode/ui/renderer.lua

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local Promise = require('opencode.promise')
66
local RenderState = require('opencode.ui.render_state')
77

88
local M = {
9-
_subscriptions = {},
109
_prev_line_count = 0,
1110
_render_state = RenderState.new(),
1211
_last_part_formatted = {
@@ -45,6 +44,7 @@ function M.reset()
4544

4645
state.messages = {}
4746
state.last_user_message = nil
47+
state.tokens_count = 0
4848

4949
if state.current_permission and state.api_client then
5050
require('opencode.api').respond_to_permission('reject')
@@ -54,27 +54,23 @@ function M.reset()
5454
trigger_on_data_rendered()
5555
end
5656

57-
---Set up all subscriptions, for both local and server events
58-
function M.setup_subscriptions(_)
59-
M._subscriptions.active_session = function(_, new, _)
60-
M.reset()
61-
if new then
62-
M.render_full_session()
63-
end
57+
---Set up event subscriptions
58+
---@param subscribe? boolean false to unsubscribe
59+
function M.setup_subscriptions(subscribe)
60+
subscribe = subscribe or true
61+
62+
if subscribe then
63+
state.subscribe('is_opencode_focused', M.on_focus_changed)
64+
state.subscribe('active_session', M.on_session_changed)
65+
else
66+
state.unsubscribe('is_opencode_focused', M.on_focus_changed)
67+
state.unsubscribe('active_session', M.on_session_changed)
6468
end
65-
state.subscribe('active_session', M._subscriptions.active_session)
66-
M._setup_event_subscriptions()
67-
end
6869

69-
---Set up server event subscriptions
70-
---@param subscribe? boolean false to unsubscribe
71-
function M._setup_event_subscriptions(subscribe)
7270
if not state.event_manager then
7371
return
7472
end
7573

76-
subscribe = subscribe or true
77-
7874
local event_subscriptions = {
7975
{ 'session.updated', M.on_session_updated },
8076
{ 'session.compacted', M.on_session_compacted },
@@ -97,29 +93,11 @@ function M._setup_event_subscriptions(subscribe)
9793
state.event_manager:unsubscribe(sub[1], sub[2])
9894
end
9995
end
100-
101-
if subscribe then
102-
state.subscribe('is_opencode_focused', M.on_focus_changed)
103-
state.subscribe('active_session', M.on_session_changed)
104-
else
105-
state.unsubscribe('is_opencode_focused', M.on_focus_changed)
106-
state.unsubscribe('active_session', M.on_session_changed)
107-
end
108-
end
109-
110-
---Unsubscribe from local state and server subscriptions
111-
function M._cleanup_subscriptions()
112-
M._setup_event_subscriptions(false)
113-
for key, cb in pairs(M._subscriptions) do
114-
state.unsubscribe(key, cb)
115-
end
116-
M._subscriptions = {}
11796
end
11897

119-
---Clean up and teardown renderer. Unsubscribes from all
120-
---events, local state and server
98+
---Clean up and teardown renderer. Unsubscribes from all events
12199
function M.teardown()
122-
M._cleanup_subscriptions()
100+
M.setup_subscriptions(false)
123101
M.reset()
124102
end
125103

@@ -970,8 +948,11 @@ function M.on_focus_changed()
970948
end
971949
end
972950

973-
function M.on_session_changed()
974-
state.tokens_count = 0
951+
function M.on_session_changed(_, new, _)
952+
M.reset()
953+
if new then
954+
M.render_full_session()
955+
end
975956
end
976957

977958
---Get all actions available at a specific line

lua/opencode/ui/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function M.create_windows()
9999
footer.setup(windows)
100100
topbar.setup()
101101

102-
renderer.setup_subscriptions(windows)
102+
renderer.setup_subscriptions()
103103

104104
autocmds.setup_autocmds(windows)
105105
autocmds.setup_resize_handler(windows)

tests/helpers.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ function M.replay_setup()
2626
-- we use the event manager to dispatch events
2727
require('opencode.event_manager').setup()
2828

29-
-- we don't change any changes on session
30-
renderer._cleanup_subscriptions()
31-
32-
-- but we do want event_manager subscriptions so set those back up
33-
renderer._setup_event_subscriptions()
29+
renderer.setup_subscriptions()
3430

3531
renderer.reset()
3632

0 commit comments

Comments
 (0)