44
55local M = {}
66
7- --- @type OpencodeState
7+ --- @type OpencodeStateData
88local _state = {
99 windows = nil ,
1010 is_opening = false ,
@@ -49,7 +49,7 @@ local _state = {
4949
5050local _listeners = {}
5151
52- --- @param key string
52+ --- @param key keyof OpencodeStateData
5353--- @param opts ? OpencodeProtectedStateSetOptions
5454local function error_on_raw_write (key , opts )
5555 if opts and opts .silent then
@@ -63,18 +63,18 @@ function M.state()
6363 return _state
6464end
6565
66- --- @generic K extends keyof OpencodeState
66+ --- @generic K extends keyof OpencodeStateData
6767--- @param key K
68- --- @return OpencodeState[K]
68+ --- @return std.RawGet<OpencodeStateData , K>
6969function M .get (key )
7070 return _state [key ]
7171end
7272
73- --- @generic K extends keyof OpencodeState
73+ --- @generic K extends keyof OpencodeStateData
7474--- @param key K
75- --- @param value OpencodeState[K]
75+ --- @param value std.RawGet<OpencodeStateData , K>
7676--- @param opts ? OpencodeProtectedStateSetOptions
77- --- @return OpencodeState[K]
77+ --- @return std.RawGet<OpencodeStateData , K>
7878function M .set (key , value , opts )
7979 local old = _state [key ]
8080 opts = opts or { source = ' helper' }
@@ -91,30 +91,30 @@ function M.set(key, value, opts)
9191 return value
9292end
9393
94- --- @generic K extends keyof OpencodeState
94+ --- @generic K extends keyof OpencodeStateData
9595--- @param key K
96- --- @param value OpencodeState[K]
96+ --- @param value std.RawGet<OpencodeStateData , K>
9797--- @param opts ? OpencodeProtectedStateSetOptions
98- --- @return OpencodeState[K]
98+ --- @return std.RawGet<OpencodeStateData , K>
9999function M .set_raw (key , value , opts )
100100 local next_opts = vim .tbl_extend (' force' , { source = ' raw' }, opts or {})
101101 return M .set (key , value , next_opts )
102102end
103103
104- --- @generic K extends keyof OpencodeState
104+ --- @generic K extends keyof OpencodeStateData
105105--- @param key K
106- --- @param updater fun ( current : OpencodeState[K] ): OpencodeState[K]
106+ --- @param updater fun ( current : std.RawGet<OpencodeStateData , K> ): std.RawGet<OpencodeStateData , K>
107107--- @param opts ? OpencodeProtectedStateSetOptions
108- --- @return OpencodeState[K]
108+ --- @return std.RawGet<OpencodeStateData , K>
109109function M .update (key , updater , opts )
110110 local next_value = updater (_state [key ])
111111 M .set (key , next_value , opts )
112112 return next_value
113113end
114114
115- --- @generic K extends keyof OpencodeState
115+ --- @generic K extends keyof OpencodeStateData
116116--- @param key K | K[] | nil
117- --- @param cb fun ( key : K , new_val : OpencodeState[K] , old_val : OpencodeState[K] )
117+ --- @param cb fun ( key : K , new_val : std.RawGet<OpencodeStateData , K> , old_val : std.RawGet<OpencodeStateData , K> )
118118function M .subscribe (key , cb )
119119 if type (key ) == ' table' then
120120 for _ , current_key in ipairs (key ) do
@@ -137,9 +137,9 @@ function M.subscribe(key, cb)
137137 table.insert (_listeners [key ], cb )
138138end
139139
140- --- @generic K extends keyof OpencodeState
140+ --- @generic K extends keyof OpencodeStateData
141141--- @param key K | nil
142- --- @param cb fun ( key : K , new_val : OpencodeState[K] , old_val : OpencodeState[K] )
142+ --- @param cb fun ( key : K , new_val : std.RawGet<OpencodeStateData , K> , old_val : std.RawGet<OpencodeStateData , K> )
143143function M .unsubscribe (key , cb )
144144 key = key or ' *'
145145 local list = _listeners [key ]
@@ -154,10 +154,10 @@ function M.unsubscribe(key, cb)
154154 end
155155end
156156
157- --- @generic K extends keyof OpencodeState
157+ --- @generic K extends keyof OpencodeStateData
158158--- @param key K
159- --- @param new_val OpencodeState[K]
160- --- @param old_val OpencodeState[K]
159+ --- @param new_val std.RawGet<OpencodeStateData , K>
160+ --- @param old_val std.RawGet<OpencodeStateData , K>
161161function M .emit (key , new_val , old_val )
162162 vim .schedule (function ()
163163 if _listeners [key ] then
@@ -177,9 +177,9 @@ function M.emit(key, new_val, old_val)
177177 end )
178178end
179179
180- --- @generic K extends keyof OpencodeState
180+ --- @generic K extends keyof OpencodeStateData
181181--- @param key K
182- --- @param value OpencodeState[K] extends any[] and OpencodeState[K] [integer] or never
182+ --- @param value std.RawGet<OpencodeStateData , K> extends any[] and std.RawGet<OpencodeStateData , K> [integer] or never
183183function M .append (key , value )
184184 if type (value ) ~= ' table' then
185185 error (' Value must be a table to append' )
@@ -196,7 +196,7 @@ function M.append(key, value)
196196 M .emit (key , _state [key ], old )
197197end
198198
199- --- @generic K extends keyof OpencodeState
199+ --- @generic K extends keyof OpencodeStateData
200200--- @param key K
201201--- @param idx integer
202202function M .remove (key , idx )
0 commit comments