feat: Fixes some tests and docker builds

This commit is contained in:
2024-11-14 14:58:09 -05:00
parent e7a849b003
commit 163f603b69
9 changed files with 176 additions and 282 deletions

View File

@@ -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