Skip to content

GlobalDependenciesAccessing

tgrapperon edited this page Aug 22, 2022 · 6 revisions

GlobalDependenciesAccessing

@available(
  *, deprecated,
  message:
    """
  If you are transitioning from `GlobalEnvironment`, you should make sure that is type is a \
  subclass of `ComposableEnvironment`.
  
  If your environment is a `struct`, replacing this by `class` should allow the project to build \
  and run again as a temporary workaround.

  If you are not transitioning from `GlobalEnvironment`, you should not have to use this type \
  at all. It is only provided to help transitioning projects from `GlobalEnvironment` to \
  `ComposableEnvironment`.
  """
)
open class GlobalDependenciesAccessing: ComposableEnvironment 

Inheritance

ComposableEnvironment, ComposableEnvironment

Initializers

init()

public required init() 

Methods

with(_:_:)

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)

aliasing(_:to:)

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.

Parameters

  • dependency: The KeyPath of the aliased dependency in Dependencies
  • to: A KeyPath of another dependency in Dependencies that serves as a reference value.

Default Implementations

with(_:_:)

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)

subscript(keyPath:)

A read-write subcript to directly access a dependency from its KeyPath in Dependencies.

public subscript<Value>(keyPath: WritableKeyPath<Dependencies, Value>) -> Value 

subscript(dynamicMember:)

A read-only subcript to directly access a global dependency from Dependencies.

public subscript<Value>(
    dynamicMember keyPath: KeyPath<Dependencies, Value>
  ) -> Value 

aliasing(_:to:)

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.

Parameters

  • dependency: The KeyPath of the aliased dependency in Dependencies
  • to: A KeyPath of another dependency in Dependencies that serves as a reference value.
Clone this wiki locally