Middleware to deep-freeze all state
LPGhatguy opened this issue · 3 comments
It'd be cool to have a middleware that automatically makes all items in the store read-only. It'd be similar to the JS package 'deep-freeze', but probably throw errors on mutations instead of silently ignoring them.
Speed of implementation doesn't really matter here, since it'd only be a development tool.
Implementing this in Lua 5.1 might be tricky. Normally for a read-only table, I'd create a new table with __index
and __newindex
metamethods acting as a proxy, but then you wouldn't be able to iterate (or shallow copy) the table! In 5.2, Lua gained __pairs
and __ipairs
which would've helped here, but alas.
There might be one edge case in this implementation. Way back when I first used Redux, I would keep JS Promise
objects in the store as a way to manage request status. Promise objects have internal mutability, which we might consider to be breaking the Redux paradigm, but because it's not observable outside of attaching a handler to the promise, I don't know if it is. If we wanted to support a use case like that, we'd need to make sure not to deep-freeze some objects, or force Promise objects to be userdata instead.
I don't think we need to support Promise
objects in the store.
I haven't gained any insight into how to actually deep-freeze tables.
I'm not sure if deep-freezing tables is even possible in Lua without __pairs
and __ipairs
. Deep-freezing in Lua 5.1 must either:
- Allow mutations of existing keys (bad)
- Not allow iteration over the frozen table (also bad)
table.freeze is now out, so this is doable.
I don't think we need to support Promise objects in the store.
In my own systems, I check getmetatable(table) ~= nil
as a heuristic for whether or not to freeze or not. Not sure how good it is as a generality.