22--- @field source ? ' helper' | ' raw'
33--- @field silent ? boolean
44
5+ --- @alias StateValue <K> K extends keyof OpencodeState and StateValue<K> or never
6+
57local M = {}
68
9+ --- @type OpencodeState
710local _state = {
811 windows = nil ,
912 is_opening = false ,
@@ -62,16 +65,20 @@ function M.state()
6265 return _state
6366end
6467
65- --- @param key string
66- --- @return any
68+ --- @generic K extends keyof OpencodeState
69+ --- @param key K
70+ --- @return OpencodeState[K]
6771function M .get (key )
6872 return _state [key ]
6973end
7074
71- --- @param key string
72- --- @param value any
75+ local c = M .get (' user_message_counte' )
76+
77+ --- @generic K extends keyof OpencodeState
78+ --- @param key K
79+ --- @param value StateValue<K>
7380--- @param opts ? OpencodeProtectedStateSetOptions
74- --- @return any
81+ --- @return StateValue<K>
7582function M .set (key , value , opts )
7683 local old = _state [key ]
7784 opts = opts or { source = ' helper' }
@@ -88,28 +95,30 @@ function M.set(key, value, opts)
8895 return value
8996end
9097
91- --- @param key string
92- --- @param value any
98+ --- @generic K extends keyof OpencodeState
99+ --- @param key K
100+ --- @param value StateValue<K>
93101--- @param opts ? OpencodeProtectedStateSetOptions
94- --- @return any
102+ --- @return StateValue<K>
95103function M .set_raw (key , value , opts )
96104 local next_opts = vim .tbl_extend (' force' , { source = ' raw' }, opts or {})
97105 return M .set (key , value , next_opts )
98106end
99107
100- --- @generic T
101- --- @param key string
102- --- @param updater fun ( current : T ): T
108+ --- @generic K extends keyof OpencodeState
109+ --- @param key K
110+ --- @param updater fun ( current : StateValue<K> ): StateValue<K>
103111--- @param opts ? OpencodeProtectedStateSetOptions
104- --- @return T
112+ --- @return StateValue<K>
105113function M .update (key , updater , opts )
106114 local next_value = updater (_state [key ])
107115 M .set (key , next_value , opts )
108116 return next_value
109117end
110118
111- --- @param key string | string[] | nil
112- --- @param cb fun ( key : string , new_val : any , old_val : any )
119+ --- @generic K extends keyof OpencodeState
120+ --- @param key K | K[] | nil
121+ --- @param cb fun ( key : K , new_val : StateValue<K> , old_val : StateValue<K> )
113122function M .subscribe (key , cb )
114123 if type (key ) == ' table' then
115124 for _ , current_key in ipairs (key ) do
@@ -132,8 +141,9 @@ function M.subscribe(key, cb)
132141 table.insert (_listeners [key ], cb )
133142end
134143
135- --- @param key string | nil
136- --- @param cb fun ( key : string , new_val : any , old_val : any )
144+ --- @generic K extends keyof OpencodeState
145+ --- @param key K | nil
146+ --- @param cb fun ( key : K , new_val : StateValue<K> , old_val : StateValue<K> )
137147function M .unsubscribe (key , cb )
138148 key = key or ' *'
139149 local list = _listeners [key ]
@@ -148,6 +158,10 @@ function M.unsubscribe(key, cb)
148158 end
149159end
150160
161+ --- @generic K extends keyof OpencodeState
162+ --- @param key K
163+ --- @param new_val StateValue<K>
164+ --- @param old_val StateValue<K>
151165function M .emit (key , new_val , old_val )
152166 vim .schedule (function ()
153167 if _listeners [key ] then
@@ -167,8 +181,9 @@ function M.emit(key, new_val, old_val)
167181 end )
168182end
169183
170- --- @param key string
171- --- @param value any
184+ --- @generic K extends keyof OpencodeState
185+ --- @param key K
186+ --- @param value StateValue<K> extends any[] and StateValue<K>[integer] or never
172187function M .append (key , value )
173188 if type (value ) ~= ' table' then
174189 error (' Value must be a table to append' )
@@ -185,7 +200,8 @@ function M.append(key, value)
185200 M .emit (key , _state [key ], old )
186201end
187202
188- --- @param key string
203+ --- @generic K extends keyof OpencodeState
204+ --- @param key K
189205--- @param idx integer
190206function M .remove (key , idx )
191207 if not _state [key ] then
0 commit comments