Download this file
| 1 | import Foundation |
| 2 | import Observation |
| 3 | |
| 4 | @MainActor @Observable final class ResumeAction { |
| 5 | var connection: ResumeConnection? |
| 6 | var readFailed = false |
| 7 | static func refused(_ error: Error) -> Bool { |
| 8 | guard let error = error as? EquipoAPIError else { return false } |
| 9 | return error.endsSession || error.status.map { (400..<500).contains($0) && $0 != 429 } == true |
| 10 | } |
| 11 | func load(client: EquipoClient, step: ThinStep) async { |
| 12 | if let connection, StoppedWorkActions.connection(for: step, in: [connection]) == nil { |
| 13 | self.connection = nil |
| 14 | } |
| 15 | do { |
| 16 | let list = try await client.resumeConnections() |
| 17 | readFailed = false |
| 18 | connection = StoppedWorkActions.connection(for: step, in: list.items ?? []) |
| 19 | } catch { |
| 20 | readFailed = true |
| 21 | if Self.refused(error) { connection = nil } |
| 22 | } |
| 23 | } |
| 24 | func perform(client: EquipoClient, step: ThinStep) async -> String? { |
| 25 | guard let connection else { return nil } |
| 26 | do { try await client.resumeConnection(connection); return nil } |
| 27 | catch { |
| 28 | // A stale pause revision must never be offered for the next tap. |
| 29 | if Self.refused(error) { self.connection = nil } |
| 30 | await load(client: client, step: step) |
| 31 | return EquipoStrings.text("card.failed") |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |