feat: Fixes some tests and docker builds
This commit is contained in:
@@ -10,7 +10,7 @@ import MQTTNIO
|
||||
@DependencyClient
|
||||
public struct TopicListener: Sendable {
|
||||
|
||||
public typealias Stream = AsyncStream<Result<MQTTPublishInfo, MQTTListenResultError>>
|
||||
public typealias Stream = AsyncStream<Result<MQTTPublishInfo, any Error>>
|
||||
|
||||
/// Create an async stream that listens for changes to the given topics.
|
||||
private var _listen: @Sendable ([String], MQTTQoS) async throws -> Stream
|
||||
@@ -82,6 +82,7 @@ public extension DependencyValues {
|
||||
// MARK: - Helpers
|
||||
|
||||
private actor MQTTTopicListener {
|
||||
|
||||
private let client: MQTTClient
|
||||
private let continuation: TopicListener.Stream.Continuation
|
||||
private let name: String
|
||||
@@ -116,12 +117,12 @@ private actor MQTTTopicListener {
|
||||
func listen(
|
||||
_ topics: [String],
|
||||
_ qos: MQTTQoS = .atLeastOnce
|
||||
) async throws(TopicListenerError) -> TopicListener.Stream {
|
||||
) async throws -> TopicListener.Stream {
|
||||
var sleepTimes = 0
|
||||
|
||||
while !client.isActive() {
|
||||
guard sleepTimes < 10 else {
|
||||
throw .connectionTimeout
|
||||
throw TopicListenerError.connectionTimeout
|
||||
}
|
||||
try? await Task.sleep(for: .milliseconds(100))
|
||||
sleepTimes += 1
|
||||
@@ -135,7 +136,7 @@ private actor MQTTTopicListener {
|
||||
|
||||
guard subscription != nil else {
|
||||
client.logger.error("Error subscribing to topics: \(topics)")
|
||||
throw .failedToSubscribe
|
||||
throw TopicListenerError.failedToSubscribe
|
||||
}
|
||||
|
||||
client.logger.trace("Done subscribing, begin listening to topics.")
|
||||
@@ -144,7 +145,7 @@ private actor MQTTTopicListener {
|
||||
switch result {
|
||||
case let .failure(error):
|
||||
self.client.logger.error("Received error while listening: \(error)")
|
||||
self.continuation.yield(.failure(.init(error)))
|
||||
self.continuation.yield(.failure(MQTTListenResultError(error)))
|
||||
case let .success(publishInfo):
|
||||
if topics.contains(publishInfo.topicName) {
|
||||
self.client.logger.trace("Recieved new value for topic: \(publishInfo.topicName)")
|
||||
@@ -169,6 +170,8 @@ private actor MQTTTopicListener {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
public enum TopicListenerError: Error {
|
||||
case connectionTimeout
|
||||
case failedToSubscribe
|
||||
|
||||
Reference in New Issue
Block a user