@@ -6,7 +6,6 @@ local Promise = require('opencode.promise')
66local RenderState = require (' opencode.ui.render_state' )
77
88local 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 ()
5555end
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 = {}
11796end
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
12199function M .teardown ()
122- M ._cleanup_subscriptions ( )
100+ M .setup_subscriptions ( false )
123101 M .reset ()
124102end
125103
@@ -970,8 +948,11 @@ function M.on_focus_changed()
970948 end
971949end
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
975956end
976957
977958--- Get all actions available at a specific line
0 commit comments