diff --git a/Sources/StreamDeckKit/Session/InternalStreamDeckSession.swift b/Sources/StreamDeckKit/Session/InternalStreamDeckSession.swift index a2808db2..6554da9d 100644 --- a/Sources/StreamDeckKit/Session/InternalStreamDeckSession.swift +++ b/Sources/StreamDeckKit/Session/InternalStreamDeckSession.swift @@ -126,32 +126,10 @@ final actor InternalStreamDeckSession { driverVersion.value = version } - guard let info = client.getDeviceInfo() else { - log(.error, "Error fetching device info.") - client.close() + guard let device = createDevice(with: client, service: service) else { continue } - guard let capabilities = client.getDeviceCapabilities() else { - log(.error, "Error fetching device capabilities \(String(reflecting: info)).") - client.close() - continue - } - - let device = StreamDeck(client: client, info: info, capabilities: capabilities) - log(.debug, "StreamDeck device attached (\(String(reflecting: info))).") - - devicesByService[service] = device - - client.setErrorHandler { [weak self] error in - if case let .disconnected = error, let self = self { - Task { await self.stop() } - } - } - device.onClose { [weak self] in - await self?.removeService(service) - } - addDevice(device: device) } } @@ -170,6 +148,35 @@ final actor InternalStreamDeckSession { removeDevice(device: device) } + private func createDevice(with client: StreamDeckClient, service: io_object_t) -> StreamDeck? { + guard let info = client.getDeviceInfo() else { + log(.error, "Error fetching device info.") + client.close() + return nil + } + + guard let capabilities = client.getDeviceCapabilities() else { + log(.error, "Error fetching device capabilities \(String(reflecting: info)).") + client.close() + return nil + } + + let device = StreamDeck(client: client, info: info, capabilities: capabilities) + log(.debug, "StreamDeck device attached (\(String(reflecting: info))).") + + devicesByService[service] = device + + client.setErrorHandler { [weak self] error in + if case let .disconnected = error, let self = self { + Task { await self.stop() } + } + } + device.onClose { [weak self] in + await self?.removeService(service) + } + return device + } + func addDevice(device: StreamDeck) { guard devices.value.firstIndex(of: device) == nil else { return } diff --git a/Sources/StreamDeckSimulator/Views/StreamDeckSimulatorView.swift b/Sources/StreamDeckSimulator/Views/StreamDeckSimulatorView.swift index 2b327d88..4aa875b6 100644 --- a/Sources/StreamDeckSimulator/Views/StreamDeckSimulatorView.swift +++ b/Sources/StreamDeckSimulator/Views/StreamDeckSimulatorView.swift @@ -74,6 +74,7 @@ struct StreamDeckSimulatorView: View { extension StreamDeckSimulatorView { + // swiftlint:disable:next function_body_length static func create( streamDeck product: StreamDeckProduct, config: StreamDeckSimulator.Configuration, diff --git a/Tests/StreamDeckSDKTests/Helper/StreamDeckRobot.swift b/Tests/StreamDeckSDKTests/Helper/StreamDeckRobot.swift index e37eed05..f899328b 100644 --- a/Tests/StreamDeckSDKTests/Helper/StreamDeckRobot.swift +++ b/Tests/StreamDeckSDKTests/Helper/StreamDeckRobot.swift @@ -161,7 +161,7 @@ final class StreamDeckRobot { ) { $0.count == keysCount + 1 && $0.last?.index == index } } } - + func touchKeyTouched( _ index: Int, touched: Bool, @@ -170,13 +170,13 @@ final class StreamDeckRobot { line: UInt = #line ) async throws { let imageCount = recorder.windowImages.count - + try await emit( .keyPress(index: index, pressed: touched), file: file, line: line ) - + if waitForLayout { try await recorder.$windowImages.waitFor( description: "window area was rendered",