You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That way we won't know about ServiceX parameter that Feature doesn't need to know about. Consider this feature some generic inner feature that doesn't know about the source where it opened.
We would have to somehow create unimplemented/testValue version of ServiceX, and here @DependencyEndpoint and @DependencyClient macros could be useful. Without conforming to DependencyKey.
Tests would still be usable with Feature, but every test would create a version of ServiceX even if it doesn't need to test the logic related to it.
Option 3.
The service is a @Dependency, but it's provided in runtime
structServiceX{letscreenSource:ScreenSourceinit(screenSource:ScreenSource){self.screenSource = screenSource }var doWork(prompt: String){
switch screenSource // ...}}// later in auth screenwithDependencies{
$0.serviceX =ServiceX(screenSource:.auth)} operation:{Feature()}
I want to avoid that option, because it could lead to unexpected behaviour when someone forgets to provide a proper setup for a dependency.
Also it raises a question what is the live version then?.
Probably we could only conform to TestDependencyKey and live version wouldn't even exist, it would only appear in runtime and that could be ok.
My first thought was to go with Option 1 and I actually think all parameters that affect a method would be the best option here. The service would signal to everyone about every dependency it needs to operate.
I would love to hear your opinion on this. I'm not sure which way to go, and I'm not sure if I'm missing something. I would love to hear your thoughts on this.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a dependency that should receive input parameters on specific events, and only then operate using these input parameters.
I think of it as a dependency with a state, but it just needs input parameters to operate.
Consider this example:
Input here is
screenSource
.doWork
then uses screenSource somehow and, for example, prints a message only when source is home.Question
The question is how stateful services operate with Swift Dependencies? Like the one I added above.
Let's also consider this is used in TCA, and the
@Dependency
way of using services is common for testing purposes.I know possible solutions, but I would love to hear your opinion on this and choose wisely.
Option 1.
The service wouldn't have state, and the input should be passed as a parameter to the method.
That way we have to pass the screenSource inside the call site. This path could be long:
screenSource
in init of@Reducer Feature
doWork
That would add one pass of
screenSource
through Feature, and every call would know about all the parameters.It might be a positive thing, because then we can see all the dependencies of the method.
Option 2.
The service could have state, and the state should be passed in the init. But we can avoid using
@Dependency
.Then we'll pass this service into
@Reducer Feature
init and use it as a stateful service.That way we won't know about ServiceX parameter that Feature doesn't need to know about. Consider this feature some generic inner feature that doesn't know about the source where it opened.
We would have to somehow create unimplemented/testValue version of ServiceX, and here
@DependencyEndpoint
and@DependencyClient
macros could be useful. Without conforming to DependencyKey.Tests would still be usable with Feature, but every test would create a version of ServiceX even if it doesn't need to test the logic related to it.
Option 3.
The service is a
@Dependency
, but it's provided in runtimeI want to avoid that option, because it could lead to unexpected behaviour when someone forgets to provide a proper setup for a dependency.
Also it raises a question what is the live version then?.
Probably we could only conform to
TestDependencyKey
and live version wouldn't even exist, it would only appear in runtime and that could be ok.My first thought was to go with Option 1 and I actually think all parameters that affect a method would be the best option here. The service would signal to everyone about every dependency it needs to operate.
I would love to hear your opinion on this. I'm not sure which way to go, and I'm not sure if I'm missing something. I would love to hear your thoughts on this.
Beta Was this translation helpful? Give feedback.
All reactions