refactor: concentrate Cast ownership and preserve intent on disconnect

This commit is contained in:
2026-10-16 21:06:53 +02:00
parent f1fbcf30b5
commit 367afdfb07
8 changed files with 600 additions and 205 deletions
+3 -2
View File
@@ -238,6 +238,7 @@ enum CastWire {
final class CastSession {
var onStatus: ((CastStatus) -> Void)?
var onDisconnect: ((String?) -> Void)?
// Recoverable command errors; terminal channel failures belong to onDisconnect.
var onError: ((String) -> Void)?
var localHost: String? {
guard let endpoint = connection?.currentPath?.localEndpoint,
@@ -499,7 +500,7 @@ final class CastSession {
destination: receiver ? "receiver-0" : transport,
operation: (command["type"] as? String ?? "command").lowercased(),
generation: mediaGeneration, id: id) { [weak self] _, error in
guard let self else { return }
guard let self, self.connection != nil else { return }
if self.pendingVolume?.request == id { self.pendingVolume = nil; self.publishSeekState() }
if case .cancelled? = error { return }
if self.pendingSeek == id { self.pendingSeek = nil; self.publishSeekState() }
@@ -760,7 +761,7 @@ final class CastSession {
namespace: CastWire.media, destination: transport, operation: "media status",
generation: self.mediaGeneration) { [weak self] _, error in
if case .cancelled? = error { return }
if let error { self?.onError?(error.localizedDescription) }
if let self, self.connection != nil, let error { self.onError?(error.localizedDescription) }
}
}
} catch { self.fail(error as? CastError ?? .disconnected); return }