Skip to content

Commit 5429155

Browse files
committed
feat(store): add batched state updates and mutate API
1 parent ab440a5 commit 5429155

16 files changed

Lines changed: 248 additions & 148 deletions

File tree

lua/opencode/context.lua

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ local toggleable_context_keys = {
2222
---@param context_key OpencodeToggleableContextKey
2323
---@return table
2424
local function ensure_context_state(context_key)
25-
local current_config = state.current_context_config or {}
26-
local current = current_config[context_key]
27-
local new_config = vim.deepcopy(current_config)
2825
local defaults = vim.tbl_get(config, 'context', context_key) or {}
29-
30-
new_config[context_key] = vim.tbl_deep_extend('force', {}, defaults, current or {})
31-
state.context.set_current_context_config(new_config)
32-
return new_config[context_key]
26+
return state.context.update_current_context_config(function(current_config)
27+
current_config[context_key] = vim.tbl_deep_extend('force', {}, defaults, current_config[context_key] or {})
28+
end)[context_key]
3329
end
3430

3531
M.ChatContext = ChatContext
@@ -200,12 +196,7 @@ function M.build_inline_selection_text(range)
200196
end
201197

202198
local filetype = vim.bo[buf].filetype or ''
203-
local text = string.format(
204-
'**`%s`**\n\n```%s\n%s\n```',
205-
file.path,
206-
filetype,
207-
current_selection.text
208-
)
199+
local text = string.format('**`%s`**\n\n```%s\n%s\n```', file.path, filetype, current_selection.text)
209200

210201
return text
211202
end

lua/opencode/core.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ M.open = Promise.async(function(opts)
9292

9393
if are_windows_closed then
9494
if not ui.is_opencode_focused() then
95-
state.ui.set_last_code_window(vim.api.nvim_get_current_win())
96-
state.ui.set_current_code_buf(vim.api.nvim_get_current_buf())
95+
state.ui.set_code_context(vim.api.nvim_get_current_win(), vim.api.nvim_get_current_buf())
9796
end
9897

9998
M.is_prompting_allowed()

lua/opencode/server_job.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ function M.spawn_local_server(promise, port, hostname)
293293
log.notify(string.format('Started local server at %s', base_url), vim.log.levels.INFO)
294294
if url_port then
295295
local port_num = tonumber(url_port)
296+
state.store.set_raw('opencode_server', state.opencode_server)
296297
state.opencode_server.port = port_num
297298
local server_pid = job and job.pid
298299
port_mapping.register(port_num, vim.fn.getcwd(), true, 'serve', nil, server_pid)

lua/opencode/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
return require('opencode.state.init') --[[@as OpencodeStateModule]]
1+
return require('opencode.state.init') --[[@as OpencodeState]]

lua/opencode/state/context.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
21
local store = require('opencode.state.store')
32

43
---@class OpencodeContextStateMutations
5-
---@field set_current_context_config fun(config: OpencodeContextConfig|nil)
6-
---@field set_context_updated_at fun(timestamp: number|nil)
7-
---@field set_current_cwd fun(cwd: string|nil)
8-
94
local M = {}
105

116
---@param config OpencodeContextConfig|nil
127
function M.set_current_context_config(config)
138
return store.set('current_context_config', config)
149
end
1510

11+
function M.update_current_context_config(mutator)
12+
return store.mutate('current_context_config', mutator)
13+
end
14+
1615
---@param timestamp number|nil
1716
function M.set_context_updated_at(timestamp)
1817
return store.set('context_updated_at', timestamp)

lua/opencode/state/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local context = require('opencode.state.context')
1616
---@field context OpencodeContextStateMutations
1717

1818
---@alias OpencodeState OpencodeStateModule & OpencodeStateData
19+
1920
---@type OpencodeState
2021
local M = {
2122
store = store,

lua/opencode/state/jobs.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
local store = require('opencode.state.store')
22

33
---@class OpencodeJobStateMutations
4-
---@field increment_count fun(delta?: integer)
5-
---@field decrement_count fun(delta?: integer)
6-
---@field set_count fun(count: integer)
7-
---@field set_server fun(server: OpencodeServer|nil)
8-
---@field clear_server fun()
9-
---@field set_api_client fun(client: OpencodeApiClient|nil)
10-
---@field set_event_manager fun(manager: EventManager|nil)
11-
---@field set_opencode_cli_version fun(version: string|nil)
12-
---@field is_running fun():boolean
13-
144
local M = {}
155

166
---@param delta integer|nil

lua/opencode/state/model.lua

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
local store = require('opencode.state.store')
22

33
---@class OpencodeModelStateMutations
4-
---@field set_mode fun(mode: string|nil)
5-
---@field clear_mode fun()
6-
---@field set_model fun(model: string|nil)
7-
---@field clear_model fun()
8-
---@field clear fun()
9-
---@field set_model_info fun(info: table|nil)
10-
---@field set_variant fun(variant: string|nil)
11-
---@field clear_variant fun()
12-
---@field set_mode_model_map fun(mode_map: table<string, string>)
13-
---@field set_mode_model_override fun(mode: string, model: string)
14-
154
local M = {}
165

176
---@param mode string|nil
@@ -33,9 +22,11 @@ function M.clear_model()
3322
end
3423

3524
function M.clear()
36-
store.set('current_model', nil)
37-
store.set('current_mode', nil)
38-
store.set('current_variant', nil)
25+
return store.batch(function()
26+
store.set('current_model', nil)
27+
store.set('current_mode', nil)
28+
store.set('current_variant', nil)
29+
end)
3930
end
4031

4132
---@param info table|nil

lua/opencode/state/renderer.lua

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
21
local store = require('opencode.state.store')
32

43
---@class OpencodeRendererStateMutations
5-
---@field set_messages fun(messages: OpencodeMessage[]|nil)
6-
---@field set_current_message fun(message: OpencodeMessage|nil)
7-
---@field set_last_user_message fun(message: OpencodeMessage|nil)
8-
---@field set_pending_permissions fun(permissions: OpencodePermission[])
9-
---@field set_cost fun(cost: number)
10-
---@field set_tokens_count fun(count: number)
11-
---@field set_stats fun(tokens_count: number, cost: number)
12-
---@field reset fun()
13-
144
local M = {}
155

166
---@param messages OpencodeMessage[]|nil
@@ -33,6 +23,10 @@ function M.set_pending_permissions(permissions)
3323
return store.set('pending_permissions', permissions)
3424
end
3525

26+
---@param mutator fun(current_permissions: OpencodePermission[]): OpencodePermission[]
27+
function M.update_pending_permissions(mutator)
28+
return store.mutate('pending_permissions', mutator)
29+
end
3630
---@param cost number
3731
function M.set_cost(cost)
3832
return store.set('cost', cost)
@@ -46,16 +40,21 @@ end
4640
---@param tokens_count number
4741
---@param cost number
4842
function M.set_stats(tokens_count, cost)
49-
store.set('tokens_count', tokens_count)
50-
store.set('cost', cost)
43+
return store.batch(function()
44+
store.set('tokens_count', tokens_count)
45+
store.set('cost', cost)
46+
end)
5147
end
5248

5349
function M.reset()
54-
store.set('messages', {})
55-
store.set('last_user_message', nil)
56-
store.set('tokens_count', 0)
57-
store.set('cost', 0)
58-
store.set('pending_permissions', {})
50+
return store.batch(function()
51+
store.set('messages', {})
52+
store.set('current_message', nil)
53+
store.set('last_user_message', nil)
54+
store.set('tokens_count', 0)
55+
store.set('cost', 0)
56+
store.set('pending_permissions', {})
57+
end)
5958
end
6059

6160
return M

lua/opencode/state/session.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
local store = require('opencode.state.store')
22

33
---@class OpencodeSessionStateMutations
4-
---@field set_active fun(session: Session|nil)
5-
---@field clear_active fun()
6-
---@field set_restore_points fun(points: RestorePoint[])
7-
---@field reset_restore_points fun()
8-
---@field set_last_sent_context fun(context: OpencodeContext|nil)
9-
---@field set_user_message_count fun(count: table<string, number>)
10-
114
local M = {}
125

136
---@param session Session|nil
147
function M.set_active(session)
15-
store.set('restore_points', {})
16-
store.set('last_sent_context', nil)
17-
store.set('user_message_count', {})
18-
return store.set('active_session', session)
8+
return store.batch(function()
9+
store.set('restore_points', {})
10+
store.set('last_sent_context', nil)
11+
store.set('user_message_count', {})
12+
return store.set('active_session', session)
13+
end)
1914
end
2015

2116
function M.clear_active()
22-
M.reset_restore_points()
23-
M.set_last_sent_context()
24-
M.set_user_message_count({})
25-
return store.set('active_session', nil)
17+
return store.batch(function()
18+
store.set('restore_points', {})
19+
store.set('last_sent_context', nil)
20+
store.set('user_message_count', {})
21+
return store.set('active_session', nil)
22+
end)
2623
end
2724

2825
---@param points RestorePoint[]

0 commit comments

Comments
 (0)