Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware to deep-freeze all state #16

Open
LPGhatguy opened this issue Mar 8, 2018 · 3 comments
Open

Middleware to deep-freeze all state #16

LPGhatguy opened this issue Mar 8, 2018 · 3 comments
Labels
blocked Blocked on an external issue enhancement

Comments

@LPGhatguy
Copy link
Contributor

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.

@LPGhatguy
Copy link
Contributor Author

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. 😦

@AmaranthineCodices
Copy link
Contributor

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)

@LPGhatguy LPGhatguy added the blocked Blocked on an external issue label Jul 31, 2018
@Kampfkarren
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked on an external issue enhancement
Projects
None yet
Development

No branches or pull requests

3 participants