Repository navigation
Expand file tree
/
Copy pathstate.lua
More file actions
175 lines (161 loc) · 4.56 KB
/
Copy pathstate.lua
File metadata and controls
175 lines (161 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local config = require('opencode.config').get()
---@class OpencodeWindowState
---@field input_win number|nil
---@field output_win number|nil
---@field footer_win number|nil
---@field footer_buf number|nil
---@field input_buf number|nil
---@field output_buf number|nil
---@class OpencodeState
---@field windows OpencodeWindowState|nil
---@field input_content table
---@field last_focused_opencode_window string|nil
---@field last_input_window_position number|nil
---@field last_output_window_position number|nil
---@field last_code_win_before_opencode number|nil
---@field display_route any|nil
---@field current_mode string
---@field was_interrupted boolean
---@field last_output number
---@field last_sent_context any
---@field active_session Session|nil
---@field new_session_name string|nil
---@field restore_points table<string, any>
---@field current_model string|nil
---@field messages Message[]|nil
---@field current_message Message|nil
---@field cost number
---@field tokens_count number
---@field opencode_server_job OpencodeServer
---@field subscribe fun( key:string|nil, cb:fun(key:string, new_val:any, old_val:any))
---@field unsubscribe fun( key:string|nil, cb:fun(key:string, new_val:any, old_val:any))
---@field is_job_running fun():boolean
---@field append fun( key:string, value:any)
---@field required_version string
---@field opencode_cli_version string|nil
-- Internal raw state table
local _state = {
-- ui
windows = nil, ---@type OpencodeWindowState
input_content = {},
last_focused_opencode_window = nil,
last_input_window_position = nil,
last_output_window_position = nil,
last_code_win_before_opencode = nil,
display_route = nil,
current_mode = config.default_mode,
was_interrupted = false,
last_output = 0,
-- context
last_sent_context = nil,
-- session
active_session = nil,
new_session_name = nil,
restore_points = {},
current_model = nil,
-- messages
messages = nil,
current_message = nil,
cost = 0,
tokens_count = 0,
-- job
opencode_server_job = nil,
-- versions
required_version = '0.6.3',
opencode_cli_version = nil,
}
-- Listener registry: { [key] = {cb1, cb2, ...}, ['*'] = {cb1, ...} }
local _listeners = {}
--- Subscribe to changes for a key (or all keys with '*').
---@param key string|nil If nil or '*', listens to all keys
---@param cb fun(key:string, new_val:any, old_val:any)
---@usage
--- state.subscribe('foo', function(key, new, old) ... end)
--- state.subscribe('*', function(key, new, old) ... end)
local function subscribe(key, cb)
key = key or '*'
if not _listeners[key] then
_listeners[key] = {}
end
table.insert(_listeners[key], cb)
end
--- Unsubscribe a callback for a key (or all keys)
---@param key string|nil
---@param cb fun(key:string, new_val:any, old_val:any)
local function unsubscribe(key, cb)
key = key or '*'
local list = _listeners[key]
if not list then
return
end
for i, fn in ipairs(list) do
if fn == cb then
table.remove(list, i)
break
end
end
end
-- Notify listeners
local function _notify(key, new_val, old_val)
if _listeners[key] then
for _, cb in ipairs(_listeners[key]) do
pcall(cb, key, new_val, old_val)
end
end
if _listeners['*'] then
for _, cb in ipairs(_listeners['*']) do
pcall(cb, key, new_val, old_val)
end
end
end
local function append(key, value)
if type(value) ~= 'table' then
error('Value must be a table to append')
end
if not _state[key] then
_state[key] = {}
end
if type(_state[key]) ~= 'table' then
error('State key is not a table: ' .. key)
end
local old = vim.deepcopy(_state[key] --[[@as table]])
table.insert(_state[key] --[[@as table]], value)
_notify(key, _state[key], old)
end
--- Observable state proxy. All reads/writes go through this table.
--- Use `state.subscribe(key, cb)` to listen for changes.
--- Use `state.unsubscribe(key, cb)` to remove listeners.
---
--- Example:
--- state.subscribe('foo', function(key, new, old) print(key, new, old) end)
--- state.foo = 42 -- triggers callback
---@type OpencodeState
local M = {}
setmetatable(M, {
__index = function(_, k)
return _state[k]
end,
__newindex = function(_, k, v)
local old = _state[k]
_state[k] = v
if not vim.deep_equal(old, v) then
_notify(k, v, old)
end
end,
__pairs = function()
return pairs(_state)
end,
__ipairs = function()
return ipairs(_state)
end,
})
M.append = append
M.subscribe = subscribe
M.unsubscribe = unsubscribe
---
--- Returns true if any job (run or server) is running
---
function M.is_job_running()
return M.opencode_server_job ~= nil
end
return M