Skip to content

Commit c430136

Browse files
committed
consolidate post-connect logic
1 parent 52439e0 commit c430136

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

Sources/SwiftOCA/OCP.1/Ocp1Connection+Connect.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ extension Ocp1Connection {
109109
}
110110
}
111111

112+
private func _didConnect() async {
113+
_updateConnectionState(.connected)
114+
await _refreshDeviceTreeWithPolicy()
115+
await refreshSubscriptions()
116+
await refreshCachedObjectProperties()
117+
}
118+
112119
/// reconnect to the OCA device with exponential backoff, updating
113120
/// connectionState
114121
private func reconnectDeviceWithBackoff() async throws {
@@ -118,7 +125,7 @@ extension Ocp1Connection {
118125
for i in 0..<options.reconnectMaxTries {
119126
do {
120127
try await _connectDeviceWithTimeout()
121-
_updateConnectionState(.connected)
128+
await _didConnect()
122129
break
123130
} catch {
124131
lastError = error
@@ -139,10 +146,6 @@ extension Ocp1Connection {
139146
_updateConnectionState(.notConnected)
140147
throw Ocp1Error.notConnected
141148
}
142-
143-
await _refreshDeviceTreeWithPolicy()
144-
await refreshSubscriptions()
145-
await refreshCachedObjectProperties()
146149
}
147150

148151
private var _needsReconnect: Bool {
@@ -236,9 +239,9 @@ public extension Ocp1Connection {
236239
if connectionState == .connecting {
237240
logger.trace("connected")
238241
_updateConnectionState(.connected)
239-
await _refreshDeviceTreeWithPolicy()
242+
await _didConnect()
240243
} else if connectionState != .connected {
241-
logger.trace("connection failed whilst connecting: \(connectionState)")
244+
logger.trace("connection failed whilst attempting to connect: \(connectionState)")
242245
throw connectionState.error ?? .notConnected
243246
}
244247
}
@@ -248,9 +251,7 @@ public extension Ocp1Connection {
248251

249252
_updateConnectionState(.notConnected)
250253

251-
try await disconnectDevice(
252-
clearObjectCache: !options.flags
253-
.contains(.retainObjectCacheAfterDisconnect)
254-
)
254+
let clearObjectCache = !options.flags.contains(.retainObjectCacheAfterDisconnect)
255+
try await disconnectDevice(clearObjectCache: clearObjectCache)
255256
}
256257
}

Sources/SwiftOCA/OCP.1/Ocp1Connection.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,11 @@ open class Ocp1Connection: CustomStringConvertible, ObservableObject {
342342
try await monitor!.run()
343343
}
344344

345-
subscriptions = [:]
346-
347345
if heartbeatTime > .zero {
348346
// send keepalive to open UDP connection
349347
try await sendKeepAlive()
350348
}
351349

352-
// refresh all objects
353-
for (_, object) in objects {
354-
await object.refresh()
355-
}
356-
357350
#if canImport(Combine) || canImport(OpenCombine)
358351
objectWillChange.send()
359352
#endif

0 commit comments

Comments
 (0)