Add targeted dependency cache reset, useful for non single-entry point systems #318
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a first pass at a feature I'd like to suggest we add to the library. I'm happy to update any documentation/additional tests if needed as well.
In general this method allows users to reset a single dependency when they call the new
DependencyValues.reset()
method. This method will take either the keypath of the DependencyValue or the Object.self.For example:
DependencyValues.reset(\.client)
orDependencyValues.reset(Client.self)
would reset the live implementation that has been cached in the cachedValues.cache.The reason for this change is for those non single-entry point systems where it is prohibitive to pass dependencies down manually with the
withDependencies
function.This method is one that I'm not suggesting lightly because I understand the benefit of the TaskLocal dependency lifetimes. Adding this method does break one of the principles of swift-dependencies which is that dependencies should change predictably and only within specific scope operation. However, I still believe there is a massive benefit for those systems that can't use TCA everywhere and are stuck on non single-entry point systems yet they don't want to pass dependencies down with the withDependencies function everywhere.
The specific use case that this is helpful for are those dependencies where we need to clear the entire state/reset the entire computed dependency. I understand that one way to do this is to move all state into a small piece of internal state for each dependency but that's not always possible, or practical everywhere in my current app without major refactors. The other thing that this helps with is the clearing out of sensitive data such as a user's information when a user logs out and another logs in.
Here is a link to the conversation in the point-free slack where we originally discussed the idea behind this feature.