-
Notifications
You must be signed in to change notification settings - Fork 10
GlobalDependenciesAccessing
A marker protocol that provides convenient access to global dependencies.
public protocol GlobalDependenciesAccessing This protocol has not requirements. By conforming to it you expose some convenient methods to setup and access global depencies.
ComposableEnvironment, ComposableEnvironment
public required init() Use this function to set the values of a given dependency for the global environment.
@discardableResult
public func with<V>(_ keyPath: WritableKeyPath<Dependencies, V>, _ value: V) -> Self Calls to this function are chainable, and you can specify any Dependencies
KeyPath, even if the current environment 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.
Use this function to set the values of a given dependency for the global environment.
@discardableResult
public func with<V>(_ keyPath: WritableKeyPath<Dependencies, V>, _ value: V) -> Self Calls to this function are chainable, and you can specify any Dependencies
KeyPath, even if the current environment 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)A read-write subcript to directly access a dependency from its KeyPath in
Dependencies.
public subscript<Value>(keyPath: WritableKeyPath<Dependencies, Value>) -> Value A read-only subcript to directly access a global dependency from Dependencies.
public subscript<Value>(
dynamicMember keyPath: KeyPath<Dependencies, Value>
) -> Value 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.