Replace detached tasks with Task.dispatch(on:) (#201)

* Use Task.detach(on:) everywhere that was blocking.
* Don't wrap the continuations in a Task 🤦‍♂️
* Add sendable requirement to closure.
This commit is contained in:
Doug
2022-09-21 17:50:15 +01:00
committed by GitHub
parent 60f0f1605b
commit 8462862afc
9 changed files with 125 additions and 130 deletions
@@ -182,7 +182,7 @@ class ClientProxy: ClientProxyProtocol {
}
func loadUserDisplayName() async -> Result<String, ClientProxyError> {
await Task.detached {
await Task.dispatch(on: .global()) {
do {
let displayName = try self.client.displayName()
return .success(displayName)
@@ -190,11 +190,10 @@ class ClientProxy: ClientProxyProtocol {
return .failure(.failedRetrievingDisplayName)
}
}
.value
}
func loadUserAvatarURLString() async -> Result<String, ClientProxyError> {
await Task.detached {
await Task.dispatch(on: .global()) {
do {
let avatarURL = try self.client.avatarUrl()
return .success(avatarURL)
@@ -202,7 +201,6 @@ class ClientProxy: ClientProxyProtocol {
return .failure(.failedRetrievingAvatarURL)
}
}
.value
}
func accountDataEvent<Content>(type: String) async -> Result<Content?, ClientProxyError> where Content: Decodable {
@@ -218,21 +216,21 @@ class ClientProxy: ClientProxyProtocol {
}
func loadMediaContentForSource(_ source: MatrixRustSDK.MediaSource) async throws -> Data {
try await Task.detached {
try await Task.dispatch(on: .global()) {
let bytes = try self.client.getMediaContent(source: source)
return Data(bytes: bytes, count: bytes.count)
}.value
}
}
func loadMediaThumbnailForSource(_ source: MatrixRustSDK.MediaSource, width: UInt, height: UInt) async throws -> Data {
try await Task.detached {
try await Task.dispatch(on: .global()) {
let bytes = try self.client.getMediaThumbnail(source: source, width: UInt64(width), height: UInt64(height))
return Data(bytes: bytes, count: bytes.count)
}.value
}
}
func sessionVerificationControllerProxy() async -> Result<SessionVerificationControllerProxyProtocol, ClientProxyError> {
await Task.detached {
await Task.dispatch(on: .global()) {
do {
let sessionVerificationController = try self.client.getSessionVerificationController()
return .success(SessionVerificationControllerProxy(sessionVerificationController: sessionVerificationController))
@@ -240,7 +238,6 @@ class ClientProxy: ClientProxyProtocol {
return .failure(.failedRetrievingSessionVerificationController)
}
}
.value
}
func logout() async {