Skip to content

Commit

Permalink
Add Request.sendAsync() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Feb 20, 2023
1 parent 0849eb7 commit e89cebf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

##### Enhancements

* None.
* Add `Request.sendAsync()` function.
[JP Simard](https://github.com/jpsim)

##### Bug Fixes

Expand Down
13 changes: 13 additions & 0 deletions Source/SourceKittenFramework/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ public enum Request {
/**
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
- returns: SourceKit output as a dictionary.
- throws: Request.Error on fail ()
*/
public func asyncSend() async throws -> [String: SourceKitRepresentable] {
initializeSourceKitFailable
let response = try await sourcekitObject.sendAsync()
defer { sourcekitd_response_dispose(response) }
return fromSourceKit(sourcekitd_response_get_value(response)) as! [String: SourceKitRepresentable]
}

/**
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
- returns: SourceKit output as a dictionary.
- throws: Request.Error on fail ()
*/
Expand Down
25 changes: 25 additions & 0 deletions Source/SourceKittenFramework/SourceKitObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ public final class SourceKitObject {
func sendSync() -> sourcekitd_response_t? {
return sourcekitd_send_request_sync(sourcekitdObject)
}

func sendAsync() async throws -> sourcekitd_response_t {
let handle = UnsafeMutablePointer<sourcekitd_request_handle_t?>.allocate(capacity: 1)

return try await withTaskCancellationHandler {
try await withUnsafeThrowingContinuation { continuation in
sourcekitd_send_request(sourcekitdObject, handle) { response in
enum SourceKitSendError: Error { case error, noResponse }

guard let response else {
continuation.resume(throwing: SourceKitSendError.noResponse)
return
}

if sourcekitd_response_is_error(response) {
continuation.resume(throwing: SourceKitSendError.error)
} else {
continuation.resume(returning: response)
}
}
}
} onCancel: {
sourcekitd_cancel_request(handle)
}
}
}

extension SourceKitObject: SourceKitObjectConvertible {
Expand Down

0 comments on commit e89cebf

Please sign in to comment.