Dependencies that depend on dynamic values #1775
Replies: 2 comments
-
On Option 1: We've been using option 1 for a while and have some ground rules to make it a bit nice:
But it still requires PR reviews to be very attentive. If a new feature is implemented and doesn't follow this rules it will be problematic. So we're still open for better suggestions ^^ |
Beta Was this translation helpful? Give feedback.
-
On Option 2: Reading again the linked discussion it seems that is suggested (or at least not frowned upon) that dependencies handle the state internally. See #1287 (comment)
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, we've encountered an interesting dependency modeling issue and we'd love to hear opinions of the community!
Our discussion might be related to #1287, can move our findings there if preferred.
Our app's network stack needs to hit different urls based on the user that's logged in and the app needs to be able to logout and log back in during its lifetime. We have a mix of in house api layers and layers that rely on third party libraries that we don't fully control, but all expect url at the time of initialization. This previously wasn't an issue because clients were created either after login or at the point of reducer creation, but since dependency values are static vars, it's now something we need to keep in mind.
Options we've came up with so far:
Use the
_DependencyKeyWritingReducer
higher order reducerThis might the easiest solution, but since our newer TCA-based features need to integrate with our older coordinator based app we would need to "override" the dependencies in many places and we can't be sure at compile time, that feature is using the correct dependency. We can alleviate this down side by not implementing live values on dependencies that depend on session lifecycle, which would warn us at run time when developing feature that uses such client and by requiring said clients at reducer init, so we can't forget to supply the correct client at point of creation. Static analysis could also help us with this option, but it doesn't seem feasible at the moment.
Handle url change internally in network clients
I think this is generally pretty good solution and one that the built-in dependencies use (
Calendar.autoupdatingCurrent
for example), however I'm not sure its ideal for our use case. Adding this capability might require large changes and big portion of the app doesn't currently need such functionality.Wrap client in object that handles resetting
With this approach I like the composability: any client can be tied to the life-time of a session, separation of concerns: session can be contained only to dependencies and rest of the app can continue using clients as they did before and the ease of use for consumers of such client: if client only provides
Session<APIClient>
through the dependencies system, consumers don't need to worry whether they're using the correct client or if they need to handle some action when urls change. It does however come with a decrease in ergonomics, if dependency uses unapplied instance method, which is unsupported by key paths, the user needs to add another property wrapper, which requires better understanding of the issue.For now we went with option 1, we found it the easiest to grasp, but we're wondering what does the community think.
Beta Was this translation helpful? Give feedback.
All reactions