-
I burnt some time while refactoring an atom that exposes an interface for loading, clearing and updating state, where I had one place in the code that was accessing the store directly, circumventing the exported callbacks. (I think I can use a typelevel approach to narrow the interface to enforce this and perhaps wrap the atom somehow in a proxy that returns a store that throws when the get/set state calls are made). But I wonder if a runtime check ability would be convenient for making an atom with "private" store, or if that's already possible using existing apis. I'd like to be able to go from an atom that is currently used in many places with directly state access to one that has a locked down interface, and feel confident that all the places the store was access, maybe even indirectly through abstracted helpers, are found. Update: I just realized this might be what ions are for. Haven’t tried those out yet. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @joprice good question. There isn't currently any built-in way to prevent using the store directly.
This wouldn't work since Zedux internally calls those methods on the store. I can't think of any way to do this currently besides crafting custom It would be about the same amount of work for me to add this as a full-fledged feature. I've been considering a concept of readonly atoms since I first explored Jotai. I believe your use case falls in that category (minus restricting "get" calls, I'll come back to that). I never implemented them because Zedux technically doesn't need them unlike Jotai, since Zedux is capable of reverse-mapping changes - derived atoms can propagate changes back to the atoms they derive from. So the spec for this would be pretty simple: Readonly atoms simply don't have the
The spec will be different in v2, though even simpler. In v2, atoms won't expose a Creating a readonly atom could be done with either a I'm a fan actually. If this spec sounds like it fulfills your use case, I'll create issues for adding this to v1 and v2. I should say, if it can wait till v2, that's ideal, but not a problem if not. |
Beta Was this translation helpful? Give feedback.
Hey @joprice good question. There isn't currently any built-in way to prevent using the store directly.
This wouldn't work since Zedux internally calls those methods on the store. I can't think of any way to do this currently besides crafting custom
AtomInstance
andAtomTemplate
classes that overrides almost all the default behavior. That is possible, but pretty complex. It would take me several hours to put together at least, but I can see if I find time for it this week.It would be about the same amount of work for me to add this as a full-fledged feature. I've been conside…