-
Notifications
You must be signed in to change notification settings - Fork 10
ComposableEnvironment
The base class of your environments.
@dynamicMemberLookup
open class ComposableEnvironment Subclass this class to define your feature's environment. You can expose
ComposableDependencies values using the Dependency property wrapper and declare child
environment using the DerivedEnvironment property wrapper.
For example, if you define:
extension ComposableDependencies {
var uuidGenerator: () -> UUID {…}
var mainQueue: AnySchedulerOf {…}
},you can declare the LocalEnvironment class, with ChildEnvironment1 and ChildEnvironment2
like:
class LocalEnvironment: ComposableEnvironment {
@Dependency(\.uuidGenerator) var uuidGenerator
@Dependency(\.mainQueue) var mainQueue
@DerivedEnvironment<ChildEnvironment1> var child1
@DerivedEnvironment<ChildEnvironment2> var child2
}Instantiate a ComposableEnvironment instance with all dependencies sets to their defaults.
public required init() After using this initializer, you can chain with(_:_:) calls to set the values of
individual dependencies. These values ill propagate to each childDerivedEnvironment as
well as their own children DerivedEnvironment.
Use this function to set the values of a given dependency for this environment and all its descendants.
@discardableResult
public func with<V>(_ keyPath: WritableKeyPath<ComposableDependencies, V>, _ value: V) -> Self Calls to this function are chainable, and you can specify any ComposableDependencies
KeyPath, even if the current environment instance does not expose the corresponding
dependency itself.
For example, if you define:
extension ComposableDependencies {
var uuidGenerator: () -> UUID {…}
var mainQueue: AnySchedulerOf {…}
},you can set their values in a LocalEnvironment instance and all its descendants like:
LocalEnvironment()
.with(\.uuidGenerator, { UUID() })
.with(\.mainQueue, .main)Generated at 2022-08-22T21:22:01+0000 using swift-doc 1.0.0-rc.1.