Repository Public
1import Foundation23/// Why a read model cannot be shown at all (as opposed to a transient failure).4public enum UnavailableReason: Sendable, Hashable {5 /// The server does not implement this endpoint yet (`501 not_implemented`).6 case notImplemented7 /// A backend dependency is down (`503`); `code` is the server's (`unavailable`, `orchestrator_frozen`...).8 case serviceUnavailable(code: String)9 /// The session lacks the permission (`403 forbidden`).10 case forbidden11 /// The server cannot be reached and nothing was cached.12 case offline13}1415/// Explicit loading state of a read model. A previously loaded value is kept by the owning `Resource` in16/// `snapshot`, so a screen can keep showing the last good data (with its timestamp) while `state` says what the17/// last attempt did.18public enum Loadable<Value: Sendable>: Sendable {19 case idle20 case loading21 case loaded(Value, receivedAt: Date)22 case failed(APIError)23 case unavailable(UnavailableReason, APIError?)2425 public var isLoading: Bool { if case .loading = self { return true }; return false }26 public var error: APIError? {27 switch self {28 case .failed(let e): return e29 case .unavailable(_, let e): return e30 default: return nil31 }32 }33 public var unavailableReason: UnavailableReason? { if case .unavailable(let r, _) = self { return r }; return nil }34 public var value: Value? { if case .loaded(let v, _) = self { return v }; return nil }35}3637extension Loadable {38 /// Classifies a failed load.39 static func failure(_ error: APIError) -> Loadable {40 if error.isNotImplemented { return .unavailable(.notImplemented, error) }41 if error.isServiceUnavailable { return .unavailable(.serviceUnavailable(code: error.code), error) }42 if error.isForbidden { return .unavailable(.forbidden, error) }43 if error.isTransport || error.code == APIError.codeOffline { return .unavailable(.offline, error) }44 return .failed(error)45 }46}47