Help with optics when dealing with atoms-in-atom #2979
Unanswered
ampossardt
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Looking for some help trying to understand optics when the subject of the focusing is an atom stored within an atom.
Currently, I have an array of items which are generically typed. They look something like this:
Each item is stored in an atom, scoped to the component in question (in this case, I'm using splitAtom at a higher level to render this list of atoms):
The reasoning behind scoping the data in such a way is to simplify accessing the data across nested components, and also to create derived atoms to represent statuses and such (each component is rather complex).
The data on each tree item has an array property which represents a list of versions which are updated every time a version is added after the initial load. Something like this:
Whenever I load the items initially, I create a wrapper atom around the versions to isolate them from changes to the data/item:
My end goal in all of this is to be able to grab the 'most recent' version off the version stack, and also have it react to new items being added to that set of versions. So, ideally I could do something like:
However, once I have focused to the 'versions' prop, the result is an atom (which can't be focused) rather than an array which can further be focused. I'm probably missing something with how the underlying code works, but if the subject of focus is an atom, and the result is a writable atom which represents a piece of the original, I would have to assume somewhere along the way it can 'unwrap' the atom (for lack of better term) to be able to drill down into its actual properties.
At this point, things have gotten rather complex so I'm thinking about a few things:
For now, I have a solution which seems to be fine for cases where the 'latest version' is data that I want to use within a component directly:
However, I can't use this in situations where I want to create a derived atom outside of the flow of a component, since then I'm creating a dependency 'versions' in its entirety, which means that derived atom is subject to all changes to the array. A third question might be, if wrapping versions initially is logical, is it equally as logical to store the 'focused' latest version within the same object? Something like:
Any tips/suggestions on how I should approach this? The longer I describe this, the more I start to think the differences in performance are likely minimal and I'm overengineering this haha. I'd love outside opinions though.
Beta Was this translation helpful? Give feedback.
All reactions