Skip to content

Commit

Permalink
Allow prepareDependencies to return result.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Jan 28, 2025
1 parent 565a7be commit 8946e94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/Dependencies/WithDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import Foundation
///
/// - Parameter updateValues: A closure for updating the current dependency values for the
/// lifetime of your application.
public func prepareDependencies(
_ updateValues: (inout DependencyValues) throws -> Void
) rethrows {
public func prepareDependencies<R>(
_ updateValues: (inout DependencyValues) throws -> R
) rethrows -> R {
var dependencies = DependencyValues._current
try DependencyValues.$preparationID.withValue(UUID()) {
return try DependencyValues.$preparationID.withValue(UUID()) {
try updateValues(&dependencies)
}
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/DependenciesTests/DependencyValuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,13 @@ final class DependencyValuesTests: XCTestCase {
XCTAssertEqual(now, Date(timeIntervalSinceReferenceDate: 0))
}

func testPrepareDependencies_returnResult() {
let result = prepareDependencies { _ in
42
}
XCTAssertEqual(result, 42)
}

func testPrepareDependencies_setsLiveContext() {
prepareDependencies {
$0.context = .live
Expand Down

0 comments on commit 8946e94

Please sign in to comment.