Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.4"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.4.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.0"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"605.0.0"),
],
targets: [
Expand Down
29 changes: 23 additions & 6 deletions Sources/Dependencies/DependencyValues/FireAndForget.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import IssueReporting

extension DependencyValues {
/// A dependency for firing off an unstructured task.
///
Expand Down Expand Up @@ -54,21 +56,36 @@ extension DependencyValues {
/// See ``DependencyValues/fireAndForget`` for more information.
public struct FireAndForget: Sendable {
public let operation:
@Sendable (TaskPriority?, @Sendable @escaping () async throws -> Void) async -> Void
@Sendable (String?, TaskPriority?, @Sendable @escaping () async throws -> Void) async -> Void

public func callAsFunction(
name: String? = nil,
priority: TaskPriority? = nil,
@_inheritActorContext _ operation: @Sendable @escaping () async throws -> Void
) async {
await self.operation(priority) { try await operation() }
await self.operation(name, priority) { try await operation() }
}
}

private enum FireAndForgetKey: DependencyKey {
public static let liveValue = FireAndForget { priority, operation in
_ = Task(priority: priority) { try await operation() }
public static let liveValue = FireAndForget { name, priority, operation in
#if compiler(>=6.2)
_ = Task(name: name, priority: priority) { try await operation() }
#else
_ = Task(priority: priority) { try await operation() }
#endif
}
public static let testValue = FireAndForget { priority, operation in
await Task(priority: priority) { try? await operation() }.value
public static let testValue = FireAndForget { name, priority, operation in
#if compiler(>=6.2)
await Task(name: name, priority: priority) {
await withErrorReporting { try await operation() }
}
.value
#else
await Task(priority: priority) {
await withErrorReporting { try await operation() }
}
.value
#endif
}
}
13 changes: 13 additions & 0 deletions Tests/DependenciesTests/FireAndForgetTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ConcurrencyExtras
import Dependencies
import Testing
import XCTest

final class FireAndForgetTests: XCTestCase {
Expand Down Expand Up @@ -83,3 +84,15 @@ final class FireAndForgetTests: XCTestCase {
}
#endif
}

@Test func failure() async {
@Dependency(\.fireAndForget) var fireAndForget
struct Failure: Error {}
await withKnownIssue {
await fireAndForget {
throw Failure()
}
} matching: {
$0.description.contains("Failure()")
}
}
Loading