Skip to content

Commit 8df845e

Browse files
Allow custom decoders in decoding completions
1 parent a676f19 commit 8df845e

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

Sources/AsyncXPCConnection/NSXPCConnection+Continuations.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Combine
12
import Foundation
23

34
enum ConnectionContinuationError: Error {
@@ -316,42 +317,45 @@ extension NSXPCConnection {
316317

317318
#if compiler(<6.0)
318319
@_unsafeInheritExecutor
319-
public func withDecodingCompletion<Service, Value: Decodable>(
320+
public func withDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
320321
function: String = #function,
322+
using decoder: Decoder = JSONDecoder(),
321323
_ body: (Service, @escaping @Sendable (Data?, Error?) -> Void) -> Void
322-
) async throws -> Value {
324+
) async throws -> Value where Decoder.Input == Data {
323325
let data: Data = try await withValueErrorCompletion(function: function) { service, handler in
324326
body(service, handler)
325327
}
326328

327-
return try JSONDecoder().decode(Value.self, from: data)
329+
return try decoder.decode(Value.self, from: data)
328330
}
329331
#else
330-
public func withDecodingCompletion<Service, Value: Decodable>(
332+
public func withDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
331333
isolation: isolated (any Actor)? = #isolation,
332334
function: String = #function,
335+
using decoder: Decoder = JSONDecoder(),
333336
_ body: (Service, @escaping (Data?, Error?) -> Void) -> Void
334-
) async throws -> Value {
337+
) async throws -> Value where Decoder.Input == Data {
335338
let data: Data = try await withValueErrorCompletion(isolation: isolation, function: function) { service, handler in
336339
body(service, handler)
337340
}
338341

339-
return try JSONDecoder().decode(Value.self, from: data)
342+
return try decoder.decode(Value.self, from: data)
340343
}
341344
#endif
342345

343346
#if compiler(>=6.0)
344-
@available(*, deprecated, message: "please use withDecodingCompletion(isolation:function:_:)")
347+
@available(*, deprecated, message: "please use withDecodingCompletion(isolation:function:using:_:)")
345348
#endif
346-
public func withDecodingCompletion<Service, Value: Decodable>(
349+
public func withDecodingCompletion<Service, Value: Decodable, Decoder: TopLevelDecoder>(
347350
function: String = #function,
348351
on actor: isolated some Actor,
352+
using decoder: Decoder = JSONDecoder(),
349353
_ body: (Service, @escaping @Sendable (Data?, Error?) -> Void) -> Void
350-
) async throws -> Value {
354+
) async throws -> Value where Decoder.Input == Data {
351355
let data: Data = try await withValueErrorCompletion(function: function, on: actor) { service, handler in
352356
body(service, handler)
353357
}
354358

355-
return try JSONDecoder().decode(Value.self, from: data)
359+
return try decoder.decode(Value.self, from: data)
356360
}
357361
}

Sources/AsyncXPCConnection/QueuedRemoteXPCService.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Combine
12
import Foundation
23

34
public protocol AsyncQueuing {
@@ -109,14 +110,15 @@ extension QueuedRemoteXPCService {
109110
return try await task.value
110111
}
111112

112-
public func addDecodingOperation<Value: Sendable & Decodable>(
113+
public func addDecodingOperation<Value: Sendable & Decodable, Decoder: TopLevelDecoder>(
113114
barrier: Bool = false,
115+
using decoder: Decoder = JSONDecoder(),
114116
operation: @escaping (Service, @escaping ValueErrorOperationHandler<Data>) -> Void
115-
) async throws -> Value {
117+
) async throws -> Value where Decoder.Input == Data {
116118
let task: Task<Value, Error> = queue.addOperation(priority: nil, barrier: barrier) {
117119
let conn = try await provider()
118120

119-
return try await conn.withDecodingCompletion { service, handler in
121+
return try await conn.withDecodingCompletion(using: decoder) { service, handler in
120122
operation(service, handler)
121123
}
122124
}

Sources/AsyncXPCConnection/RemoteXPCService.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Combine
12
import Foundation
23

34
/// A thin wrapper around NSXPCConnection with a defined service type.
@@ -88,11 +89,12 @@ extension RemoteXPCService {
8889
}
8990

9091
@_unsafeInheritExecutor
91-
public func withDecodingCompletion<Value: Decodable>(
92+
public func withDecodingCompletion<Value: Decodable, Decoder: TopLevelDecoder>(
9293
function: String = #function,
94+
using decoder: Decoder = JSONDecoder(),
9395
_ body: (Service, @escaping (Data?, Error?) -> Void) -> Void
94-
) async throws -> Value {
95-
try await connection.withDecodingCompletion(function: function, body)
96+
) async throws -> Value where Decoder.Input == Data {
97+
try await connection.withDecodingCompletion(function: function, using: decoder, body)
9698
}
9799
#else
98100
public func withValueErrorCompletion<Value: Sendable>(
@@ -119,12 +121,13 @@ extension RemoteXPCService {
119121
try await connection.withErrorCompletion(isolation: isolation, function: function, body)
120122
}
121123

122-
public func withDecodingCompletion<Value: Decodable>(
124+
public func withDecodingCompletion<Value: Decodable, Decoder: TopLevelDecoder>(
123125
isolation: isolated (any Actor)? = #isolation,
124126
function: String = #function,
127+
using decoder: Decoder = JSONDecoder(),
125128
_ body: (Service, @escaping (Data?, Error?) -> Void) -> Void
126-
) async throws -> Value {
127-
try await connection.withDecodingCompletion(isolation: isolation, function: function, body)
129+
) async throws -> Value where Decoder.Input == Data {
130+
try await connection.withDecodingCompletion(isolation: isolation, function: function, using: decoder, body)
128131
}
129132
#endif
130133
}

0 commit comments

Comments
 (0)