Dependencies without Reducers #1289
Replies: 1 comment 5 replies
-
Hey @JimRoepcke, this is a great question, thanks for bringing it up. We will actually get into this topic very briefly in later eps of the protocol series, but essentially we do feel that the dependencies system could be extracted into its own repo some day (that's one reason why we have it as a separate library in the package). However, it comes with some caveats. While it could power the dependencies for other kinds of applications, such as vanilla UIKit/AppKit/SwiftUI, it cannot have the all the features and ergonomics that it does with TCA. One of the more interesting aspects to how TCA is designed is that it gives a single entry point into an application's logic, which is This is also true of SwiftUI views. There's only one way to create a view, and that is the Because all of these systems have a single entry point, you can do incredible things such as change the dependencies for a well-defined scope, run code in that scope, and then revert the dependencies once the scope ends. And only the code run in the scope will see the updated dependencies. Nothing outside will. For example, in the new protocol style we can override a dependency for just one child reducer (and all of its children), by running the child reducer inside a task local's This is not possible at all with a vanilla UIKit/AppKit/SwiftUI application because there is no single entry point to a feature's logic. For example, you can have an But, if you don't need dependency scoping in your application, then this system could work for you. You would still every once in awhile run into some rough edges. For example, in tests if you wanted to run an func testBasics() async {
let model = FeatureModel()
let dependencies = /* set up test dependencies */
await DependencyValues.$current.withValue(dependencies) {
await model.recordButtonTapped()
XCTAssert(…)
await model.stopButtonTapped()
XCTAssert(…)
}
} One thing cool here is that as long as your model stays in the world of structured concurrency, you will be able to run its logic in a fully controlled environment. That wouldn't have been possible before Swift's concurrency tools. So, in conclusion:
|
Beta Was this translation helpful? Give feedback.
-
Is the dependency system designed to only be usable in TCA contexts?
I'm curious if we could use
@Dependency
throughout our legacy / UIKit app as well as in TCA features.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions