Skip to content

Commit 845616e

Browse files
authored
Improve Forwarded documentation (#187)
1 parent fd04513 commit 845616e

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

Documentation/Manual.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,51 @@ Property declarations within `@Instantiable` types decorated with [`@Instantiate
177177

178178
Property declarations within `@Instantiable` types decorated with [`@Forwarded`](../Sources/SafeDI/Decorators/Forwarded.swift) represent dependencies that come from the runtime, e.g. user input or backend-delivered content. Like an `@Instantiated`-decorated property, a `@Forwarded`-decorated property is available to be `@Received` by objects instantiated further down the dependency tree.
179179

180-
A `@Forwarded` property is forwarded into the SafeDI dependency tree by a [`Instantiator`](#instantiator)’s `instantiate(_ forwardedProperties: T.ForwardedProperties) -> T` function that creates an instance of the property’s enclosing type.
180+
A `@Forwarded` property is forwarded into the SafeDI dependency tree by an [`Instantiator`](#instantiator)’s `instantiate(_:)` function that creates an instance of the property’s enclosing type.
181181

182182
Forwarded property types do not need to be decorated with the `@Instantiable` macro.
183183

184+
Here’s an example showing how to forward a runtime value into an `@Instantiable` type:
185+
186+
```swift
187+
// A view that requires a runtime value (the user’s name).
188+
@Instantiable
189+
public struct LoggedInView: View, Instantiable {
190+
public init(userName: String) {
191+
self.userName = userName
192+
}
193+
194+
public var body: some View {
195+
Text("Hello, \(userName)")
196+
}
197+
198+
@Forwarded private let userName: String
199+
}
200+
201+
// A view that creates LoggedInView when there is a user.
202+
@Instantiable
203+
public struct RootView: View, Instantiable {
204+
public init(loggedInViewBuilder: Instantiator<LoggedInView>, loggedOutViewBuilder: Instantiator<LoggedOutView>, userService: UserService) {
205+
self.loggedInViewBuilder = loggedInViewBuilder
206+
self.loggedOutViewBuilder = loggedOutViewBuilder
207+
self.userService = userService
208+
}
209+
210+
public var body: some View {
211+
if let userName = userService.currentUser?.name {
212+
// Pass the forwarded property when instantiating.
213+
loggedInViewBuilder.instantiate(userName)
214+
} else {
215+
loggedOutViewBuilder.instantiate()
216+
}
217+
}
218+
219+
@Instantiated private let loggedInViewBuilder: Instantiator<LoggedInView>
220+
@Instantiated private let loggedOutViewBuilder: Instantiator<LoggedOutView>
221+
@Instantiated private let userService: UserService
222+
}
223+
```
224+
184225
### @Received
185226

186227
Property declarations within `@Instantiable` types decorated with [`@Received`](../Sources/SafeDI/Decorators/Received.swift) are injected into the enclosing type’s initializer. Received properties must be `@Instantiated` or `@Forwarded` by an object higher up in the dependency tree.

0 commit comments

Comments
 (0)