Changes
1 changed files (+67/-37)
-
-
@@ -189,58 +189,88 @@ }} } .task { startLoop() do { try await startLoop() } catch { Logger().info("Cancelled message loop") } } .focusedSceneValue(conn) } func startLoop() { queue.async { let logger = Logger() logger.debug("Starting getEvent loop...") func startLoop() async throws { try await withTaskCancellationHandler( operation: { try await withUnsafeThrowingContinuation { (cont: UnsafeContinuation<Void, any Error>) in queue.async { let logger = Logger() while true { let event = conn.getEvent() logger.debug("Starting getEvent loop...") guard let event = event else { logger.debug("Got null event, quitting") return } while true { if Task.isCancelled { cont.resume(throwing: CancellationError()) return } switch event { case .connected(let ev): onAuthorizeAction?(ev.token) conn.subscribeZoneChanges() break case .connectionError(let ev): logger.error("Failed to connect: \(ev.code.rawValue)") break case .zoneList(let ev): DispatchQueue.main.async { for zone in ev.addedZones { zones[zone.id] = zone } let event = conn.getEvent() for zone in ev.changedZones { zones[zone.id] = zone } if Task.isCancelled { cont.resume(throwing: CancellationError()) return } for id in ev.removedZoneIds { zones.removeValue(forKey: id) if zone_id == id { zone_id = nil guard let event = event else { logger.debug("Got null event, quitting") cont.resume() return } } if zone_id == nil { zone_id = zones.first?.value.id switch event { case .connected(let ev): onAuthorizeAction?(ev.token) conn.subscribeZoneChanges() break case .connectionError(let ev): logger.error("Failed to connect: \(ev.code.rawValue)") break case .zoneList(let ev): DispatchQueue.main.async { for zone in ev.addedZones { zones[zone.id] = zone } for zone in ev.changedZones { zones[zone.id] = zone } for id in ev.removedZoneIds { zones.removeValue(forKey: id) if zone_id == id { zone_id = nil } } if zone_id == nil { zone_id = zones.first?.value.id } } break } } } break } }, onCancel: { actionQueue.sync { NSApp.terminate(nil) } // FIXME: The below "close()" crashes. Once fixed, remove the above "terminate()" call. // actionQueue.async { // conn.close() // } } } ) } }
-