mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-04 12:07:09 +00:00
refactor(companion): tidy snapshot serialization
This commit is contained in:
1 parent
1cc820637e
commit
629194015d
2 files changed
+14
-11
No files matched your search
@@ -10,6 +10,9 @@ final class Server {
|
|||||||
private let port: NWEndpoint.Port
|
private let port: NWEndpoint.Port
|
||||||
private let listener: NWListener
|
private let listener: NWListener
|
||||||
private let queue = DispatchQueue(label: "dev.sanderling.companion.server")
|
private let queue = DispatchQueue(label: "dev.sanderling.companion.server")
|
||||||
|
// Requests are handled off the network queue so blocking automation work
|
||||||
|
// (snapshot, gesture synthesis) never stalls accept and receive.
|
||||||
|
private let work = DispatchQueue(label: "dev.sanderling.companion.work")
|
||||||
|
|
||||||
// The bundle identifier of the most recently snapshotted app, used as the
|
// The bundle identifier of the most recently snapshotted app, used as the
|
||||||
// default target for typeText when no explicit bundleId is supplied.
|
// default target for typeText when no explicit bundleId is supplied.
|
||||||
@@ -58,13 +61,16 @@ final class Server {
|
|||||||
|
|
||||||
private func handleLine(_ lineData: Data, on connection: NWConnection) {
|
private func handleLine(_ lineData: Data, on connection: NWConnection) {
|
||||||
guard !lineData.isEmpty else { return }
|
guard !lineData.isEmpty else { return }
|
||||||
let requestId = (try? JSONSerialization.jsonObject(with: lineData))
|
work.async { [weak self] in
|
||||||
.flatMap { ($0 as? [String: Any])?["id"] as? Int } ?? 0
|
guard let self = self else { return }
|
||||||
do {
|
let requestId = (try? JSONSerialization.jsonObject(with: lineData))
|
||||||
let response = try dispatch(lineData)
|
.flatMap { ($0 as? [String: Any])?["id"] as? Int } ?? 0
|
||||||
send(response, on: connection)
|
do {
|
||||||
} catch {
|
let response = try self.dispatch(lineData)
|
||||||
send(["id": requestId, "error": "\(error)"], on: connection)
|
self.send(response, on: connection)
|
||||||
|
} catch {
|
||||||
|
self.send(["id": requestId, "error": "\(error)"], on: connection)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ enum Snapshot {
|
|||||||
|
|
||||||
private static func serialize(_ node: XCUIElementSnapshot) -> [String: Any] {
|
private static func serialize(_ node: XCUIElementSnapshot) -> [String: Any] {
|
||||||
let frame = node.frame
|
let frame = node.frame
|
||||||
var element: [String: Any] = [
|
return [
|
||||||
"type": elementTypeName(node.elementType),
|
"type": elementTypeName(node.elementType),
|
||||||
"frame": [
|
"frame": [
|
||||||
"x": Double(frame.origin.x),
|
"x": Double(frame.origin.x),
|
||||||
@@ -44,9 +44,6 @@ enum Snapshot {
|
|||||||
"AXValue": nullableString(stringifyValue(node.value)),
|
"AXValue": nullableString(stringifyValue(node.value)),
|
||||||
"AXUniqueId": nullableString(node.identifier),
|
"AXUniqueId": nullableString(node.identifier),
|
||||||
]
|
]
|
||||||
// Keep the dictionary keys deterministic for readers; values stay as set.
|
|
||||||
_ = element
|
|
||||||
return element
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func nullableString(_ value: String?) -> Any {
|
private static func nullableString(_ value: String?) -> Any {
|
||||||
|
|||||||
Reference in new issue
Block a user