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
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
40 changes: 27 additions & 13 deletions Sources/TDLibKit/Generated/API/TdApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17994,14 +17994,21 @@ public final class TdApi {
where Q: Codable, R: Codable {

let dto = DTO(query, encoder: self.encoder)
try! client.send(query: dto) { [weak self] result in
guard let strongSelf = self else { return }
if let error = try? strongSelf.decoder.decode(DTO<Error>.self, from: result) {
completion(.failure(error.payload))
} else {
let response = strongSelf.decoder.tryDecode(DTO<R>.self, from: result)
completion(response.map { $0.payload })
do {
try client.send(query: dto) { [weak self] result in
guard let strongSelf = self else { return }
if let error = try? strongSelf.decoder.decode(DTO<Error>.self, from: result) {
completion(.failure(error.payload))
} else {
let response = strongSelf.decoder.tryDecode(DTO<R>.self, from: result)
completion(response.map { $0.payload })
}
}
} catch let err as Error {
completion( .failure(err))
} catch let any {
let err = Error(code: 500, message: any.localizedDescription)
completion( .failure(err))
}
}

Expand All @@ -18010,13 +18017,20 @@ public final class TdApi {
private func execute<Q, R>(query: Q) async throws -> R where Q: Codable, R: Codable {
let dto = DTO(query, encoder: self.encoder)
return try await withCheckedThrowingContinuation { continuation in
try! client.send(query: dto) { result in
if let error = try? self.decoder.decode(DTO<Error>.self, from: result) {
continuation.resume(with: .failure(error.payload))
} else {
let response = self.decoder.tryDecode(DTO<R>.self, from: result)
continuation.resume(with: response.map { $0.payload })
do {
try client.send(query: dto) { result in
if let error = try? self.decoder.decode(DTO<Error>.self, from: result) {
continuation.resume(with: .failure(error.payload))
} else {
let response = self.decoder.tryDecode(DTO<R>.self, from: result)
continuation.resume(with: response.map { $0.payload })
}
}
} catch let err as Error {
continuation.resume(with: .failure(err))
} catch let any {
let err = Error(code: 500, message: any.localizedDescription)
continuation.resume(with: .failure(err))
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions Sources/TDLibKit/TdClientImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ open class TdClientImpl: TdClient {
if !stopFlag {
try! send(query: DTO(Close()), completion: { _ in })
}
isClientDestroyed = true
td_json_client_destroy(client)
}

/// Receives incoming updates and request responses from the TDLib client
Expand All @@ -64,12 +62,18 @@ open class TdClientImpl: TdClient {
self.logger?.log(String(cString: res), type: .receive)
self.queryResultAsync(data)
}
self.isClientDestroyed = true
td_json_client_destroy(self.client)
self.stopFlag = false
}
}

/// Sends request to the TDLib client.
public func send(query: TdQuery, completion: (CompletionHandler)? = nil) throws {
guard !self.isClientDestroyed else { throw Error(code: 404, message: "Client destroyed") }
guard !self.isClientDestroyed else {
logger?.log("Client destroyed. Query send aborted. Query: \(query)", type: .custom("Warning"))
return
}

tdlibQueryQueue.async { [weak self] in
guard let `self` = self else { return }
Expand All @@ -95,7 +99,10 @@ open class TdClientImpl: TdClient {

/// Synchronously executes TDLib request.
public func execute(query: TdQuery) throws -> [String:Any]? {
guard !self.isClientDestroyed else { throw Error(code: 404, message: "Client destroyed") }
guard !self.isClientDestroyed else {
logger?.log("Client destroyed. Execution aborted. Query: \(query)", type: .custom("Warning"))
return nil
}

do {
let data = try query.make(with: nil)
Expand Down
8 changes: 7 additions & 1 deletion Tests/TDLibKitTests/TDLibKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class TDLibKitTests: XCTestCase {
}

override func tearDownWithError() throws {
// api.client.close()
self.client.close()
try super.tearDownWithError()
}

Expand All @@ -103,6 +103,12 @@ class TDLibKitTests: XCTestCase {
let configs = await [config1, config2, config3, config4, config5, config6, config7, config8, config9, config10]
print("Application Configs \(configs)")
}

func testGetCountries() async {
let countries = try! await api.getCountries()
print("Countries \(countries)")
XCTAssertNotEqual(countries, nil)
}

}

Expand Down
40 changes: 27 additions & 13 deletions scripts/tl2swift/Sources/TlParserLib/Composer/MethodsComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,20 @@ final class MethodsComposer: Composer {
.addLine("private func execute<Q, R>(query: Q) async throws -> R where Q: Codable, R: Codable {")
.addLine(" let dto = DTO(query, encoder: self.encoder)")
.addLine(" return try await withCheckedThrowingContinuation { continuation in")
.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(" 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("}")
Expand All @@ -216,14 +223,21 @@ final class MethodsComposer: Composer {
.addLine(" where Q: Codable, R: Codable {")
.addBlankLine()
.addLine(" let dto = DTO(query, encoder: self.encoder)")
.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(" 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("}")
}
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.2
1.4.3
Loading