-
Couldn't load subscription status.
- 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
Dependencies values using the Dependency property wrapper and declare child
environment using the DerivedEnvironment property wrapper.
For example, if you define:
extension Dependencies {
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
}GlobalEnvironment, GlobalEnvironment
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.
public required init() 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<Dependencies, V>, _ value: V) -> Self Calls to this function are chainable, and you can specify any Dependencies's
KeyPath, even if the current environment instance does not expose the corresponding
dependency itself.
For example, if you define:
extension Dependencies {
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)Identify a dependency to another one.
public func aliasing<Value>(
_ dependency: WritableKeyPath<Dependencies, Value>,
to default: WritableKeyPath<Dependencies, Value>
) -> Self You can use this method to synchronize identical dependencies from different domains.
For example, if you defined a main dispatch queue dependency called .main in one domain and
.mainQueue in another, you can identify both dependencies using
environment.aliasing(\.main, to: \.mainQueue)The second argument provides its default value to all aliased dependencies, and all aliased dependencies returns this default value until the value any of the aliased dependencies is set.
You can set the value of any aliased dependency using any KeyPath:
environment
.aliasing(\.main, to: \.mainQueue)
.with(\.main, DispatchQueue.main)
// is equivalent to:
environment
.aliasing(\.main, to: \.mainQueue)
.with(\.mainQueue, DispatchQueue.main)If you chain multiple aliases for the same dependency, the closest to the root is the one responsible for the default value:
environment
.aliasing(\.main, to: \.mainQueue) // <- The default value will be the
.aliasing(\.uiQueue, to: \.main) // default value of `mainqueue`If dependencies aliased through DerivedEnvironment are aliased in the order of environment
composition, with the dependency closest to the root environment providing the default value
if no value is set for any aliased dependency.
- dependency: The
KeyPathof the aliased dependency inDependencies - to: A
KeyPathof another dependency inDependenciesthat serves as a reference value.
Generated at 2022-08-22T21:22:01+0000 using swift-doc 1.0.0-rc.1.