Skip to content

Commit

Permalink
Doc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
philippzagar committed Dec 7, 2023
1 parent cfb533f commit 6eda4f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Sources/Spezi/Dependencies/DependencyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public struct DependencyCollection: DependencyDeclaration {
/// - Parameters:
/// - type: The generic type resulting from the passed closure, has to conform to ``Module``.
/// - singleEntry: Closure returning a dependency conforming to ``Module``, stored within the ``DependencyCollection``.
///
/// ### Usage
///
/// The `ExampleDependencyBuilder` enforces certain type constraints (e.g., `SomeTypeConstraint`, more specific than `Module`) during aggregation of ``Module/Dependency``s (``Module``s) via a result builder. The individual dependency expressions within the result builder conforming to `SomeTypeConstraint` are then transformed to a ``DependencyCollection`` via ``DependencyCollection/init(for:singleEntry:)``.
///
/// ```swift
/// @resultBuilder
/// public enum ExampleDependencyBuilder: DependencyCollectionBuilder {
/// public static func buildExpression<T: SomeTypeConstraint>(_ expression: @escaping @autoclosure () -> T) -> DependencyCollection {
/// DependencyCollection(singleEntry: expression)
/// }
/// }
/// ```
public init<Dependency: Module>(for type: Dependency.Type = Dependency.self, singleEntry: @escaping (() -> Dependency)) {
self.init(DependencyContext(for: type, defaultValue: singleEntry))
}
Expand Down
19 changes: 17 additions & 2 deletions Sources/Spezi/Dependencies/DependencyPropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,28 @@ extension _DependencyPropertyWrapper: ModuleArrayDependency where Value == [any
self.init(DependencyCollection())
}

/// Creates the `@Dependency` property wrapper from an instantiated ``DependencyCollection``.
/// Creates the `@Dependency` property wrapper from an instantiated ``DependencyCollection``, enabling the use of a custom ``DependencyBuilder`` enforcing certain type constraints on the passed, nested ``Dependency``s.
/// - Parameters:
/// - dependencies: The ``DependencyCollection`` to be wrapped.
///
/// ### Usage
///
/// The `ExampleModule` is initialized with nested ``Module/Dependency``s (``Module``s) enforcing certain type constraints via the `SomeCustomDependencyBuilder`.
/// Spezi automatically injects declared ``Dependency``s within the passed ``Dependency``s from the initializer, enabling proper nesting of ``Module``s.
///
/// ```swift
/// class ExampleModule: Module {
/// @Dependency var dependentModules: [any Module]
///
/// init(@SomeCustomDependencyBuilder _ dependencies: @Sendable () -> DependencyCollection) {
/// self._dependentModules = Dependency(using: dependencies())
/// }
/// }
/// ```
public convenience init(using dependencies: DependencyCollection) {
self.init(dependencies)
}

public convenience init(@DependencyBuilder _ dependencies: () -> DependencyCollection) {
self.init(dependencies())
}
Expand Down

0 comments on commit 6eda4f1

Please sign in to comment.