feat: Cleaning up some unused code.
This commit is contained in:
@@ -12,7 +12,11 @@ public extension MQTTConnectionManager {
|
|||||||
let manager = ConnectionManager(client: client, logger: logger)
|
let manager = ConnectionManager(client: client, logger: logger)
|
||||||
return .init { _ in
|
return .init { _ in
|
||||||
try await manager.connect(cleanSession: cleanSession)
|
try await manager.connect(cleanSession: cleanSession)
|
||||||
|
|
||||||
return manager.stream
|
return manager.stream
|
||||||
|
.removeDuplicates()
|
||||||
|
.eraseToStream()
|
||||||
|
|
||||||
} shutdown: {
|
} shutdown: {
|
||||||
manager.shutdown()
|
manager.shutdown()
|
||||||
}
|
}
|
||||||
@@ -26,6 +30,7 @@ private actor ConnectionManager {
|
|||||||
private let continuation: AsyncStream<MQTTConnectionManager.Event>.Continuation
|
private let continuation: AsyncStream<MQTTConnectionManager.Event>.Continuation
|
||||||
private nonisolated let logger: Logger?
|
private nonisolated let logger: Logger?
|
||||||
private let name: String
|
private let name: String
|
||||||
|
private var started: Bool = false
|
||||||
let stream: AsyncStream<MQTTConnectionManager.Event>
|
let stream: AsyncStream<MQTTConnectionManager.Event>
|
||||||
|
|
||||||
init(
|
init(
|
||||||
@@ -42,6 +47,7 @@ private actor ConnectionManager {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
client.removeCloseListener(named: name)
|
client.removeCloseListener(named: name)
|
||||||
|
client.removeShutdownListener(named: name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect(cleanSession: Bool) async throws {
|
func connect(cleanSession: Bool) async throws {
|
||||||
@@ -51,13 +57,16 @@ private actor ConnectionManager {
|
|||||||
continuation.yield(.connected)
|
continuation.yield(.connected)
|
||||||
|
|
||||||
client.addCloseListener(named: name) { _ in
|
client.addCloseListener(named: name) { _ in
|
||||||
Task {
|
self.continuation.yield(.disconnected)
|
||||||
self.continuation.yield(.disconnected)
|
self.logger?.debug("Connection closed.")
|
||||||
self.logger?.debug("Connection closed.")
|
self.logger?.debug("Reconnecting...")
|
||||||
self.logger?.debug("Reconnecting...")
|
Task { try await self.connect(cleanSession: cleanSession) }
|
||||||
try await self.connect(cleanSession: cleanSession)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.addShutdownListener(named: name) { _ in
|
||||||
|
self.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
client.logger.trace("Failed to connect: \(error)")
|
client.logger.trace("Failed to connect: \(error)")
|
||||||
continuation.yield(.disconnected)
|
continuation.yield(.disconnected)
|
||||||
@@ -66,6 +75,7 @@ private actor ConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nonisolated func shutdown() {
|
nonisolated func shutdown() {
|
||||||
|
client.logger.trace("Shutting down connection.")
|
||||||
continuation.yield(.shuttingDown)
|
continuation.yield(.shuttingDown)
|
||||||
continuation.finish()
|
continuation.finish()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public struct MQTTConnectionManager: Sendable {
|
|||||||
public var shutdown: () -> Void
|
public var shutdown: () -> Void
|
||||||
|
|
||||||
public enum Event: Sendable {
|
public enum Event: Sendable {
|
||||||
|
case notStarted
|
||||||
case connected
|
case connected
|
||||||
case disconnected
|
case disconnected
|
||||||
case shuttingDown
|
case shuttingDown
|
||||||
@@ -65,9 +66,9 @@ public actor MQTTConnectionService: Service {
|
|||||||
// continue to run and handle graceful shutdowns.
|
// continue to run and handle graceful shutdowns.
|
||||||
logger?.trace("Received connection event: \(event)")
|
logger?.trace("Received connection event: \(event)")
|
||||||
}
|
}
|
||||||
|
manager.shutdown()
|
||||||
} onGracefulShutdown: {
|
} onGracefulShutdown: {
|
||||||
self.logger?.trace("Received graceful shutdown.")
|
self.logger?.trace("Received graceful shutdown.")
|
||||||
Task { await self.manager.shutdown() }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ struct Application {
|
|||||||
|
|
||||||
try await withDependencies {
|
try await withDependencies {
|
||||||
$0.psychrometricClient = .liveValue
|
$0.psychrometricClient = .liveValue
|
||||||
// $0.sensorsClient = .live(client: mqtt)
|
|
||||||
$0.topicListener = .live(client: mqtt)
|
$0.topicListener = .live(client: mqtt)
|
||||||
$0.topicPublisher = .live(client: mqtt)
|
$0.topicPublisher = .live(client: mqtt)
|
||||||
$0.mqttConnectionManager = .live(client: mqtt, logger: logger)
|
$0.mqttConnectionManager = .live(client: mqtt, logger: logger)
|
||||||
@@ -59,6 +58,8 @@ struct Application {
|
|||||||
|
|
||||||
try await serviceGroup.run()
|
try await serviceGroup.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try await mqtt.shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user