2 strategies for sharing state #2564
Replies: 1 comment 1 reply
-
Yeah, it was a well-written article about the two most common(?) ways of sharing state in TCA. At first, I tried to solve most things with option 1, what you call root ownership, but I have gradually shifted more towards dependency ownership. Mainly because we're using GRDB/Sqlite and it works very well to observe changes in the database. Then the database becomes the source of truth. But we also use it for many 'clients' because it just easier to share the state between features if the data should be shared with a feature's descendents. I like the concept of being able to share state via scoping. Unfortunately, I find it only practical when you have simple and direct 'parent-child' relationships, like A->B. A can hold B's state which contains the shared property, no need to copy or create computed properties. But if you have A->B->C, it becomes a bit more complicated. A->B is simple, just as above, but then you often have to use some kind of strategy like 'delegate' or 'computed property' to sync between B->C because B has a property that C's state needs. Add an additional feature as a child to C and then it starts to get quite hairy to keep track of everything. It gets a bit more complicated if you have navigation which uses enums because then you need to manipulate each case separately to share the state. Maybe the new CasePaths lib can help out a bit but it still requires code to sync the state. I was thinking if macros or property wrappers could be used to simplify the 'computed property' strategy but I'm not sure if it's possible to describe such thing because you need to specify which properties should be copied. |
Beta Was this translation helpful? Give feedback.
-
I wrote a blog post about 2 distinct strategies for sharing state in TCA: root ownership and dependency ownership. I've seen these two strategies discussed in bits and pieces on this discussion board, and I wanted to put a name to each and try to work through an example of each to clarify my own understanding.
I consider myself a TCA beginner, so I'd be happy to get feedback on whether my understanding of state sharing matches more experienced TCA users.
https://twocentstudios.com/2023/11/13/tca-strategies-sharing-state/
Beta Was this translation helpful? Give feedback.
All reactions