Skip to content

Commit 5991c4b

Browse files
implement function to handle state changes
This is where the magic happens. Allowing for different levels of state changes while ensuring any specific state reference is immutable. Not yet tested for bugs.
1 parent 2ddce62 commit 5991c4b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

file-browser.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,29 @@ local compatible_file_extensions = {
252252
--------------------------------------------------------------------------------------------------------
253253
--------------------------------------------------------------------------------------------------------
254254

255+
--the values table will be made readonly and set as part of the state - do not use values after passing to this function!
256+
local function update_state(level, values)
257+
local new_mt = { level = level }
258+
setmetatable(values, new_mt)
259+
260+
if level == 0 then
261+
state = API.read_only(values)
262+
return state
263+
end
264+
265+
--bypasses the readonly reference and grabs the original table and metatable
266+
local mutable_state = getmetatable(state).__index
267+
local mt = getmetatable(mutable_state)
268+
269+
--travels up the state chain until it finds a mt of the same level as `level`
270+
--this mt will be pointing to a state table of one level lower, which is what we want to point to
271+
while (mt.level > level ) do mt = getmetatable(mt.__index) end
272+
new_mt.__index = mt.__index
273+
274+
state = API.read_only(values)
275+
return state
276+
end
277+
255278
--metatable of methods to manage the cache
256279
local __cache = {}
257280

0 commit comments

Comments
 (0)