Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with_Completion and with_Operation variants that can return optional values #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions Sources/AsyncXPCConnection/NSXPCConnection+Continuations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ extension NSXPCConnection {
}
}
}

/// Begins remote method invocation that calls out to an optional value-error pair completion handler.
///
/// This function always throws if an error is returned from the completion handler.
@_unsafeInheritExecutor
public func withOptionalValueErrorCompletion<Service, Value: Sendable>(
function: String = #function,
_ body: (Service, @escaping @Sendable (Value?, Error?) -> Void) -> Void
) async throws -> Value? {
try await withContinuation(function: function) { service, continuation in
body(service) { value, error in
switch (value, error) {
case let (_, error?):
continuation.resume(throwing: error)
case let (value, nil):
continuation.resume(returning: value)
}
}
}
}
#else
/// Begins remote method invocation that calls out to a value-error pair completion handler.
///
Expand All @@ -185,6 +205,27 @@ extension NSXPCConnection {
}
}
}

/// Begins remote method invocation that calls out to an optional value-error pair completion handler.
///
/// This function always throws if an error is returned from the completion handler.
/// > Note: The `Value: Sendable` should not be required...
public func withOptionalValueErrorCompletion<Service, Value: Sendable>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
_ body: (Service, sending @escaping (Value?, Error?) -> Void) -> Void
) async throws -> Value? {
try await withContinuation(isolation: isolation, function: function) { service, continuation in
body(service) { value, error in
switch (value, error) {
case let (_, error?):
continuation.resume(throwing: error)
case let (value, nil):
continuation.resume(returning: value)
}
}
}
}
#endif

/// Begins remote method invocation that calls out to a value-error pair completion handler.
Expand Down Expand Up @@ -328,6 +369,21 @@ extension NSXPCConnection {

return try decoder.decode(Value.self, from: data)
}

@_unsafeInheritExecutor
public func withOptionalDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
function: String = #function,
using decoder: Decoder = JSONDecoder(),
_ body: (Service, @escaping @Sendable (Data?, Error?) -> Void) -> Void
) async throws -> Value? where Decoder.Input == Data {
guard let data: Data = try await withOptionalValueErrorCompletion(function: function, { service, handler in
body(service, handler)
}) else {
return nil
}

return try decoder.decode(Value.self, from: data)
}
#else
public func withDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
isolation: isolated (any Actor)? = #isolation,
Expand All @@ -341,6 +397,21 @@ extension NSXPCConnection {

return try decoder.decode(Value.self, from: data)
}

public func withOptionalDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
using decoder: Decoder = JSONDecoder(),
_ body: (Service, sending @escaping (Data?, Error?) -> Void) -> Void
) async throws -> Value? where Decoder.Input == Data {
guard let data: Data = try await withOptionalValueErrorCompletion(isolation: isolation, function: function, { service, handler in
body(service, handler)
}) else {
return nil
}

return try decoder.decode(Value.self, from: data)
}
#endif

#if compiler(>=6.0)
Expand Down
31 changes: 31 additions & 0 deletions Sources/AsyncXPCConnection/QueuedRemoteXPCService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ extension QueuedRemoteXPCService {
return try await task.value
}

public func addOptionalValueErrorOperation<Value: Sendable>(
barrier: Bool = false,
operation: @escaping (Service, sending @escaping ValueErrorOperationHandler<Value>) -> Void
) async throws -> Value? {
let task: Task<Value?, Error> = queue.addOperation(priority: nil, barrier: barrier) {
let conn = try await provider()

return try await conn.withOptionalValueErrorCompletion { service, handler in
operation(service, handler)
}
}

return try await task.value
}

public func addDecodingOperation<Value: Sendable & Decodable, Decoder: TopLevelDecoder>(
barrier: Bool = false,
using decoder: Decoder = JSONDecoder(),
Expand All @@ -119,4 +134,20 @@ extension QueuedRemoteXPCService {

return try await task.value
}

public func addOptionalDecodingOperation<Value: Sendable & Decodable, Decoder: TopLevelDecoder>(
barrier: Bool = false,
using decoder: Decoder = JSONDecoder(),
operation: @escaping (Service, sending @escaping ValueErrorOperationHandler<Data>) -> Void
) async throws -> Value? where Decoder.Input == Data {
let task: Task<Value?, Error> = queue.addOperation(priority: nil, barrier: barrier) {
let conn = try await provider()

return try await conn.withOptionalDecodingCompletion(using: decoder) { service, handler in
operation(service, handler)
}
}

return try await task.value
}
}
34 changes: 34 additions & 0 deletions Sources/AsyncXPCConnection/RemoteXPCService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ extension RemoteXPCService {
try await connection.withValueErrorCompletion(function: function, body)
}

@_unsafeInheritExecutor
public func withOptionalValueErrorCompletion<Value: Sendable>(
function: String = #function,
_ body: (Service, @escaping (Value?, Error?) -> Void) -> Void
) async throws -> Value? {
try await connection.withOptionalValueErrorCompletion(function: function, body)
}

@_unsafeInheritExecutor
public func withResultCompletion<Value: Sendable>(
function: String = #function,
Expand All @@ -96,6 +104,15 @@ extension RemoteXPCService {
) async throws -> Value where Decoder.Input == Data {
try await connection.withDecodingCompletion(function: function, using: decoder, body)
}

@_unsafeInheritExecutor
public func withOptionalDecodingCompletion<Value: Decodable, Decoder: TopLevelDecoder>(
function: String = #function,
using decoder: Decoder = JSONDecoder(),
_ body: (Service, @escaping (Data?, Error?) -> Void) -> Void
) async throws -> Value? where Decoder.Input == Data {
try await connection.withOptionalDecodingCompletion(function: function, using: decoder, body)
}
#else
public func withValueErrorCompletion<Value: Sendable>(
isolation: isolated (any Actor)? = #isolation,
Expand All @@ -105,6 +122,14 @@ extension RemoteXPCService {
try await connection.withValueErrorCompletion(isolation: isolation, function: function, body)
}

public func withOptionalValueErrorCompletion<Value: Sendable>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
_ body: (Service, sending @escaping (Value?, Error?) -> Void) -> Void
) async throws -> Value? {
try await connection.withOptionalValueErrorCompletion(isolation: isolation, function: function, body)
}

public func withResultCompletion<Value: Sendable>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
Expand All @@ -129,5 +154,14 @@ extension RemoteXPCService {
) async throws -> Value where Decoder.Input == Data {
try await connection.withDecodingCompletion(isolation: isolation, function: function, using: decoder, body)
}

public func withOptionalDecodingCompletion<Value: Decodable, Decoder: TopLevelDecoder>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
using decoder: Decoder = JSONDecoder(),
_ body: (Service, sending @escaping (Data?, Error?) -> Void) -> Void
) async throws -> Value? where Decoder.Input == Data {
try await connection.withOptionalDecodingCompletion(isolation: isolation, function: function, using: decoder, body)
}
#endif
}