This commit is contained in:
fmodf 2024-10-19 17:17:14 +02:00
parent c02b0ce41f
commit 5689a9e342

View file

@ -83,7 +83,12 @@ final class ClientsStore: ObservableObject {
// MARK: - Login/Connections // MARK: - Login/Connections
extension ClientsStore { extension ClientsStore {
func tryLogin(_ jidStr: String, _ pass: String) async throws { func tryLogin(_ jidStr: String, _ pass: String) async throws {
// login with fake timeout if let client = clients.first(where: { $0.credentials.bareJid == jidStr }) {
// check if credentials already exist and enable it
// this change will invoke reconnect automatically
try? await client.credentials.setActive(flag: true)
} else {
// new client login with fake timeout
async let sleep: Void? = try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC) async let sleep: Void? = try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC)
async let request = try await Client.tryLogin(with: .init(bareJid: jidStr, pass: pass, isActive: true)) async let request = try await Client.tryLogin(with: .init(bareJid: jidStr, pass: pass, isActive: true))
let client = try await(request, sleep).0 let client = try await(request, sleep).0
@ -91,6 +96,7 @@ extension ClientsStore {
clients.append(client) clients.append(client)
try? await client.credentials.save() try? await client.credentials.save()
} }
}
private func reconnectNeeded() { private func reconnectNeeded() {
Task { Task {