Mutable properties in an Environment
#505
-
In the project I'm working on we have a number of places where we have what is technically mutable state, in an For example: we have an So, This means we're basically using this environment property as global mutable state, which has always struck me as code smell, but I haven't really thought of a good alternative. I guess the "correct" way to do this would be to keep this property in the btw, this account ID cannot be encoded in the Auth Token, since a single user can have multiple accounts and can switch between them whilst using the app. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Can you store them in the Keychain or a database, and just write to it via the environment? |
Beta Was this translation helpful? Give feedback.
-
We don't think mutable state in an environment dependency is a bad thing! In fact, that's what the environment is for! For example, if you wrote a nice wrapper around And so the API client having a little shared mutable data to represent the current session, such as an access token, current user, or in your case account id, is totally fine. And as @jasdev pointed out, this is exactly what we do in isowords. The "live" API client holds a mutable One important thing to note is that it's locally scoped to just the construction of the live instant, and so it will never leak out unintentionally. The only way to get access to this shared mutable state is through the dependency's endpoints. For example, when you "authenticate" the player we update that shared mutable value: And when you log out we And when we refresh the current player we update that mutable value: In our opinion it's no different than thinking of "current player" as something in a database, or stored in shared user defaults, or stored on disk, etc. The key is to control how it's accessed through the dependency because that makes it easier to understand how the outside world accesses and mutates it, and makes it testable. |
Beta Was this translation helpful? Give feedback.
-
In case anyone is interested in providing feedback, I created a PR for Isowords attempting to implement my interpretation of how persistence should be ideally handled: pointfreeco/isowords#169. It's a work-in-progress but hopefully I can get it to a production-ready state and then we can all benefit from learning how persistence can be handled in a real-world app :) |
Beta Was this translation helpful? Give feedback.
We don't think mutable state in an environment dependency is a bad thing! In fact, that's what the environment is for!
For example, if you wrote a nice wrapper around
UserDefaults
and put that in your environment then you've immediately got global shared state. Same if you have a dependency wrapper around file access. Or if you have a database dependency.And so the API client having a little shared mutable data to represent the current session, such as an access token, current user, or in your case account id, is totally fine.
And as @jasdev pointed out, this is exactly what we do in isowords. The "live" API client holds a mutable
currentPlayer
(it's also backed by user defaults, but tha…