Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
+ Extract function
  • Loading branch information
r-dent committed Aug 21, 2024
1 parent 264504b commit fd1ef4b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
53 changes: 30 additions & 23 deletions Sources/StreamDeckKit/Session/InternalStreamDeckSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct StreamDeckSimulatorView: View {

extension StreamDeckSimulatorView {

// swiftlint:disable:next function_body_length
static func create(
streamDeck product: StreamDeckProduct,
config: StreamDeckSimulator.Configuration,
Expand Down
6 changes: 3 additions & 3 deletions Tests/StreamDeckSDKTests/Helper/StreamDeckRobot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ final class StreamDeckRobot {
) { $0.count == keysCount + 1 && $0.last?.index == index }
}
}

func touchKeyTouched(
_ index: Int,
touched: Bool,
Expand All @@ -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",
Expand Down

0 comments on commit fd1ef4b

Please sign in to comment.