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

Graceful client close (fix #36) #38

Merged
merged 3 commits into from
Jul 16, 2023
Merged

Graceful client close (fix #36) #38

merged 3 commits into from
Jul 16, 2023

Conversation

Kylmakalle
Copy link
Contributor

This one fixes #36 and makes the client close properly. However, trying to call client.run() immediately after the client.close() may result in non-working client. I assume this scenario is quite rare.

This PR does not interfere with public API much, except for some throws, but I believe nobody care about these two 404s "Client destroyed"

@XMLHexagram
Copy link
Contributor

XMLHexagram commented Jul 8, 2023

if swiftAsync {
return ""
.addLine(Constants.asyncAvailableString)
.addLine("private func execute<Q, R>(query: Q) async throws -> R where Q: Codable, R: Codable {")
.addLine(" let dto = DTO(query, encoder: encoder)")
.addLine(" return try await withCheckedThrowingContinuation { continuation in")
.addLine(" do {")
.addLine(" try client.send(query: dto) { result in")
.addLine(" if let error = try? self.decoder.decode(DTO<Error>.self, from: result")) {
.addLine(" continuation.resume(with: .failure(error.payload))")
.addLine(" } else {")
.addLine(" let response = self.decoder.tryDecode(DTO<R>.self, from: result)")
.addLine(" continuation.resume(with: response.map { $0.payload })")
.addLine(" }")
.addLine(" }")
.addLine(" } catch let err as Error {")
.addLine(" continuation.resume(with: .failure(err))")
.addLine(" } catch let any {")
.addLine(" let err = Error(code: 500, message: any.localizedDescription)")
.addLine(" continuation.resume(with: .failure(err))")
.addLine(" }")
.addLine(" }")
.addLine("}")
} else {
return ""
.addLine("private func execute<Q, R>(")
.addLine(" query: Q,")
.addLine(" completion: @escaping (Result<R, Swift.Error>) -> Void)")
.addLine(" where Q: Codable, R: Codable {")
.addBlankLine()
.addLine(" let dto = DTO(query, encoder: encoder)")
.addLine(" do {")
.addLine(" try client.send(query: dto) { [weak self] result in")
.addLine(" guard let strongSelf = self else { return }")
.addLine(" if let error = try? strongSelf.decoder.decode(DTO<Error>.self, from: result) {")
.addLine(" completion(.failure(error.payload))")
.addLine(" } else {")
.addLine(" let response = strongSelf.decoder.tryDecode(DTO<R>.self, from: result)")
.addLine(" completion(response.map { $0.payload })")
.addLine(" }")
.addLine(" }")
.addLine(" } catch let err as Error {")
.addLine(" completion( .failure(err))")
.addLine(" } catch let any {")
.addLine(" let err = Error(code: 500, message: any.localizedDescription)")
.addLine(" completion( .failure(err))")
.addLine(" }")
.addLine("}")

Please, also pay attention here, I think remove try! by using do catch and completion( .failure(err)) in the execute will make code much safer.

And I fix it in my PR #37.

@Kylmakalle
Copy link
Contributor Author

@XMLHexagram addressed, thank you!

@Kylmakalle Kylmakalle merged commit a654524 into main Jul 16, 2023
1 check failed
@Kylmakalle Kylmakalle deleted the gracefull-close branch April 8, 2024 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Call client.close() after client.run() will 100% crash the app
2 participants